【问题标题】:Storing dword into address将双字存储到地址中
【发布时间】:2016-10-11 00:39:50
【问题描述】:

我正在自学汇编语言的某些部分,现在,我专注于将数据声明存储在地址中。

当谈到存储十六进制时,我知道如果我正在处理字节,例如;

1234

我可以这样存储它们:

Address 0000  -  12  
Address 0001  -  24

因为双字是 32 位,我假设每个会占用两倍的空间。

如果我以 dword 结尾:

54 00 87 D1 49 5A AF 56 32

它们会这样存储吗:

Address 0000  -  54  
Address 0002  -  00
Address 0004  -  87
Address 0006  -  D1
Address 0008  -  49
Address 000A  -  5A
Address 000C  -  AF
Address 000F  -  56
Address 0011  -  32

?

【问题讨论】:

  • 为什么DWORD例子有9个字节?无论如何,你的问题是什么,endianness
  • @MargaretBloom 我打算尝试用 0 填充其余部分(除非那不正确)。是的,如果我使用 little-endian,即使它是 32 位,程序是否相同? This site 暗示确实如此,而且似乎在说无论我在处理什么,步骤都是一样的。我想我正在寻找验证。
  • 使用 little-endian 存储多字节值(word、dword 等)时,经验法则是“低地址的低字节”,其中“低字节”表示最低有效字节。我对您的问题感到困惑,因为在第一个示例中,WORD 1234h 存储在 BE 中,第二个示例存储字节,它们没有字节序。
  • @MargaretBloom 嗯,它确实是 DWORD 540087D1495AAF5632h。不确定这是否会改变任何事情。
  • 但是540087D1495AAF5632h 不是 DWORD,DWORD 是 4 个字节。在十六进制格式中,您可以轻松地以字节为单位计算值的长度,每对数字都是一个字节,因此您的值以字节为单位 54_00_87_D1_49_5A_AF_56_32 = 9 个字节。 (即 2x DWORD + 1 BYTE)。

标签: assembly hex endianness dword


【解决方案1】:

正如已经指出的那样,您的值超过了一个双字。

在 x86 上,“字”是 16 位的,因为 8086 是 16 位微处理器。在这种情况下,它的意思是“两个字节”。 “双字”是两个字或四个字节,而“四字”是四个字或八个字节。 x86 是一个“小端”处理器,因此它从寄存器的小端开始写入内存。

如果你这样做(intel 语法和 gcc 样式的十六进制数字):

#Load the lowest 8 bits of the rax register (al) with 0x54
#This is loading a BYTE (1 byte)
mov   al,0x54
#Load the lowest 16 bits of the rbx register (bx) with 0x5400
#This is loading a WORD (2 bytes)
mov   bx,0x5400
#Load the lowest 32 bits of the rcx register (ecx) with 0x540087D1
#This is loading a DWORD (4 bytes)
mov   ecx,0x540087D1
#Load the entire 64 bit register rdx with 0x540087D1495AAF56
#This is loading a QWORD (8 bytes)
mov   rdx,0x540087D1495AAF56

然后,如果您要将这些移动到寄存器 rsi 中保存的地址,您会得到:

#Put the value of al (0x54) into address at [rsi+0]
mov   [rsi],al
#Put the value of bx (0x5400) starting at the address at rsi+0, 
# such that [rsi+0] will be 0x00 and [rsi+1] will be 0x54
mov   [rsi],bx
#Put the value of ecx (0x540087D1) starting at the address of rsi+0,
# such that [rsi+0] will be 0xD1, [rsi+1] will be 0x87, 
# [rsi+3] will be 0x00, and [rsi+4] will be 0x54
mov   [rsi],ecx
#Put the value of rdx (0x540087D1495AAF56) starting at the address of rsi+0,
#such that [rsi++0] will be 0x56, [rsi+1] will be 0xAF, 
# [rsi+2] will be 0x5A, [rsi+3] will be 0x49, 
# [rsi+4] will be 0xD1, [rsi+5] will be 0x87,
# [rsi+6] will be 0x00, and [rsi+7] will be 0x54
mov   [rsi],rdx  

您的示例值有 9 个字节,不适合任何寄存器,也不是机器类型。

所以你得到的双字 ram 看起来像:

0x540087D1 (小端,如x86):
第一个地址 - 0xD1
第二个地址 - 0x87
第三个地址 - 0x00
第四个地址——0x54

(大端,如 SPARC):
第一个地址 - 0x54
第二个地址 - 0x00
第三个地址 - 0x87
第四个地址——0xD1

我还要补充一点,在未来的汇编问题中,您应该始终讨论相关架构 - 几乎没有通用的汇编问题。

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2020-05-25
    • 2020-07-27
    相关资源
    最近更新 更多