Calling C printf from ARM ASM

A while back I started to learn some ARM assembly on the Raspberry Pi. In my previous snippets, I was using SWI to do a system call to write to stdout, but it appears syscall 4 to write to stdout only writes Strings – if you have a byte value, this approach doesn’t work (without converting to a String first?)

From this question here, it appears you can call C functions like printf, which seems like cheating a bit, but I guess it gets the job done.

Here’s a quick snippet to add two numbers, and then print the result using printf with a string containing %d to substitute the result into the String:

[code]
.global main
.extern printf
main:
push {ip, lr}
mov r3, #1 /* move 1 to r3 */
mov r4, #2 /* move 2 to r4 */
add r4, r3, r4 /* r4 = r3 + r4 */
_ouput:
ldr r0,=output /* load address of output string */
mov r1, r4 /* move r4 result to r1 to include as param in string */
bl printf /* call printf */
_exit:
MOV R1, #0
MOV R7, #1
SWI 0
.data
output:
.asciz "Result: %d\n"
[/code]

Where previously I was compiling and linking in two steps with as and ld, it seems this doesn’t work if you are referencing C functions too, so for this example, compile with: ‘gcc add.s -o add’

This snippet and a few others I’ve started to collect I’ve shared in a github repo here: https://github.com/kevinhooke/learning-arm-asm

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.