Reputation: 138
I want to say that I'm a complete noob at assembly and started to learn it just a few days ago. Learned a bit about user input, registers, and definitions. Now I tried to combine all that in a calculator program. But at first summing, there's an issue. The program outputs the welcome message, but the result isn't printed.
Can someone help me, please?
section .bss
sinput1: resb 255
sinput2: resb 255
section .data
msg db 'Welcome to the Calculator',0xa
lenMsg equ $ - msg
section .text
global _start
_start:
;Print out the Welcome message
mov eax,4
mov ebx,1
mov edx, lenMsg
mov ecx, msg
int 80h
;Input first digit
mov edx,255
mov ecx,sinput1
mov ebx,0
mov eax,3
int 80h
;Input second digit
mov edx,255
mov ecx,sinput2
mov ebx,0
mov eax,3
int 80h
;Sum them up
mov esi,sinput1
mov edx,sinput2
add esi,edx
;Print out the result
mov eax,4
mov ebx,1
mov edx, 255
mov ecx, esi
int 80h
;Quit the program
mov eax,1
int 80h
Upvotes: 2
Views: 120
Reputation: 39676
The instruction mov esi, sinput1
moves the address of your first buffer in the ESI
register, but you really want the byte that is stored there. You retrieve it through mov al, [sinput1]
.
Similarly the instruction mov edx, sinput2
moves the address of your second buffer in the EDX
register, but you really want the byte that is stored there. You retrieve it through mov dl, [sinput2]
.
Next these bytes will be characters, hopefully in the range "0" to "9", but your addition would love to work with the values that these characters represent. For this to happen you need to subtract 48 from the ASCII codes of both characters.
Once you get a correct sum you need to convert it into a character, ready for displaying. This requires you to add 48 in order to obtain the ASCII code that sys_write can use.
The code below will output
Welcome to the Calculator
7
if you input using following keys
3Enter4Enter
mov al, [sinput1] ; Character "3"
sub al, '0' ; Convert to 3
mov dl, [sinput2] ; Character "4"
sub dl, '0' ; Convert to 4
add al, dl ; Sum is 7
add al, '0' ; Convert to character "7"
mov ah, 0Ah ; Newline
mov [sinput1], ax ; Store both in buffer
;Print out the result
mov edx, 2 ; Character and Newline
mov ecx, sinput1 ; Buffer
mov ebx, 1 ; Stdout
mov eax, 4 ; sys_write
int 80h
For this to become a robust program you still need
EAX
from sys_read !Upvotes: 2