Skip to content

About

A modular, low-level implementation of the standard C `printf()` function written entirely in 32-bit ARM Assembly.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

ARM32 printf — Custom printf Subroutine in ARM Assembly

ARM Architecture Language Calling Convention Environment

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.


Features

  • 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.
  • Low-Level Software Interrupts: Uses ARM SWI traps (SWI 0 for character output, SWI 4 for integer rendering, SWI 2 for execution termination).

Supported Format Specifiers

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

Architecture & Implementation Details

Calling Convention (APCS)

  • 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 via LDMFD R13!, {R4, R5, R14} before returning with MOV PC, R14.

Register Allocation Internal Map

  • 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.

How to Run & Test

  1. Load any assembly file from tests/ (e.g., tests/main.s or tests/test_hex.s) into an ARM7 / 32-bit ARM Assembler & Simulator (such as Komodo or aasm).
  2. Assemble and run the binary.
  3. The simulator console will output the formatted strings using the native SWI software interrupt output handlers.

Example Code Snippet

    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

About

A modular, low-level implementation of the standard C `printf()` function written entirely in 32-bit ARM Assembly.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages