简体   繁体   中英

Printing bigger number in NASM

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM