Reputation: 109
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}
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!
Upvotes: 4
Views: 7544
Reputation: 199
Why not write a C program do the same thing, and run
gcc -S file.c
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.
Upvotes: 1
Reputation: 28882
I think you are having problems with the scanf
part.
You need to supply scanf
(in r1) with the address of where the string input needs to go. The big of memory to obtain will be from the stack. Since the ARM ABI uses a full descending stack, you subtract the number of bytes you need from the current stack pointer, just make sure this is word aligned. You then can copy the new sp to r1 which will then get used in scanf.
You probably do not need the addr stuff in your data section. You might need to specify that your code goes into the text section and there is nothing stopping you putting all your read only data in the text section as well. This will really help if you are using PC relative addresses.
Hope that this helps.
Upvotes: 1