gpt4 book ai didi

linux - assembly 变量赋值

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:55 24 4
gpt4 key购买 nike

我正在阅读 Jeff Duntemann 撰写的《使用 Linux 进行汇编语言逐步编程》。

书中他提到了代码

EatMsg: db "Eat at Joe's",10
mov ecx,EatMsg

复制的不是 EatMsg 的内容,而是存储内容的内存。这是理解的,我已经通过 edb 确认了这一点。但是声明:

EatMsg: dw "Eat at Joe's",10

当我在调试器中检查此代码时,它与使用 db 指令时的内存地址相同,这也可以理解,但我知道 dw 指的是“定义字”(2 个字节)。

但是有人能告诉我在执行期间实际发生了什么吗 CPU 一次读取一个字,因为 dw 指令?我正在尝试将其形象化。

这本书让我感到困惑,因为他提到通用寄存器将保存定义字符串的地址,然后我阅读了以下摘录并感到困惑:

WordString: dw 'CQ'
DoubleString: dd 'Stop'

The DW directive defines a word-length variable, and a word (16 bits) may hold two 8-bit characters. Similarly, the DD directive defines a double word (32-bit) variable, which may hold four 8-bit characters. The different handling comes in when you load these named strings into registers. Consider these two instructions:

mov ax,WordString
mov edx,DoubleString

In the first MOV instruction, the characters ‘‘CQ’’ are placed into register AX, with the ‘‘C’’ in AL and the ‘‘Q’’ in AH. In the second MOV instruction, the four characters ‘‘Stop’’ are loaded into EDX in little-endian order, with the ‘‘S’’ in the lowest-order byte of EDX, the ‘‘t’’ in the second-lowest byte, and so on. This sort of thing is a lot less common (and less useful) than using DB to define character strings, and you won’t find yourself doing it very often. Because eatsyscall.asm does not incorporate any uninitialized data, I’ll hold off discussing such definitions until we look at the next example program.

CPU 如何执行此操作以及使用 db 指令对 dw 指令的好处是什么?

提前致谢

最佳答案

你使用哪个汇编器?在 nasm dw 与字符串只会将字符串填充到两个字节的边界。

正在读取的两个字节和四个字节之间的差异与 dbdw 无关,而是使用 ax (两个字节寄存器)与 edx(一个四字节寄存器)。

加载两个字节的寄存器将导致读取两个字节的内存。例如,如果您改为从使用 dd 声明的数据加载 dx,您将获得一个由四个字节中的两个字节填充的寄存器。

总而言之,dddbdw 和 friend 只是让汇编程序生成您拥有的某些数据的正确二进制表示心里。最后它只是一个地址的二进制数据,如果你只有 db 就可以解决这个问题,这只会让你更多地考虑布局。

关于linux - assembly 变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30145100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com