问题描述
我正在尝试编写一个函数,该函数确定作为第一个参数给出的字符串的长度,因此%rdi将包含char * ptr.当我调用movb (%rdi),%rcx
将%rdi指向的字符移动到%rcx时,出现以下错误:incorrect register '%rdx' used with 'b' suffix
据我了解,只有某些寄存器可以在x86-64中保存一个字节,那么我可以使用哪一个将字节移入?还是我用来提取字符串中每个字节字符的方法不正确?
I am trying to write a function that determines the length of a string given as the first argument, so %rdi will contain char *ptr. When I call movb (%rdi),%rcx
to move the character pointed to by %rdi into %rcx, I get the following error:incorrect register '%rdx' used with 'b' suffix
As I understand it, only certain registers can hold a byte in x86-64, so which ones can I use to move the byte into? Or is the method I am using to extract the character at each byte in the string incorrect?
推荐答案
所有通用寄存器具有低8位,分别可寻址为al
,bl
,cl
,dl
,sil
,dil
,bpl
,spl
,r8b
到r15b
(英特尔文档使用后缀l
).另外,还有一些8..15位也可寻址,即ah
,bh
,ch
和dh
.
All the general-purpose registers have the low 8 bits separately addressable as al
, bl
, cl
, dl
, sil
, dil
, bpl
, spl
, r8b
through r15b
(intel documentation uses l
suffix). In addition, a few have bits 8..15 also addressable, namely ah
, bh
, ch
and dh
.
因此,如果您只想加载一个字节,则可以使用上述方法之一.另外,您可以使用零扩展或符号扩展来加宽字节数据,例如在movzbl (%rdi), %ecx
的情况下(读取:将零扩展字节移动到long ).请注意,对32位寄存器进行操作会将父级" 64位寄存器的高32位清零,但是对8位或16位子寄存器进行操作将使其余位保持不变.
So if you only want to load a byte, you can use one of the above. Alternatively, you can use zero- or sign-extension to widen the byte data, for example in your case movzbl (%rdi), %ecx
(read: move zero extended byte to long). Note that operating on 32 bit registers zeroes the top 32 bits of the "parent" 64 bit register, but operating on 8 or 16 bit sub-registers leaves the rest of the bits unchanged.
我觉得您应该(重新)阅读intel手册的基本体系结构部分.
I feel you should probably (re-)read the basic architecture section of the intel manuals.
这篇关于在将字节存储到寄存器x86-64程序集中时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!