rdos/kernel/kprintf.asm

141 lines
2.1 KiB
NASM
Raw Normal View History

2019-03-10 20:23:09 +01:00
; important functions in this file: kprintf
; write a character to kernel output
; in: al
kputc:
2019-03-13 21:11:49 +01:00
push ax
push bx
mov bx, 0x0000
mov ah, 0x0e
int 0x10
pop bx
2019-03-10 20:23:09 +01:00
pop ax
ret
; write a string to kernel output
; in: ds:di
kputs:
push ax
.loop:
lodsb
cmp al,0x00
je .end
call kputc
jmp .loop
.end:
pop ax
ret
; prints a nibble in hex
; in: al
kprint4:
and al, 0x0F
add al, 0x30
cmp al, 0x3a
jl kputc
add al, 0x07
jmp kputc
; print a byte
; in: al
kprint8:
push ax ; avoid destroying ah
aam 16 ; high nibble moved into ah
xchg ah,al ; high nibble first
call kprint4
xchg ah,al
call kprint4
pop ax
ret
; print a word
; in: ax
kprint16:
xchg ah,al
call kprint8
xchg ah,al
call kprint8
ret
; read a word from the stack, using bp as stack pointer
; in: bp
; out: ax, bp
kprintf_lodsw:
push ds ; work segment for lodsw
push si ; work pointer for lodsw
push ss ; data transfer to ds
pop ds
mov si,bp
lodsw
mov bp,si ; write back incremented value
pop si
pop ds
ret
; print data from stack
; in: ds:si, ss:sp
kprintf:
push ax
push bp ; state variable for stack lodsw
push si ; return original pointer to caller
mov ax, sp
add ax, 0x08
mov bp, ax
.loop:
lodsb
cmp al,0x00
je .end
cmp al,0x25 ; '%'
je .fseq
call kputc
jmp .loop
.end:
pop si
pop bp
mov al,0x0A
call kputc
mov al,0x0D
call kputc
pop ax
ret
.fseq:
lodsb
cmp al,0x00
je .end
cmp al,0x25 ; '%'
je .fmt_pc
cmp al,0x63 ; 'c'
je .fmt_c
cmp al,0x73 ; 's'
je .fmt_s
cmp al,0x78 ; 'x'
je .fmt_x
cmp al,0x58 ; 'X'
je .fmt_X
mov al,0x3F ; '?'
call kputc
jmp .loop
.fmt_pc:
mov al,0x25
call kputc
jmp .loop
.fmt_c:
call kprintf_lodsw
call kputc
jmp .loop
.fmt_s:
push si
call kprintf_lodsw
mov si,ax
call kputs
pop si
jmp .loop
.fmt_x:
call kprintf_lodsw
call kprint8
jmp .loop
.fmt_X:
call kprintf_lodsw
call kprint16
jmp .loop