在 X86 程序集中访问和移动字节

Accessing and moving bytes in X86 assembly

我有几个关于 X86 汇编中的内存和寄存器的问题:

  1. 我有一个字符串 "abcdefgh",寄存器 %eax 保存了一个指向该字符串的指针。现在我使用 movl (%eax), %edx 将字符串的前四个字节抓取到 %edx 中。它们如何存储在寄存器中? %dl寄存器中的字符d,还是字符a?

  2. 例如,当使用movb %eax, %dl时,它实际上移动了%eax的哪个字节? %al中的那个还是相反的那个?甚至可以这样做吗?或者我应该使用这样的指针 - movb (%eax), %dh - 获取指针指向的第一个字节?

假设您使用的是不寻常的 GAS 语法(源是第一个操作数,目标​​是第二个)而不是英特尔的:

How are they stored in the register? Is the character d in the %dl register, or is it the character a?

由于您在访问字符串时就好像它是一个 32 位数字一样,所以字节顺序适用。 x86 是 little-endian 所以你在最低地址得到最低有效字节,所以 DL 将保持 'a' (0x61),整个 EDX 将是 0x64636261.

When using movb %eax, %dl for example, which of %eax's bytes does it actually move? The one in %al or the opposite one? Is it even possible to do this?

这会产生语法错误,因为操作数的大小不同。你不能将 32 位移动到 8 位。

Or should I use a pointer like this - movb (%eax), %dh - to take the first byte the pointer points to?

如果您想访问 EAX 指向的数据而不是 EAX 本身,那么可以,那应该可以。