I writing a simple calculator in Assembly x86 with Linux system calls, everything work's great but i have a problem with displaying my calculation result. I checked my code two times and don't know what's wrong with my code.
section .data ;Data segment
firstNumberMsg db 'Please give the first number: ' ;Ask the user for first number to calculate
lenFirstNumberMsg equ $-firstNumberMsg ;The length of the message
secondNumberMsg db 'Please give the second number: ' ;Ask the user for second number to calculate
lenSecondNumberMsg equ $-secondNumberMsg ;The length of the message
signMsg db 'Please give the calculation sign: ' ;Ask the user for calculation type
lenSignMsg equ $-signMsg ;The length of the message
resultMsg db 'Result: ', 0xA, 0xD
lenResultMsg equ $-resultMsg
section .bss
first resb 5
second resb 5
result resb 10
sign resb 1
section .text
global _start
exit: ;Exit from program
mov eax, 1
mov ebx, 0
int 80h
equalsSignSum:
;sum system
xor eax, eax; set result to zero
add eax, first ; add first to result
add eax, second ; add second to result
mov [result], eax
;display result section
mov eax, 4
mov ebx, 1
mov ecx, resultMsg
mov edx, lenResultMsg
int 80h
;display the result number
mov eax, 4
mov ebx, 1
mov ecx, [result]
mov edx, 10
int 80h
jmp exit
_start: ;Display the first prompt for entering a number
mov eax, 4
mov ebx, 1
mov ecx, firstNumberMsg
mov edx, lenFirstNumberMsg
int 80h
;Read stdin from user (as a first number)
mov eax, 3
mov ebx, 2
mov ecx, first
mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
int 80h
;Display the second prompt for entering a number
mov eax, 4
mov ebx, 1
mov ecx, secondNumberMsg
mov edx, lenSecondNumberMsg
int 80h
;Read stdin from user (as a second number)
mov eax, 3
mov ebx, 2
mov ecx, second
mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
int 80h
;Display the sign prompt
mov eax, 4
mov ebx, 1
mov ecx, signMsg
mov edx, lenSignMsg
int 80h
;Read stdin from user (as a sign)
mov eax, 3
mov ebx, 2
mov ecx, sign
mov edx, 1 ;1 byte of length (we need to get a character as sign)
int 80h
;Calculation sign section
mov ebx, 43 ;43 is '+' in ascii
cmp [sign], ebx
je equalsSignSum ;jump to this label if user entered '+' as sign
;else
; Exit code
mov eax, 1
mov ebx, 0
int 80h
I have this code and assembling with nasm -f elf calculator.asm && ld -m elf_i386 -s -o calculator calculator.o && ./calculator
And everything dones without any errors, but when i launch my program with this input:
Please give the first number: 10
Please give the second number: 5
Please give the calculation sign: +
Result:
The Result line don't gives me the result of this calculation, i want to get Result: 15
, i get nothing. I don't know why it doesn't working.