【问题标题】:assembly armv7 counting number of chars in a string汇编 armv7 计算字符串中的字符数
【发布时间】:2021-03-04 23:41:18
【问题描述】:

这个程序应该要求输入字符串并计算字符数 知道为什么我输入的任何内容都会得到 0 个字符。

例如,当我输入 hello 时, 输出是:“有 0 个字符:“你好”

这是我的代码:

.data

courseStr: .string "myName\t"

userInput: .string "\nThe string is:"   
    
countMessage: .string "There are %d charachters in:\"%s\".\n"



temp: .word 10

inputBuffer: .skip 15

inputValue: .string "%s"

.text

.global main


main:

    STMDB   SP!, {R4,LR} 

    LDR R0, =courseStr

    BL puts
    
    
    LDR R0, =userInput

    BL printf
    
    LDR R0, =inputValue

从用户那里获取输入

    LDR R1, =inputBuffer
    BL  scanf      

获取char函数

getLine:

    MOV R2,R0

    BL getchar

调用计数字符循环函数

    LDR R0,=inputBuffer
    BL  countCharactersLoop  // call the counter 

字符循环函数 countCharactersLoop: // 计数器

    LDRB    R0,[R1,R2]
    CMP     R0,#00        // if null then print   
    BEQ     countCharactersDone
    ADD     R1,R2, #01     // if not null add one
    B       countCharactersLoop // repeat

如果 char 为 null 那么我们打印结果 如果没有,我们继续计数

countCharactersDone:
// print "there are (numberOfChars) in the string inputed"
    LDR R2,=inputBuffer
    MOV R1,R0
    LDR R0, =countMessage
    BL printf
    LDMIA   SP!,{R4,LR}
    MOV     R0, R2
    BX      LR

【问题讨论】:

  • 你有什么问题?
  • edit您的问题添加一个实际问题。此外,将错误描述移到问题的正文中,并尽可能选择更好的标题。

标签: string assembly counter armv7


【解决方案1】:

一个问题是CMP R0,#00 将整个 R0 与零进行比较,而不是字节寄存器 R0。

另一个是你调用 CountCharacters 为:

LDR R0,=inputBuffer
BL  countCharactersLoop  // call the counter 

但将传递的字符串引用为:

LDRB R0,[R1,R2]

并增加 R1:

ADD R1,R2,#01 //如果不为空则加一

作为建议,在 C 中编写您想要的函数,验证它是否有效,然后将其编译为汇编输出(通常是 cc -S),遵循 C 编译器的引导,然后在它工作后对其进行改进。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-03
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    相关资源
    最近更新 更多