rdos/lib/printf.asm
2020-03-01 12:03:35 +00:00

105 lines
1.5 KiB
NASM

printf:
cld
pop si
push bp
mov bp, sp
.loop:
mov al, [cs:si]
inc si
cmp al, 0x00
je .end
cmp al, 0x25
je .handle_25h
.literal:
call putc
jmp .loop
.end:
pop bp
push si
ret
.handle_25h:
mov al, [cs:si]
inc si
cmp al, 0x25
je .literal
cmp al, 0x58 ; 'X'
je .printhex
cmp al, 0x55 ; 'U'
je .printdec
cmp al, 0x53 ; 'S'
je .printstr
mov al, 0x3F
jmp .literal
.printhex:
add bp, 2
mov ax, [bp]
mov bx, 0x0010
call print_number
jmp .loop
.printdec:
add bp, 2
mov ax, [bp]
mov bx, 0x000A
call print_number
jmp .loop
.printstr:
add bp, 2
mov ax, [bp]
call print_string
jmp .loop
; converts a integer to ascii
; in ax input integer
; bx base
; cx minimum number of digits
; out bx garbled
; dx garbled
print_number_padded:
xor dx, dx
div bx
push dx
dec cx
jz .nopad
call print_number_padded
jmp print_number.end
.nopad:
call print_number
jmp print_number.end
; converts a integer to ascii
; in ax input integer
; bx base
; out bx garbled
; dx garbled
print_number:
xor dx, dx
div bx ; ax = dx:ax / 10, dx = dx:ax % 10
push dx
and ax, ax
jz .end
call print_number
.end:
pop bx
xor bh, bh
add bx, print_chars
mov al, [cs:bx]
call putc
ret
; putc's a string
; in DS:AX null-terminated string
print_string:
push si
mov si, ax
.loop:
lodsb
cmp al, 0x00
je .end
call putc
jmp .loop
.end:
pop si
ret
print_chars:
db "0123456789ABCDEF"