In my assembly calculator code, I’m encountering a continuous addition operation instead of other arithmetic operations. It seems like there is an issue with the logic that handles different operations. I need assistance in debugging and correcting this problem to ensure that all arithmetic operations function correctly.
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
section .data
msg1 db "Enter a digit: ", 0xA,0xD
len1 equ $ - msg1
msg2 db "Please enter a second digit: ", 0xA,0xD
len2 equ $ - msg2
msg3 db "Enter the operation (+, -, *, /): ", 0xA,0xD
len3 equ $ - msg3
msg4 db "The result is: "
len4 equ $ - msg4
segment .bss
num1 resb 1
num2 resb 1
operation resb 1
result resb 1
section .text
global main
main:
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num1
mov edx, 2
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, len3
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, operation
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, num2
mov edx, 2
int 0x80
mov al, [num1]
sub al, '0'
mov bl, [num2]
sub bl, '0'
mov cl, [operation]
mov dl, '+'
cmp cl, dl
je addition
mov dl, '-'
cmp cl, dl
je subtraction
mov dl, '*'
cmp cl, dl
je multiplication
mov dl, '/'
cmp cl, dl
je division
addition:
add al, bl
add al, '0'
mov [result], al
jmp print_result
subtraction:
sub al, bl
add al, '0'
mov [result], al
jmp print_result
multiplication:
imul bl
add al, '0'
mov [result], al
jmp print_result
division:
mov ah, 0 ; Clear AH before division
cbw ; Extend AL (signed) to AX
cwd ; Extend AX (signed) to DX:AX
idiv bl ; Signed division
mov [result], al
print_result:
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg4
mov edx, len4
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, result
mov edx, 2
int 0x80
mov eax, SYS_EXIT
xor ebx, ebx
int 0x80
New contributor
ShaLud is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.