如何将字符串复制到64位ARM组件中动态分配的新字符串中?
我将如何在下面的程序中动态分配数据,而不是静态分配。至少从我听到的内容中,您需要遵循3个步骤...
- malloc静态字符串
- 获取字符串的长度,然后将其传递给Malloc
- 将字符串复制到新的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...
- Malloc the static string
- Get the length of the string and pass it to malloc
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论