NASM x86 使用 extern printf 打印整数 [英] NASM x86 print integer using extern printf

查看:86
本文介绍了NASM x86 使用 extern printf 打印整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 x86 程序集中使用 printf 打印一个整数.对于格式字符串 printf(fmtstring, vals),我将 %d 存储为 fmtd.然后我将 1 放入 ax,2 放入 bx,将它们相加并想使用 call printf 打印结果.这是代码.

I try to print an integer using printf in x86 assembly. For the format string printf(fmtstring, vals) i have stored the %d as fmtd. Then i put 1 into ax, 2 into bx, add them and want to print the result using call printf. Here is the code.

global _main

extern _printf

section .data
    fmtd db "%d"

section .text

_main:
    push ebp
    mov ebp, esp

_begin:
    mov ax, 1
    mov bx, 2
    add ax, bx
    push ax
    push fmtd
    call _printf
    add esp, 8

_end:
    mov esp, ebp
    pop ebp
    ret

但我明白了

-10485757

而不是预期

3

你能帮我看看有什么问题吗?

can you help me whats wrong with it?

我刚写的时候

push 3
push fmtd
call _printf

它照常工作并打印 3.

it works as usual and prints 3.

谢谢

推荐答案

您需要使用完整的 32 位寄存器:

You need to use the full 32 bit registers:

你想要这个:

mov eax, 1
mov ebx, 2
add eax, ebx
push eax
push fmtd
call _printf

输出说明 -10485757 你得到:

-10485757 十六进制为 FF600003.0003 来自 push ax,它推送 eax 的 16 个低位.FF60 是堆栈中剩余的任何东西.

-10485757 in hexadecimal is FF600003. The 0003 comes from the push ax which pushes the 16 low bits of eax. The FF60 is whatever leftover was on the stack.

阅读这篇 SO 文章,了解有关axeax 之间的关系.

Reads this SO article for detailed explanation about the relationship between ax and eax.

这篇关于NASM x86 使用 extern printf 打印整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆