从数据库1加载字节,但printf整数显示大量 [英] Loading a byte from a DB 1, but printf integer show a large number

查看:57
本文介绍了从数据库1加载字节,但printf整数显示大量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用printf来打印一个整数,但不能打印正确的值:

I'm trying to call printf to print an integer, put it doesn't print the right value:

section .data

         an:    db 1
         format: db "num: %d" , 10, 0

section .text
         global main
         extern printf

main:
         push ebp
         mov ebp,esp

         mov eax, [an]
         push eax
         push dword format
         call printf

         add esp, 8
         mov esp,ebp
         pop ebp

         mov eax, 0
         ret

此代码显示"num:1836412417"

this code prints "num: 1836412417"

当我尝试打印一个字符时就可以使用它!

put when I try to print a char it works!

section .data

         an:    db 'a'
         format: db "num: %c" , 10, 0

section .text
         global main
         extern printf

main:
         push ebp
         mov ebp,esp

         mov eax, [an]
         push eax
         push dword format
         call printf

         add esp, 8
         mov esp,ebp
         pop ebp

         mov eax, 0
         ret

现在它显示"num:a"

now it prints "num: a"

那么第一个代码怎么了?!

so what's wrong with the first code ?!!

推荐答案

db 声明8位(一个字节)的值,而%d 打印32位(x86上的四个字节)的值.

db declares 8-bit (one byte) values, while %d prints 32-bit (four byte) values on x86.

实际上,当将32位寄存器 eax mov eax一起加载[an] 时,您正在加载字母"num" 到寄存器的高字节.以后使用%d 将它们打印为数字,或者使用%c 忽略它们.

In effect, when loading 32-bit register eax with mov eax, [an] you are loading bits of letters "num" to high bytes of the register. They are later printed as number, when using %d or ignored when using %c.

要声明32位值,应使用 dd ,而不是 db .

To declare 32 bit values you should use dd, instead of db.

这篇关于从数据库1加载字节,但printf整数显示大量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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