0

I am trying to compare the values of 2 variables to each other in assembly. I'm moving 3 into both variables. I want to compare the variables to each other. I've tried using brackets on each register and variable when comparing and it will not compile, such as cmp [eax], [ebx] and cmp [num], [num1]. Is there a way to compare them to each other or can you only compare by using cmp [num], byte 3? Thanks for the help.

section .data
hello:     db 'Hello world!',10    ; 'Hello world!' plus a linefeed character
helloLen:  equ $-hello             ; Length of the 'Hello world!' string

section .bss
num resb 1
num1 resb 1

section .text
global _start

_start:

mov [num], byte 3
mov [num1], byte 3

mov eax, [num]
mov ebx, [num1]

cmp eax, ebx
jne end

add [num], byte '0'
mov eax,4            ; The system call for write (sys_write)
mov ebx,1            ; File descriptor 1 - standard output
mov ecx,num        ; Put the offset of hello in ecx
mov edx,1     ; helloLen is a constant, so we don't need to say
                     ;  mov edx,[helloLen] to get it's actual value
int 80h     

add [num1], byte '0'
mov eax,4            ; The system call for write (sys_write)
mov ebx,1            ; File descriptor 1 - standard output
mov ecx,num1        ; Put the offset of hello in ecx
mov edx,1     ; helloLen is a constant, so we don't need to say
                    
int 80h     

end:

mov eax,4            ; The system call for write (sys_write)
mov ebx,1            ; File descriptor 1 - standard output
mov ecx,hello        ; Put the offset of hello in ecx
mov edx,helloLen     ; helloLen is a constant, so we don't need to say
int 80h   


mov eax,1            ; The system call for exit (sys_exit)
mov ebx,0            ; Exit with return code of 0 (no error)
int 80h;
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Noelle472
  • 1
  • 3

1 Answers1

1

You can just move them to registers and compare them

mov al, byte[num]

cmp al, byte[num1]
jg someLabelIfGreater
je someLabelIfEqual
; etc

If you are unsure whether you can use a particular combination of operands you can always check the reference.

https://c9x.me/x86/html/file_module_x86_id_35.html

As you can see the comparisons you can make are:

CMP AL, imm8      Compare imm8 with AL.
CMP AX, imm16     Compare imm16 with AX.
CMP EAX, imm32    Compare imm32 with EAX.
CMP r/m8, imm8    Compare imm8 with r/m8.
CMP r/m16, imm16. Compare imm16 with r/m16.
CMP r/m32,imm32   Compare imm32 with r/m32.
CMP r/m16,imm8    Compare imm8 with r/m16.
CMP r/m32,imm8    Compare imm8 with r/m32.
CMP r/m8,r8       Compare r8 with r/m8.
CMP r/m16,r16     Compare r16 with r/m16.
CMP r/m32,r32     Compare r32 with r/m32.
CMP r8,r/m8       Compare r/m8 with r8.
CMP r16,r/m16     Compare r/m16 with r16.
CMP r32,r/m32     Compare r/m32 with r32.

Here r stands for register, m for memory, imm for an immediate number which means numbers that you hardcode (like 0xF4 or something). There is no cmp m8, m8 or equivalent thing in the list so you cannot compare these variables directly.