Reputation: 3
I'm just starting learning assembly and i've encountered a little problem. I'm trying to code a program which takes two integers and prints the bigger one. I want to do this using printf and scanf from C. Unfortunately what I wrote always returns the second value and I keep wondering why. Here's the code:
extern printf
extern scanf
global main
section .text
main:
push rbp
;input of the first number
mov rdi, fmt
mov rsi, number1
xor rax, rax
call scanf
;input of the second number
mov rdi, fmt
mov rsi, number2
xor rax, rax
call scanf
;comparing numbers
mov rdx, qword [number1]
cmp rdx, qword [number2]
jl _1isSmaller
jge _2isSmaller
_1isSmaller: ;1st number is smaller
mov rdi, fmt_out
mov rsi, qword [number1]
xor rax, rax
call printf
jmp _exit
_2isSmaller: ;2nd number is smaller
mov rdi, fmt_out
mov rsi, qword [number2]
xor rax, rax
call printf
jmp _exit
_exit:
pop rbp
mov rax, 0
mov rbx, 1
int 80h
section .data
fmt db "%d", 0
fmt_out db "Smaller number: %d", 10, 0
number1 dd 0
number2 dd 0
Is there anyone who can help me? Thanks in advance
Upvotes: 0
Views: 152
Reputation: 29042
You have defined your numbers (number1
and number2
) as DWORD
s with dd
in the .data
-section, but you're referencing them as QWORD
s with cmp
.
So the result is quite unpredictable/depending on the memory layout.
Upvotes: 2