为什么我的 NASM 代码在将 db 字节变量打印为 int 时打印出一个很大的值?

问题描述 投票:0回答:1

我正在用 nasm 编码,但我不明白发生了什么。 Linux 发行版是 Ubuntu 16 64 位,但 NASM 运行在 32 位。

预期输出 -->

"Number is: 2"

实际输出 -->

"number is: 134520868"

代码:

%include "io.inc"

section .data
    n1 db 2 ; i know it's a bad practice to define a db variable, it's just for a test
    msg: db 'number is: %d',10,0

section .text
extern printf
global main

main:
    push ebp
    mov ebp, esp

    push dword n1
    push msg
    call printf

    mov esp, ebp
    pop ebp
    ret

我尝试用

dd
定义n1,或者推送
n1
的内容,即使使用像
eax
这样的寄存器。

更新,即使我这样做了

push dword [n1]
,唯一改变的是现在输出是“数字是:1836404226”...

linux assembly x86 printf nasm
1个回答
2
投票

您想要一个 32 位数字,因此使用

dd
并且您不想打印变量的地址,而是打印其内容。所以:

n1 dd 2 ; ...

push dword [n1]
push msg
call printf
© www.soinside.com 2019 - 2024. All rights reserved.