[英]assembly armv7 counting number of chars in a string
該程序應該要求輸入字符串並計算字符數 知道為什么我輸入的任何字符都為 0。
例如,當我輸入 hello 時,輸出是:“有 0 個字符:“hello”
這是我的代碼:
.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
Getchar 函數
getLine:
MOV R2,R0
BL getchar
調用計數字符loop函數
LDR R0,=inputBuffer
BL countCharactersLoop // call the counter
charactersloop 函數 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 為空,則打印結果,否則我們繼續計數
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
一個問題是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 編譯器的指導,然后在它起作用后對其進行改進。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.