0

I'm trying to write this little program which accepts 2 integers and adds them and displays them to the user. I manage to write the entire program and save the variables (number1, number2), and now I have to store number1 + number2 in result.

Here's the code. Where does the add function go?

segment .data 

    msg db "Please enter a digit ", 0xA,0xD 
    len equ $ - msg 

segment .bss

    number1 resb 10 
    number2 resb 10 
    result resb 10    

segment .text 

    msg2 db "Please enter a second digit", 0xA,0xD 
    len2 equ $ - msg2 

    msg3 db "The sum is : ", 0xA,0xD 
    len3 equ $ - msg3

global _start 

 _start: 

    mov eax, 4         
    mov ebx, 1         
    mov ecx, msg         
    mov edx, len 
    int 0x80                

    mov eax, 3 
    mov ebx, 0  
    mov ecx, number1 
    mov edx, 10 
    int 0x80            

    mov eax, 4        
    mov ebx, 1         
    mov ecx, msg2          
    mov edx, len2         
    int 0x80

    mov eax, 3  
    mov ebx, 0  
    mov ecx, number2 
    mov edx, 10 
    int 0x80        

    mov eax, 4         
    mov ebx, 1         
    mov ecx, msg3          
    mov edx, len3         
    int 0x80

    ;add result, number1
    ;add result, number2 

    mov eax, 4        
    mov ebx, 1 

    mov ecx, $result         
    mov edx, len3         
    int 0x80


exit:    
    mov eax, 1   
    xor ebx, ebx 
    int 0x80  
nathanchere
  • 8,008
  • 15
  • 65
  • 86
Shawn ricshawnawic
  • 147
  • 2
  • 5
  • 13

1 Answers1

1

For a start, the digits you get from the input will be in ASCII, so you're going to need to subtract '0' from them to get the actual decimal value. For this case I'm assuming each number is 1 digit long and I've made the buffers 2 bytes in size (to read the digit + '\n').

Once the numbers (single digits as stated) have been added, add '0' to the result to convert from decimal to ASCII. This will obviously only work for a result between 0-9; since this sounds like it might be homework I'm not going to add code that'll print numbers with multiple digits.

Anyway I've added a few lines to show you how to get started and neatened up some of your mess. You should really use equates for things like syscall numbers.

    SYS_EXIT  equ 1
    SYS_READ  equ 3
    SYS_WRITE equ 4
    STDIN     equ 0
    STDOUT    equ 1

segment .data 

    msg db "Please enter a digit ", 0xA,0xD 
    len equ $- msg 

segment .bss

    number1 resb 2 
    number2 resb 2 
    result resb 1    

segment .text 

    msg2 db "Please enter a second digit", 0xA,0xD 
    len2 equ $- msg2 

    msg3 db "The sum is: "
    len3 equ $- msg3

global _start 

 _start: 

    mov eax, SYS_WRITE         
    mov ebx, STDOUT         
    mov ecx, msg         
    mov edx, len 
    int 0x80                

    mov eax, SYS_READ 
    mov ebx, STDIN  
    mov ecx, number1 
    mov edx, 2
    int 0x80            

    mov eax, SYS_WRITE        
    mov ebx, STDOUT         
    mov ecx, msg2          
    mov edx, len2         
    int 0x80

    mov eax, SYS_READ  
    mov ebx, STDIN  
    mov ecx, number2 
    mov edx, 2
    int 0x80        

    mov eax, SYS_WRITE         
    mov ebx, STDOUT         
    mov ecx, msg3          
    mov edx, len3         
    int 0x80

    ; load number1 into eax and subtract '0' to convert from ASCII to decimal
    mov eax, [number1]
    sub eax, '0'
    ; do the same for number2
    mov ebx, [number2]
    sub ebx, '0'

    ; add eax and ebx, storing the result in eax
    add eax, ebx
    ; add '0' to eax to convert the digit from decimal to ASCII
    add eax, '0'

    ; store the result in result
    mov [result], eax

    ; print the result digit
    mov eax, SYS_WRITE        
    mov ebx, STDOUT
    mov ecx, result         
    mov edx, 1        
    int 0x80


exit:    
    mov eax, SYS_EXIT   
    xor ebx, ebx 
    int 0x80
AusCBloke
  • 18,014
  • 6
  • 40
  • 44
  • Hey really thanks for your help , just to clarify this is not homework lol teachers dont give that kind of easy assignments im a university student just learning a new language , btw i increase the size of the buffer to 4 bytes but it still adds only single characters .... do i need to make a new ecx register and parse the ascii char in there and later concatenate "assume concatenation is easy with assembly" – Shawn ricshawnawic Oct 26 '11 at 13:27
  • Hey just figured it out :D i just increased the buffer size but here a question suppose my buffer size is 4 right and 1+1 =2$$$ , why do i get funny characters like that at the end of my answer is there any was to fix this ? but when i do 12 +12 = 24$$ ... – Shawn ricshawnawic Oct 26 '11 at 13:34
  • You're probably getting funny characters because you're printing out 4 characters when only two are digits and the others might be characters like the newline (`\n`), so when you're adding `'0'` to convert from decimal to ASCII you might also be doing it to the extra characters that aren't digits. Anyway, I'm not 100% sure you're adding or printing the numbers up right, if you're not there's a few questions like [this one](http://stackoverflow.com/questions/7884143/how-to-print-numbers-greater-then-10-in-arm200-assembly-language) that explain how to do so properly in ASM. – AusCBloke Oct 26 '11 at 20:02