A modular, low-level implementation of the standard C printf() function written entirely in 32-bit ARM Assembly. This project demonstrates variadic argument parsing from memory arrays, register management following the ARM Procedure Call Standard (APCS), string parsing, stack frame preservation, and dynamic integer-to-ASCII formatting with custom field width and zero-padding options.
- APCS Compliant: Arguments passed in registers (
R0= format string address,R1= arguments array pointer). Preserves callee-saved registers (R4–R8) via stack push/pop frames (STMFD/LDMFD). - Rich Format Specifier Support:
- String (
%s), Character (%c), and literal percent (%%). - Signed (
%d) and Unsigned (%u) 32-bit decimal integer conversion. - Custom field width (
%Nd,%Nu) with space padding. - Zero-padded field width (
%0Nd,%0Nu). - Uppercase 32-bit Hexadecimal (
%x) conversion.
- String (
- Low-Level Software Interrupts: Uses ARM SWI traps (
SWI 0for character output,SWI 4for integer rendering,SWI 2for execution termination).
| Specifier | Description | Argument Type | Example Specifier | Output Example |
|---|---|---|---|---|
%% |
Literal % character |
None | %% |
% |
%c |
ASCII Character | 32-bit Word (ASCII code) | %c |
A |
%s |
Null-terminated String | 32-bit Pointer | %s |
Hello World |
%u |
Unsigned Decimal Integer | 32-bit Unsigned Integer | %u |
4294967231 |
%Nu |
Unsigned Decimal with Width | 32-bit Unsigned Integer | %4u |
65 |
%0Nu |
Unsigned Decimal with Zero Pad | 32-bit Unsigned Integer | %04u |
0065 |
%d |
Signed Decimal Integer | 32-bit Signed Integer | %d |
-65 |
%Nd |
Signed Decimal with Width | 32-bit Signed Integer | %4d |
-65 |
%0Nd |
Signed Decimal with Zero Pad | 32-bit Signed Integer | %04d |
-065 |
%x |
Uppercase Hexadecimal | 32-bit Unsigned Integer | %x |
DECAFF |
R0: Pointer to the null-terminated format string in memory.R1: Pointer to the argument array containing 32-bit words corresponding to each conversion specifier.- Stack Frame: Stack pointer (
R13) initialized at high memory.STMFD R13!, {R4, R5, R14}pushes preserved working registers and link register (R14) upon entry, restored viaLDMFD R13!, {R4, R5, R14}before returning withMOV PC, R14.
R4: Current format string pointer (incremented as string is traversed).R5: Current argument array pointer (advanced by 4 bytes per argument consumed).R0: Temporary character accumulator / output byte register.R2: Field width parameter parsed from format string specifiers.
- Load any assembly file from
tests/(e.g.,tests/main.sortests/test_hex.s) into an ARM7 / 32-bit ARM Assembler & Simulator (such as Komodo oraasm). - Assemble and run the binary.
- The simulator console will output the formatted strings using the native
SWIsoftware interrupt output handlers.
ADR R0, format_str ; R0 = address of format string
ADR R1, arg_values ; R1 = address of argument array
BL printf ; Call printf subroutine
SWI 2 ; Exit execution
format_str:
DEFB "User ID: %04d, Key: 0x%x, Role: %s\n", 0
ALIGN
arg_values:
DEFW 42, 0xC0FFEE, role_str
role_str:
DEFB "Admin", 0