; $Id: kprfmt.mac 1184 2019-07-10 20:39:44Z mueller $
; SPDX-License-Identifier: GPL-3.0-or-later
; Copyright 2013- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
;
; synchronous (polling) console print: very simple printf
; Call:
;   jsr pc, kprfmt
; Arguments:
;   r0: pointer format string
;   r1: pointer to argument list
; 
; r2-r5 registers preserved
;  

kprfmt: mov     r2,-(sp)        ; save r2

        mov     r0,r2           ; r2 now ptr to fmt string

1$:     movb    (r2)+,r0        ; next fmt char
        beq     20$             ; if zero quit
        cmpb    #'%,r0          ; is it '%' ?
        bne     10$             ; if not, print
        movb    (r2)+,r0        ; if yes, get next
        beq     20$             ; if zero quit

        cmpb    #'s,r0          ; %s found ?
        bne     2$
        mov     (r1)+,r0        ; get next arg
        jsr     pc,kprstr       ; and print string
        br      1$              ; go for next fmt char

2$:     cmpb    #'o,r0          ; %o found ?
        bne     3$
        mov     (r1)+,r0        ; get next arg
        jsr     pc,kproct       ; and print octal number
        br      1$              ; go for next fmt char

3$:     cmpb    #'d,r0          ; %d found
        bne     4$
        mov     (r1)+,r0        ; get next arg
        jsr     pc,kprdec       ; and print as decimal number
        br      1$

4$:     movb    -2(r2),r0       ; was neither %s,%o,%d
        jsr     pc,kprchr       ; so simply print these two letters...
        movb    -1(r2),r0

10$:    jsr     pc,kprchr       ; print fmt char
        br      1$              ; go for next fmt char

20$:    mov     (sp)+,r2        ; restore r2
        rts     pc

