SteryNomo
SteryNomo

Reputation: 25

how can i compare an input number and a number in assembly?

i trying to compare a keyboard input and a already defined number in assembly but it won't work. so thats my code:

; is number bigger than 5 or not

  section .text
  global _start

_start:
  
  mov eax, 4
  mov ebx, 1
  mov ecx, asknum
  mov edx, lennum
  int 80h

  mov eax, 3
  mov ebx, 0
  mov ecx, num
  mov edx, 5
  int 80h
  
  mov eax, [num]
  sub eax, '0'

  cmp eax, num5
  jg gt5 
  jl lt5
  int 80h
  jmp cont

cont:
  int 80h
  mov eax, 4
  mov ebx, 1
  mov ecx, ifequ
  mov edx, lene
  int 80h
  jmp end

gt5:
  int 80h

  mov eax, 4
  mov ebx, 1
  mov ecx, resifyes
  mov edx, lenyes
  int 80h
  jmp end

lt5:
  int 80h

  mov eax, 4
  mov ebx, 1
  mov ecx, resifno
  mov edx, lenno
  int 80h
  jmp end

end:
  mov eax, 1
  int 80h

  section .data
asknum: db "Please enter a number and i will tell you if your number bigger than 5 or not: ", 0xa, 0xd
lennum: equ $-asknum

ifequ: db "Your number is equal to five.", 0xa, 0xd
lene: equ $-ifequ

resifno: db "No, your number not bigger than 5.", 0xa, 0xd
lenno: equ $-resifno

resifyes: db "Yes, your number bigger than 5", 0xa, 0xd
lenyes: equ $-resifyes

num5: db 5

  segment .bss
num resb 5

but i receive same line 'no, your number not bigger than 5.' i compile with nasm and ld so maybe i miss some flags but i couldn't find that.

i tried everything about data types and i still can not solve it. by the way i trying to execute on linux if you need.

Upvotes: 0

Views: 969

Answers (1)

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32732

You're using a 32 bit register (eax) when you should be using an 8 bit one (al) when loading from num and comparing with num5. Correcting the syntax so you compare with the value of num5 instead of the address, you get:

mov al,[num]
sub al,'0'

cmp al,[num5]

Upvotes: 3

Related Questions