Tuesday 4 September 2012

Still no RPi :(

But I have been working through some of the tutorials in the previous post, and noticed that some people were writing output to stdout using the C library's puts and some using printf (with, of course, printf being especially useful if you want dynamic output).

But while the following program:

.globl main
.align 2
.section .rodata
hello:
    .asciz "hello"
.text
main:
    ldr r0, =hello
    bl puts
    mov r7, #1
    swi 0

compiles(assembles?) and runs fine, substituting printf for puts causes no output. I posted the query to the Raspberry Pi Bare Metal forum and got an immediate answer: use \n at the end of the hello string othewise it won't flush the buffer to stdout. A followup post explained that this program exits using syscall 1, which frees up the unix resources, but not C stdio. A proper return from main is required for a program using the C library:

.globl main
.align 2
.section .rodata
hello:
    .asciz "hello"
.text
main:
    stmfd sp!, {lr}
    ldr r0, =hello
    bl printf
    mov r3, #0
    mov r0, r3
    ldmfd sp!, {lr}

(and WHOA! gotta work on getting code samples to markup better than that!)

No comments:

Post a Comment