move string functions to video.nasm

caught a bug where SetTextMode was setting mono text not 16-color mode
This commit is contained in:
2025-09-05 10:33:03 -04:00
parent d824493ba2
commit c3871d2b7d
2 changed files with 101 additions and 97 deletions

View File

@@ -24,12 +24,14 @@ SetTextMode:
pushf pushf
.func: .func:
xor ah, ah ; Set Video mode BIOS function xor ah, ah ; Set Video mode BIOS function
mov al, 0x02 ; 16 color 80x25 Text mode mov al, 0x03 ; 16 color 80x25 Text mode
int 0x10 ; Call video interrupt int 0x10 ; Call video interrupt
; TODO: check for carry and clear carry before call
mov ah, 0x05 ; Select active display page BIOS function mov ah, 0x05 ; Select active display page BIOS function
xor al, al ; page 0 xor al, al ; page 0
int 0x10 ; call video interrupt int 0x10 ; call video interrupt
; TODO: check for carry and clear carry before call
.endp: .endp:
popf popf
__CDECL16_EXIT __CDECL16_EXIT
@@ -51,5 +53,102 @@ disable_cursor:
__CDECL16_EXIT __CDECL16_EXIT
ret ret
; Prints a C-Style string (null terminated) using BIOS vga teletype call
; void PrintString(char* buf)
ALIGN 4, db 0x90
PrintString:
__CDECL16_ENTRY
mov di, [bp + 4] ; first arg is char[]
.str_len:
xor cx, cx ; ECX = 0
not cx ; ECX = -1 == 0xFFFF
xor ax, ax ; search for al = 0x0
cld
repne scasb ; deincrement cx while searching for al
not cx ; the inverse of a neg number = abs(x) - 1
dec cx ; CX contains the length of the string - nul byte at end
.print:
mov si, [bp + 4] ; source string
.print_L0:
movzx ax, byte [si]
push ax
call PrintCharacter
add sp, 0x2
inc si
dec cx
jcxz PrintString.endp
jmp PrintString.print_L0 ; Fetch next character from string
.endp:
__CDECL16_EXIT
ret ; Return from procedure
; Prints a single character
; void PrintCharacter(char c);
ALIGN 4, db 0x90
PrintCharacter:
__CDECL16_ENTRY
.func:
movzx ax, byte [bp + 4] ; AL = character c
mov ah, 0x0E ; INT 0x10, AH=0x0E call
mov bx, 0x0007 ; BH = page no. BL =Text attribute 0x07 is lightgrey font on black background
int 0x10 ; call video interrupt
; TODO: check for carry and clear carry before call
.endp:
__CDECL16_EXIT
ret
; TODO: fix the prolog, epilog and stack usage to confirm with cdecl16
; prints the hex representation of of val
; void PrintDWORD(uint32_t val);
ALIGN 4, db 0x90
PrintDWORD:
__CDECL16_ENTRY
.func:
mov si, IntToHex_table
mov ebx, 16 ; base-16
mov eax, dword [bp + 4] ;val
xor edx, edx
xor cx, cx
.next_digit:
div ebx ; dividend in edx:eax -> quotient in eax, remainder in edx
push dx ; save remainder
inc cx
xor dx, dx
test eax, eax
jnz PrintDWORD.next_digit
.zero_pad:
cmp cx, 0x0008
je PrintDWORD.print_stack
xor ax, ax
push ax
inc cx
jmp PrintDWORD.zero_pad
.print_stack:
pop bx
dec cx
push cx
movzx ax, byte [bx+si+0] ; bx = index into Hex lookup table
push ax
call PrintCharacter
add sp, 0x2
pop cx
jcxz PrintDWORD.endp
jmp PrintDWORD.print_stack
.endp:
__CDECL16_EXIT
ret
%endif %endif
%define __INC_VIDEO %define __INC_VIDEO

View File

@@ -191,102 +191,7 @@ hcf:
; ;
; ############################## ; ##############################
; Prints a C-Style string (null terminated) using BIOS vga teletype call
; void PrintString(char* buf)
ALIGN 4, db 0x90
PrintString:
__CDECL16_ENTRY
mov di, [bp + 4] ; first arg is char[]
.str_len:
xor cx, cx ; ECX = 0
not cx ; ECX = -1 == 0xFFFF
xor ax, ax ; search for al = 0x0
cld
repne scasb ; deincrement cx while searching for al
not cx ; the inverse of a neg number = abs(x) - 1
dec cx ; CX contains the length of the string - nul byte at end
.print:
mov si, [bp + 4] ; source string
.print_L0:
movzx ax, byte [si]
push ax
call PrintCharacter
add sp, 0x2
inc si
dec cx
jcxz PrintString.endp
jmp PrintString.print_L0 ; Fetch next character from string
.endp:
__CDECL16_EXIT
ret ; Return from procedure
; Prints a single character
; void PrintCharacter(char c);
ALIGN 4, db 0x90
PrintCharacter:
__CDECL16_ENTRY
.func:
movzx ax, byte [bp + 4] ; AL = character c
mov ah, 0x0E ; INT 0x10, AH=0x0E call
mov bx, 0x0007 ; BH = page no. BL =Text attribute 0x07 is lightgrey font on black background
int 0x10 ; call video interrupt
.endp:
__CDECL16_EXIT
ret
; TODO: fix the prolog, epilog and stack usage to confirm with cdecl16
; prints the hex representation of of val
; void PrintDWORD(uint32_t val);
ALIGN 4, db 0x90
PrintDWORD:
__CDECL16_ENTRY
.func:
mov si, IntToHex_table
mov ebx, 16 ; base-16
mov eax, dword [bp + 4] ;val
xor edx, edx
xor cx, cx
.next_digit:
div ebx ; dividend in edx:eax -> quotient in eax, remainder in edx
push dx ; save remainder
inc cx
xor dx, dx
test eax, eax
jnz PrintDWORD.next_digit
.zero_pad:
cmp cx, 0x0008
je PrintDWORD.print_stack
xor ax, ax
push ax
inc cx
jmp PrintDWORD.zero_pad
.print_stack:
pop bx
dec cx
push cx
movzx ax, byte [bx+si+0] ; bx = index into Hex lookup table
push ax
call PrintCharacter
add sp, 0x2
pop cx
jcxz PrintDWORD.endp
jmp PrintDWORD.print_stack
.endp:
__CDECL16_EXIT
ret
; ############################## ; ##############################
; ;