如何将字符串复制到64位ARM组件中动态分配的新字符串中?

发布于 2025-01-25 20:27:54 字数 1526 浏览 1 评论 0原文

我将如何在下面的程序中动态分配数据,而不是静态分配。至少从我听到的内容中,您需要遵循3个步骤...

  1. malloc静态字符串
  2. 获取字符串的长度,然后将其传递给Malloc
  3. 将字符串复制到新的malloc'd字符串中。 (?)

如何完成步骤3?这是我到目前为止的东西...

.data 

str1:   .asciz "The Cat in the Hat\n"

data:   .quad 0

.global _start

.text

_start:

    // Static Version

    LDR X0,=str1;   // Points to the string "Cat in the Hat\n" 

    LDR X1,=data;   // Points to "Data"
    STR X0,[X1].    // Stores the string into "Data"


    // Dynamic Version: What I have so far...
    
    LDR X0,=str1;   // Points to the string "Cat in the Hat\n" 

    BL  strlength   // Returns the length of the string in X0. Modifies registers 
                    // X0, X1, X2, and X7 
    ADD X0,X0,#1    // +1 to the length to account for the null at the end 
    BL  malloc      // Passes X0 into malloc as the amount of bytes to be dynamically 
                    // allocated. Returns the Address of the block of memory allocated 
                    // in X0

    LDR X1,=str1    // Holds the string 

loop:

    LDRB    W5,[X1],#1  // Grabs the first byte from the string
    STRB    W5,[X0],#1  // Stores that byte into X0

    CMP     W5, #0      // Checks for Null
    B.EQ    endLoop     // Branches if Null
    
    B       loop        // Loops
    

    endLoop:       
          
                                        


    LDR X1,=data;   // Points to "Data"
    STR X0,[X1].    // Stores the string into "Data"


end:
    MOV X0, #0
    MOV X8, #93
    SVC 0

How would I dynamically allocate the data in the program below instead of statically. At least from what I've heard, you need to follow 3 steps...

  1. Malloc the static string
  2. Get the length of the string and pass it to malloc
  3. Copy the string into the new malloc'd string. (?)

How would I accomplish step 3? Here's what I have so far...

.data 

str1:   .asciz "The Cat in the Hat\n"

data:   .quad 0

.global _start

.text

_start:

    // Static Version

    LDR X0,=str1;   // Points to the string "Cat in the Hat\n" 

    LDR X1,=data;   // Points to "Data"
    STR X0,[X1].    // Stores the string into "Data"


    // Dynamic Version: What I have so far...
    
    LDR X0,=str1;   // Points to the string "Cat in the Hat\n" 

    BL  strlength   // Returns the length of the string in X0. Modifies registers 
                    // X0, X1, X2, and X7 
    ADD X0,X0,#1    // +1 to the length to account for the null at the end 
    BL  malloc      // Passes X0 into malloc as the amount of bytes to be dynamically 
                    // allocated. Returns the Address of the block of memory allocated 
                    // in X0

    LDR X1,=str1    // Holds the string 

loop:

    LDRB    W5,[X1],#1  // Grabs the first byte from the string
    STRB    W5,[X0],#1  // Stores that byte into X0

    CMP     W5, #0      // Checks for Null
    B.EQ    endLoop     // Branches if Null
    
    B       loop        // Loops
    

    endLoop:       
          
                                        


    LDR X1,=data;   // Points to "Data"
    STR X0,[X1].    // Stores the string into "Data"


end:
    MOV X0, #0
    MOV X8, #93
    SVC 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文