【问题标题】:Assembly Function adding strFrom to the end of StrTo装配函数将 strFrom 添加到 StrTo 的末尾
【发布时间】:2021-10-18 20:21:53
【问题描述】:

这是我目前正在使用的汇编代码,但我只获得了我尝试更改代码但无法得到正确答案的初始字符串:

.global stringCat

.text

stringCat:

stringCat_loop:
LDRB R2, [R1], #1
STRB R2, [R0], #1
CMP R2, #0
BNE stringCat_loop
BX LR

这是调用函数的 C 代码:

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

extern void stringCat(char* strFrom, char* strTo);

int main(void) {
char test3[20] = "ijkl";
char test4[44] = "mnop"
stringCat(test3, test4);

printf("Question 2, stringCat: Correct answer = mnopijkl\n");
printf("Question 2, stringCat: student answer = %s\n\n", test4);

return EXIT_SUCCESS;
}

My current outputs using my code

【问题讨论】:

  • 你需要在第一个字符串的末尾找到空字节,然后开始复制到那里。
  • 你实现了strcpy(),而不是strcat()
  • 看起来应该是stringCat(char* strTo, char* strFrom);,而不是stringCat(char* strFrom, char* strTo);(在您在strTo 中找到\0 之后)

标签: c assembly arm armv7


【解决方案1】:
stringCat_loop:
LDRB R2, [R1], #1
STRB R2, [R0], #1
CMP R2, #0
BNE stringCat_loop
BX LR

在您的代码中,R0 == test3R1 == test4 - 在您的函数结束时,我们希望将 test3 连接到 test4 - 即 R0R1 之后(顺便说一下strcat中的参数顺序相反)。

但是在你的循环中——你实际上是在做strcpy(R0, R1)——你遍历R1中的每个字节直到你到达一个空字节,然后将每个字节复制到R0中。


你应该做的是:

  1. 找到R1 的结尾以开始追加:

     loop_r1:
     LDRB R2, [R1], #1
     CMP R2, #0
     BNE loop_r1
    
  2. 像以前一样执行 strcpy - 但颠倒您对 R0R1 的使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2011-06-16
    • 1970-01-01
    • 2013-08-07
    相关资源
    最近更新 更多