0

I've been trying to work on a program where I ask for user input in C and enter that into functions (add, palindromes, and factorials) in assembly.

However, I'm running into a wall when trying to add two numbers. My professor want us to ask for two strings from user, convert those into integers, and then add them through assembly. My issue is that I'm not sure that the numbers I'm inputting are going through the assembly code because I will enter numbers like 2 and 1 then it returns 10 or 106 or anything that is not the correct answer. Based on some reading from here and other sites, I placed the EXTERN in the wrong spot (and also global possibly). I'm super new to assembly so any help will be greatly appreciated.

This is a part of the C code:

char addStr(char *a, char *b);    
int addStrings()
    {
        char a[10];
        
        int atoi(const char *a)
        
        char b[10];
        
        int atoi(const char *b)
        
        printf("You want to add two numbers...\n");
        
        printf("Insert the 1st Number: \n");
        
        scanf("%s", a);
        
        printf("Insert the 2nd Number: \n");
        
        scanf("%s", b);
        
        int result = addStr(a, b);
        
        printf("The answer is %d \n", result);
        
        return;
    }

And this is the assembly code:

BITS 32

section .text
    global addStr
    
    EXTERN addStrings
    
    call addStrings


addStr:
    push ebp
    
    mov ebp, esp
    
    push ebx
    
    mov eax, [ebp + 8]
    
    sub eax, '0'
    
    mov ebx, [ebp + 12]
    
    sub ebx, '0'

    add eax, ebx 
    
    add eax, '0'
    
    pop ebx
    
    pop ebp
        
    ret
zx485
  • 26,827
  • 28
  • 51
  • 55
  • You're correctly passing args to your function. Your function args are `char*`, aka strings, not single `char` objects. You need to loop over each string and do `total = total*10 + digit`. – Peter Cordes Dec 01 '21 at 10:15

0 Answers0