x86_fat12bs/src/print16.lib

237 lines
2.9 KiB
Plaintext

; global position for vram
vb_pos: dw 0
;;;;;;;;;;;;;;;;;;;;;
;print a 64-bit integer
; input: push u64* (seg, offset)
;;;;;;;;;;;;;;;;;;;;;;
print_u64:
push bp
mov bp, sp
push eax
push es
push di
; es points to the target seg
mov ax, word [ss:bp + 2 * 2]
mov es, ax
; di points to the target offset
mov di, word [ss:bp + 3 * 2]
mov eax, dword [es:di+4]
push eax
call print_u32
add sp, 4
mov eax, dword [es:di]
push eax
call print_u32
add sp, 4
pop di
pop es
pop eax
pop bp
ret
;;;;;;;;;;;;;;;;;;;;;
;print a 32-bit integer
; input: push u32
;;;;;;;;;;;;;;;;;;;;;;
print_u32:
push bp
mov bp, sp
push ax
; print high 16 bits first
mov ax, word [ss:bp + 2 * 2 + 2]
push ax
call print_u16
add sp, 2
; print low 16 bits
mov ax, word [ss:bp + 2 * 2]
push ax
call print_u16
add sp, 2
pop ax
pop bp
ret
;;;;;;;;;;;;;;;;;;;;;
;print a char in
;;;;;;;;;;;;;;;;;;;;;;
print_char:
; input: al = char
push di
push ax
push bx
push gs
push es
; dx -> stores char
mov dx, ax
; gs points to vram
mov bx,0b800h
mov gs,bx
; es points to current seg
mov bx, cs
mov es, bx
; di holds vb_pos
mov di, word [es:vb_pos]
; gs points to vram
mov ax,0b800h
mov gs,ax
mov ax, dx
; handle return character
cmp al, 0xA
jne .char
mov ax, di
mov bl,2 * 80 ; bl = the # of bytes per line
div bl ; ax = di / bl
and ax, 0xFF ; only keep al
inc ax ; ax now has the next row
; calculate the new pos
mul bl
mov di,ax
jmp .end
.char:
mov dh, 0xF
mov word [gs:di], dx
add di, 2
.end:
mov word [es:vb_pos], di
pop es
pop gs
pop bx
pop ax
pop di
ret
;;;;;;;;;;;;;;;;;;;;;
;print a null-terminated string
;;;;;;;;;;;;;;;;;;;;;;
print_str:
;(seg, offset)
push bp
mov bp, sp
push si
push ax
push ds
; ds points to target seg
mov ax, word [ss:bp + 2 * 2]
mov ds, ax
; si holds target offset
mov si, word [ss:bp + 3 * 2]
.loop_each_char:
mov al, byte [ds:si]
test al, al
je .end
call print_char
inc si
jmp .loop_each_char
.end:
pop ds
pop ax
pop si
pop bp
ret
;;;;;;;;;;;;;;;;;;;;;
;print a uint8
;;;;;;;;;;;;;;;;;;;;;;
print_u8:
; push u16 that contains the u8
push bp
mov bp, sp
push ax
push bx
; es points to current segment for referencing vb_pos
mov ax, cs
mov es, ax
; bl holds uint8
mov bx, word [ss:bp + 2 * 2]
; print the high 4 bits first
mov al, bl
shr al, 4
call al_to_char
call print_char
; print the low 4 bits
mov al, bl
and al, 0xF
call al_to_char
call print_char
pop bx
pop ax
pop bp
ret
;;;;;;;;;;;;;;;;;;;;;
; helper function converting 4 bit val in al to its character
;;;;;;;;;;;;;;;;;;;;;;
al_to_char:
; no parameters
cmp al,09h
jna .l2
add al,37h
jmp .end
.l2:
add al,30h
.end:
ret
;;;;;;;;;;;;;;;;;;;;;;
;print a u16
;;;;;;;;;;;;;;;;;;;;;;
print_u16:
;Entrance: push u16
push bp
mov bp, sp
push ax
; print high 8 bits
mov ax, word [ss:bp + 2 * 2]
shr ax, 8
push ax
call print_u8
add sp, 2
; print low 8 bits
mov ax, word [ss:bp + 2 * 2]
and ax, 0xFF
push ax
call print_u8
add sp, 2
pop ax
pop bp
ret