I am trying to print a string char by char, with this ARM32 code:
.global main
.type main%function
@ r0 = asciz c
@ r1 = singlechar
@ r2 = string
@ r3 = offset
main:
mov r3,#0 // initialize offset
ldr r0,=single_c
ldr r2,=string
push {ip,lr} // save the lr
loop:
ldrb r1,[r2,r3] // load 1 byte of the address string+offset
cmp r1,#0 // if the char is the null char
beq end // then go to the end
bl printf // else printf("%c",r1)
add r3,#1 // increase offset
b loop // repeat the loop
end:
pop {ip,lr} // restore lr
bx lr // return
string:
.asciz "Test\n"
single_c:
.asciz "%c\n"
I really don't get what am I doing wrong, since if I execute it, I get this:
$ /a.out
T
Segmentation Fault
So it only prints the first letter.
EDIT: I added a push {r0,r1,r2,r3} before the printf call and a pop {r0,r1,r2,r3} after the printf call, and now it works... but my question is: does printf ALWAYS modify the first four register?