Trying to write a small program to compare two numbers (on NASM under Linux)

section .data value1 db 6 value2 db 77 messege_success db "success1!" lenSuccessMsg equ $-messege_success section .text global _start _start: mov eax,[value1] mov ebx,[value2] ;Сравнение, если eax<ebx, то переходим на success_story cmp eax,ebx jl success_story jmp exit_story success_story: mov eax, 4 mov ebx, 1 mov ecx, messege_success mov edx, lenSuccessMsg int 80h jmp exit_story exit_story: mov eax, 1 mov ebx, 0 int 80h 

In theory, if eax is less than ebx, then there should be a transition to the success_story label, but the transition is always there (regardless of value2, we set 0 or 99, we still get the transition). What could be my mistake?

  • I apologize for the stupid question, but how to add it with zeros? - Vadimcg

1 answer 1

Your variables are declared as db - i.e. size one byte. Before you compare them, you load them into 4-byte registers. I do not know why your assembler did not give you a warning, but the value of "'u', 's', 77, 6" falls into the EAX register, in EBX, respectively, shifted by 1 byte.

To compare single-byte values, use single-byte registers:

 mov al, [value1] cmp al, [value2] 

If for some reason you need to work with these bytes as four-byte values, you must use the load in the register with the extension zero:

 movzx eax, byte ptr [value1]