intfmt 的含义:db "%d", 10, 0 in assembly [英] Meaning of intfmt: db "%d", 10, 0 in assembly

查看:20
本文介绍了intfmt 的含义:db "%d", 10, 0 in assembly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在我的一个汇编文件的顶部看到了这个,并意识到我在打印整数的过程中花了很长时间使用它,却没有真正意识到它最初来自哪里(在我的基本汇编模板中使用)或 10, 0 结尾的意思是:

I recently saw this at the top of one of my assembly files and realised I had spent ages using it in the process of printing integers without actually realising where it came from originally (used in my basic assembly template) or what the 10, 0 on the end means:

section .data
    intfmt: db "%d", 10, 0

谁能分解一下并解释不同的组成部分,特别是第 2 行的组成部分?

Could anyone break this down and explain the different components, specifically of line 2?

我现在正在尝试使用 scanf 读取输入,但此格式似乎无法正常工作.想我是时候了解这些数字的含义了!

I am now trying to read input using scanf and it seems this format isn't working correctly for this. Think it's about time I learned what the numbers meant!

此外,如果您对使用 scanf 读取 64 位架构上的输入有任何想法,我以前从未这样做过,这给我带来了一些问题,因此也将不胜感激!

Also, if you have any ideas about reading in input on a 64bit architecture using scanf, I haven't done it before and it's causing me some problems so any pointers to doing that would also be appreciated!

推荐答案

intfmt: 是一个标签——它可以是任何字符串,但其他代码引用它.

intfmt: is a label -- it could be any string, but other code refers to it.

db 是定义字节"伪指令.它不是组装机器代码指令,而是将原始字节转储到代码流中(在本例中为 .data 部分).

db is the "define bytes" pseudo-instruction. Instead of assembling a machine code instruction, it dumps raw bytes into the code stream (.data section in this case).

"%d", 10, 0 是要转储到流中的字节.第一个是 ascii 字符串,它转储两个字节(字符 '%' 和 'd'),10 是换行符(C 中的 ),以及0 是空字节.

"%d", 10, 0 are the bytes to dump into the stream. The first is an ascii string, which dumps two bytes (the characters '%' and 'd'), 10 is a newline character ( in C), and 0 is a null byte.

请注意,该字符串是一个原始字符串——不是以 nul 结尾的,并且不支持任何 C 风格的转义(例如 用于换行).所以总的来说,这创建了一些等效于 C 字符串 "%d " 的东西,带有一个换行符和一个 NUL 终止符.

Note that the string is a raw string -- NOT nul terminated, and NOT supporting any C style escapes (like for newline). So overall this creates something equivalent to the C string "%d " with a newline and a NUL terminator.

现在至于为什么这适用于 printf 而不适用于 scanf(以及为什么在删除 10 后会切换)与 printf 和 scanf 的工作方式以及换行符对每个的含义有关.

Now as to why this works for printf and not for scanf (and why that switches if you remove the 10) has to do with how printf and scanf work and what a newline means to each.

在 printf 中,换行符打印一个换行符,然后(如果输出处于行缓冲模式,它可能是),刷新内部输出缓冲区,以便您可以实际看到结果.因此,当您移除 10 时,没有刷新,您也看不到输出.

In printf, the newline prints a newline and then (if the output is in line buffered mode, which it probably is), flushes the internal output buffer so you can actually see the result. So when you remove the 10, there's no flush and you don't see the output.

在 scanf 中,换行符的意思是读取并丢弃字符,直到遇到非空白字符",将该字符作为下一个要读取的字符.因此,如果您在终端输入上执行 scanf(例如),换行符将导致它阻塞,直到您在数字后输入非空行...

In scanf, the newline means "read and throw away character until you reach a NON-whitespace character", leaving that character as the next to be read. So if you're doing scanf on a terminal input (for example), the newline will cause it to block until you enter a non-blank line after the number...

这篇关于intfmt 的含义:db "%d", 10, 0 in assembly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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