如何判断寄存器内的ASCII字符是否等于64位臂组件中另一个寄存器内部的ASCII字符
我正在尝试将字符串的第一个字母加载到寄存器W2中,然后将寄存器内部的值与另一个寄存器内部的值(W5)进行比较。在下面的程序中,即使每个寄存器中的两个字符都相等,代码也拒绝分支。如何将代码分支。
.data
string: .asciz "Hello" // Loads string the ascii character "Hello"
charH: .asciz "H" // Initializes charH with 'H'
.global _start // Provide program with starting address to linker
.text
_start:
LDR X0,=string // Loads the string "Hello" into X0
LDR W5,=charH // Loads the character "H" into W5
// Should Load the first letter of string into the W2 register and then post-increments the pointer to the next byte of the string
LDRB W2, [X0], #1
CMP W2, W5 // Compares W2 with W5
B.EQ equalsH // Branches if both equal
end:
MOV X0, #0 // Use 0 return code
MOV X8, #93 // Service command code 93 to terminate the program
SVC 0 // Call linux to terminate
.end
// The Goal is to get the condition to branch here! "equalsH"
equalsH:
// ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ldr W5,= Charh
不会将字节'H'
加载到W5中。相反,它将标签的地址的低32位加载到W5中。如果您想获取存储在该地址的内存中的实际字符,则需要其他负载。 (并使用完整的64位地址开始。)
但是,ARM64具有可移动的指令,可以处理最多16位的任何值。因此,您可以完全删除
charh
仅为此而做,因为
cmp
也可以立即进行(最多12位),您可以完全跳过W5,然后加载后W2只是做:另一个注释,
LDR X0,= String
并不是获得String
地址的最佳方法。最好使用adr
或adrp
,以便您具有独立于位置的代码。请参阅用于概述。但简而言之,有两个说明和没有字面的池数据,您既可以计算地址又可以完成负载。这并不像您的代码那样在INCREMENT X0后进行,但是您的代码实际上从未使用过增量值。
LDR W5,=charH
doesn't load the byte'H'
into W5. Rather, it loads the low 32 bits of the address of the labelcharH
into W5.If you wanted to get the actual character that is stored in memory at that address, you would need another load. (And use the full 64-bit address to begin with.)
However, ARM64 has move-immediate instructions that can handle any value up to 16 bits. Thus you can drop
charH
altogether and simply doFor that matter, since
CMP
can also take an immediate (up to 12 bits), you could skip W5 altogether, and after loading W2 just do:On another note,
LDR X0, =string
isn't really the best way to get the address ofstring
. It's better to useadr
oradrp
so that you have position-independent code. See Understanding ARM relocation (example: str x0, [tmp, #:lo12:zbi_paddr]) for an overview. But in brief, with two instructions and no literal pool data, you can both compute the address and do the load.This doesn't post-increment X0 as your code does, but your code never actually uses the incremented value anyway.