将较小的值移动到寄存器中 [英] Moving a value of a lesser size into a register
问题描述
我已经存储了 8
的一字节值,我想把它移到 rax
寄存器中.我目前正在使用 movzx
来对字节进行零扩展:
I have stored a one-byte value of 8
and I'd like to move that into the rax
register. I'm currently doing this with movzx
to zero-extend the byte:
.globl main
main:
push %rbp
mov %rsp, %rbp
movb $8, -1(%rbp)
movzx -1(%rbp), %rax <-- here
...
movzx
指令如何知道"-1(%rbp)
处的值只有一个字节长?从这里说,如果我正确阅读它,它可以在 byte
和 word
上工作,但它怎么知道?例如,如果我在 -2(%rbp)
处添加了一个两字节的值,它怎么知道要获取这个两字节的值?是否有另一条指令可以让我在地址处抓取 one
或 two
或 four
字节值并将其插入 64 位寄存器?
How does the movzx
instruction 'know' that the value at -1(%rbp)
is only one byte long? From here is says, if I'm reading it properly, that it can work on both a byte
and a word
, but how would it know? For example, if I added a two-byte value at -2(%rbp)
how would it know to grab the two-byte value? Is there another instruction where I can just grab a one
or two
or four
byte value at an address and insert it into a 64 bit register?
我想另一种方法是先将寄存器清零,然后将其添加到 8 位(或多位)组件中,例如:
I suppose another way to do it would be to first zero-out the register and then add it to the 8-bit (or however many bits) component, such as:
mov $0, %rax
mov -1(%rbp), %al
有没有一种方式比另一种方式更受欢迎?
Is there one way that is more preferred than another way?
推荐答案
movzx
指令如何知道"-1(%rbp)
处的值只有一个字节长?
How does the
movzx
instruction 'know' that the value at-1(%rbp)
is only one byte long?
有两个(甚至三个)指令:
There are two (or even three) instructions:
movzxb
(-1(%rbp)
是一个字节长)和 movzxw
(-1(%rbp)
是一个 16 位字长).
movzxb
(-1(%rbp)
is one byte long) and movzxw
(-1(%rbp)
is one 16-bit word long).
我的汇编程序将 movzx
解释为 movzxb
;但是,您不应该依赖它!
My assembler interprets movzx
as movzxb
; however, you should not rely on that!
最好使用包含源代码大小的指令名称(movzxb
或 movzxw
),以确保汇编程序使用正确的指令.
Better use the instruction name including the source size (movzxb
or movzxw
) to ensure that the assembler uses the correct instruction.
这篇关于将较小的值移动到寄存器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!