ARM 程序集:从 STDIN 获取字符串 [英] ARM Assembly: Getting a string from STDIN
问题描述
我目前正在学习 CS 课程,我们刚刚开始在 Raspberry Pi 上使用 ARM Assembly.事实证明这相当困难,想知道是否有人可以提供帮助.我目前的任务是从标准输入中获取一个字符串(使用 scanf)并计算其中的字符数,然后返回该数字(所以基本上实现了我自己的 strlen).我有这个代码的基本想法:
Im currently in a CS course and we've just started working with ARM Assembly on the Raspberry Pi. It's proving to be decently difficult and was wondering if anyone could help. My current assignment is to take a string from stdin (Using scanf) and count the number of characters in it, then return that number (So basically implement my own strlen). I have the basic idea down with this code:
.section .rodata
promptWord:
.ascii "Enter a word: \000"
readWord:
.ascii "%s\000"
printLength:
.ascii "Word length is %d characters.\n\000"
.section .data
.align 2
.comm word,4,4
.text
addrword: .word word
addrPromptWord: .word promptWord
addrReadWord: .word readWord
addrPrintLength: .word printLength
.global main
/* s: r0 */
main:
stmfd sp!, {fp, lr} /* Save pc, lr, r4*/
ldr r0, addrPromptWord
bl printf
ldr r0, addrReadWord
ldr r1, addrword
bl scanf
ldr r0, addrword
ldr r0, [r0]
mov r1, #0
skip:
ldrb r2,[r0] /* r2 <- *a */
mov r3,#0
cmp r2,r3
beq endskip /* if (*a == 0) jump endskip */
mov r3,#1
add r0,r0,r3 /* a++ */
add r1, r1, r3 /* len++ */
bal skip /* go to skip */
endskip:
mov r0, r1 /* Return len */
ldmfd sp!, {fp, pc}
我假设问题出在代码的 .data 部分,因为(我假设)这不是对齐字符串的正确方法.任何帮助深表感谢.谢谢!
I'm assuming that the issue is with the .data section part of the code since (I'm assuming) that isnt the proper way to align a string. Any help is much appreciated. Thanks!
推荐答案
为什么不写一个C程序做同样的事情,然后运行
Why not write a C program do the same thing, and run
gcc -S file.c
你会在file.s(gcc生成的汇编代码)中看到编译器是如何处理的.即使你不理解file.s中的某些行,它也会引导你到arm汇编手册的正确位置.
That you will see how the compiler deal with it in file.s(assembly code generated by gcc).Even if you do not understand some lines in file.s, it would lead you to the right place of arm assembly manual.
这不是您问题的直接答案.但很遗憾我不能评论你的帖子,否则我会这样做.
This is not directly answer of your question. But sad I can not comment your post, otherwise I would do that.
这篇关于ARM 程序集:从 STDIN 获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!