如何判断寄存器内的ASCII字符是否等于64位臂组件中另一个寄存器内部的ASCII字符

发布于 2025-01-23 12:47:25 字数 1003 浏览 1 评论 0 原文

我正在尝试将字符串的第一个字母加载到寄存器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:

    // ... 

I'm attempting to load the first letter of a string into the register W2, then compare the value inside that register to the value inside of another register (W5). In the program below, the code refuses to branch even though both characters in each registers are equal. How can I get the code to branch.

     .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_蜘蛛 2025-01-30 12:47:25

ldr W5,= Charh 不会将字节'H'加载到W5中。相反,它将标签的地址的低32位加载到W5中。

如果您想获取存储在该地址的内存中的实际字符,则需要其他负载。 (并使用完整的64位地址开始。)

LDR X5, =charH
LDRB W5, [X5]

但是,ARM64具有可移动的指令,可以处理最多16位的任何值。因此,您可以完全删除 charh 仅为此而做

MOV W5, 'H'

,因为 cmp 也可以立即进行(最多12位),您可以完全跳过W5,然后加载后W2只是做:

CMP W2, 'H'
B.EQ equalsH

另一个注释, LDR X0,= String 并不是获得 String 地址的最佳方法。最好使用 adr adrp ,以便您具有独立于位置的代码。请参阅用于概述。但简而言之,有两个说明和没有字面的池数据,您既可以计算地址又可以完成负载。

ADRP X0, string
LDRB W2, [X0, #:lo12:string]

这并不像您的代码那样在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 label charH 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.)

LDR X5, =charH
LDRB W5, [X5]

However, ARM64 has move-immediate instructions that can handle any value up to 16 bits. Thus you can drop charH altogether and simply do

MOV W5, 'H'

For that matter, since CMP can also take an immediate (up to 12 bits), you could skip W5 altogether, and after loading W2 just do:

CMP W2, 'H'
B.EQ equalsH

On another note, LDR X0, =string isn't really the best way to get the address of string. It's better to use adr or adrp 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.

ADRP X0, string
LDRB W2, [X0, #:lo12:string]

This doesn't post-increment X0 as your code does, but your code never actually uses the incremented value anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文