简体   繁体   English

在NASM中打印更大的数字

[英]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. 我想使用C语言的printf和scanf来执行此操作。不幸的是,我编写的内容始终返回第二个值,而我一直想知道为什么。 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 . 您已经在.data section中用dd将数字( number1number2 )定义为DWORD ,但是使用cmp将它们引用为QWORD

So the result is quite unpredictable/depending on the memory layout. 因此,结果非常不可预测/取决于内存布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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