Jason Xu 9c5c2813a0 Update QEMU command in all README and Makefile преди 2 години
..
Makefile 426205e2a3 Added makefiles for LLVM Clang преди 5 години
Makefile.clang 9c5c2813a0 Update QEMU command in all README and Makefile преди 2 години
Makefile.gcc 9c5c2813a0 Update QEMU command in all README and Makefile преди 2 години
OLVASSEL.md 9c5c2813a0 Update QEMU command in all README and Makefile преди 2 години
README.md 9c5c2813a0 Update QEMU command in all README and Makefile преди 2 години
gpio.h 823040a6bf New tutorials преди 6 години
kernel8.img 8ce0f75b47 Recompiled with latest gcc преди 4 години
link.ld 823040a6bf New tutorials преди 6 години
main.c 823040a6bf New tutorials преди 6 години
mbox.c 214885df63 compute the mailbox cmd only once преди 6 години
mbox.h 823040a6bf New tutorials преди 6 години
sprintf.c 823040a6bf New tutorials преди 6 години
sprintf.h 823040a6bf New tutorials преди 6 години
start.S ea4691947c Improve comments wrt stack setup преди 3 години
uart.c c59ad439f4 Enable UART0 FIFOs преди 3 години
uart.h 823040a6bf New tutorials преди 6 години

README.md

Tutorial 12 - Printf

Before we can improve our exception handler, we are going to need some functions very well known from the C library. Since we are programming bare metal, we don't have libc, therefore we have to implement printf() on our own.

$ qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial stdio
Hello World!
This is character 'A', a hex number: 7FFF and in decimal: 32767
Padding test: '00007FFF', '    -123'

Sprintf.h, sprintf.c

The interesting part. We heavily rely on our compiler's features to handle variable length argument list. As usual in these tutorials, it's not a fully featured, but rather a bare minimum implementation. Supports '%s', '%c', '%d' and '%x'. Padding is limited, only right alignment with leading zeros for hex and spaces for decimal.

sprintf(dst, fmt, ...) same as printf, but stores result in a string

vsprintf(dst, fmt, va) a variant that receives an argument list parameter instead of a variable length list of arguments.

Uart.h, uart.c

printf(fmt, ...) the good old C library function. Uses the sprintf function above and then outputs the string in the same way as uart_puts() did. Since we have '%x', uart_hex() became unnecessary, therefore removed.

Start

Although we are not going to use floats and doubles, gcc built-ins might. So we have to enable the FPU coprocessor to avoid "undefined instruction" exceptions. Also, in a lack of a proper exception handler, we have a dummy exc_handler stub this time.

Main

We test our printf implementation.