0

So I'm trying to write an assembly code to compare two numbers. So far I managed to get the code to run for single digits. The problem starts when I enter double digits. For example, if I enter 25 and 12 , the output will be 25. But if I enter say, 21 and 13, the output will be 13. I realized that the program is only comparing the last part of my digits(for 25 and 12 it compares 5 and 2 and for 21 and 13 it compares 1 and 3) and outputting the largest of them instead of comparing the whole number. This is the code I wrote:

section .text
     global _start
_start:
;Prompt user to input first number
mov eax , 4
mov ebx , 1
mov ecx , msg1
mov edx , lenmsg1
int 0x80

;input first number
mov eax, 3
mov ebx, 2
mov ecx,num1
mov edx, 5
int 0x80

;prompt user to input second number
mov eax, 4
mov ebx, 1
mov ecx, msg2
mov edx, lenmsg2
int 0x80

;input second number
mov eax, 3
mov ebx, 2
mov ecx, num2
mov edx, 5
int 0x80

mov ecx, [num1]
CMP ecx ,[num2]
JG outcondition
;resmsg
mov eax, 4
mov ebx, 1
mov ecx, resmsg
mov edx, lenresmsg
int 0x80

;resultmsg
mov ecx, num2
mov eax , 4
mov ebx, 1
mov ecx, ecx
mov edx ,2
int 0x80

JMP exit


outcondition:
mov ecx , num1
mov eax, 4
mov ebx, 1
mov ecx, resmsg
mov edx, lenresmsg
int 0x80

;resultmsg
mov ecx, num1
mov eax , 4
mov ebx, 1
mov ecx,ecx
mov edx ,2
int 0x80

exit:
mov eax, 1
int 0x80


section .bss
num1 resb 5
num2 resb 5

section .data
msg1 db "Enter your first number ", 0xA , 0xD
lenmsg1 equ $ - msg1

msg2 db "Enter your second number ", 0xA , 0xD
lenmsg2 equ $ - msg2

resmsg db "The bigger number is " , 0xA , 0xD
lenresmsg equ $ - resmsg
Retr0_Xan
  • 1
  • 1
  • Have you tried debugging? If so, share with us where/when it goes wrong. – Erik Eidt Jul 26 '22 at 03:34
  • 2
    You understand when reading `num1` and `num2` you are reading ASCII digits, not integers, right? It's unclear because there are no numeric conversions, but `mov ecx, [num1]` and `CMP ecx ,[num2]` would need to only compare the byte in `cl` as multiple ASCII digits and their integer representation would be entirely different. – David C. Rankin Jul 26 '22 at 04:24

0 Answers0