;    s.PrintHex
;
;******************************************************************************
;
;    This file assembles to a linkable object exporting one procedure which
;    may be useful in several programs, and obeys the ARM procedure call
;    standard, so may be called not only from assembler but also from high
;    level languages. The object file is intended to be incorporated in an
;    example linkable library with the LibFile tool.
;
;    Points demonstrated here include:
;       - Use of the LibFile tool to construct linkable libraries
;       - Leaf procedures obeying the APCS
;
;    The procedure exported is print_hex, which prints in hexadecimal an
;    interger contained in r0. It has the C prototype:
;    void print_hex(int number);
;

; Use the GET directive to include a list of SWI names as if typed here

        GET     ^.AsmHdrs.h.SWINames

; Area name C$$code advisable if wanted to link with C output

        AREA    |C$$code|, CODE, READONLY

        EXPORT  |print_hex|

; APCS specifies input arguments in lowest registers - since only 1 here use r0
; r0 - r3 can be altered
; Since this is a leaf procedure (calls no others) it doesn't set up a stack frame

|print_hex|

        MOV     r3, lr                 ; store lr in r3 in case called in SVC
                                       ; mode, when SWI corrupts lr
        MOV     r2, r0
        MOV     r1, #32-4
loop_print
        MOV     r0, r2, LSR r1
        AND     r0, r0, #&F
        CMPS    r0, #10
        ADDCC   r0, r0, #"0"
        ADDCS   r0, r0, #"A"-10
        SWI     OS_WriteC
        SUBS    r1, r1, #4
        BPL     loop_print
 [ {CONFIG}=26
        MOVS    pc, r3
 |
        MOV     pc, r3
 ]

        END
