Reputation: 11
I'm playing around with some assembler on the RaspberryPi, calling functions from C library, printf and scanf. No problem with reading and printing characters, using %c, or decimals, using %d, but I'd like to see how one reads in a string of characters? For example, the following code:
.data
27 .balign 4
26 prompt: .asciz "What is your name? "
25 .balign 4
24 message: .asciz "Hello %s\n"
23 .balign 4
22 pattern: .asciz "%s"
21 .balign 4
20 name: .asciz " "
19 .balign 4
18 lr_bu: .word 0
17
16 /* ------------------------------------------------------------ */
15 /* CODE Section */
14 /* ------------------------------------------------------------ */
13 .global main
12 .func main
11
10 main:
9 @ Store the link register
8 ldr r1, =lr_bu
7 str lr, [r1]
6
5 @ display prompt
4 ldr r0, =prompt
3 bl printf
2
1 @ set up parameters for scanf to get input
39 ldr r0, =pattern
1 ldr r1, =name
2 bl scanf
3
4 @ print the message with name
5 ldr r0, =message
6 ldr r1, =name
7 ldr r1, [r1]
8 bl printf
9
10 @ restore the link register
11 ldr lr, =lr_bu
12 ldr lr, [lr]
13 bx lr
14
15 exit:
16 mov r7, #1 @ exit gracefully
17 swi 0
This produces a segmentation fault so obviously this is not the way to do it! Searched through dozens of articles and pages but all focus on decimal input and output rather than whole strings. Also written the C code and looked at the assembled listing but it wasn't a great deal of help ... to me.
Does it need to be read back out in a loop character by character?
I'd be grateful for any pointers to steer me in the right direction. I'm new to StackExchange so hopefully I've provided enough information and in the correct format but do advise if not.
Thanks
Upvotes: 0
Views: 1403
Reputation: 11
That fixed it, thanks Jester! I made the following changes:
name: .asciz " "
and removed:
ldr r1, [r1]
from the output section and magic happened! (well, I know it's not magic but ...)
Upvotes: 1