Skip to content

Instantly share code, notes, and snippets.

@qookei
Last active August 12, 2019 11:24
Show Gist options
  • Save qookei/2ae9144ee0e47b89fabc68715a37cd35 to your computer and use it in GitHub Desktop.
Save qookei/2ae9144ee0e47b89fabc68715a37cd35 to your computer and use it in GitHub Desktop.
bits 16
org 0x7C00
font_seg equ 0x1000 ; font at 0x1000:0x0000
jmp 0x0:.start
.start:
cli
cld
xor ax,ax
mov ss,ax
mov ds,ax
mov gs,ax
mov sp, 0x7C00
push ds
push font_seg
mov ax, 0x1130
mov bh, 6
xor di,di
int 0x10
push es
pop ds
pop es
mov si, bp
mov cx, 1024
rep movsd
pop ds
mov ax, 0x12
int 0x10
push 0xA000
push 0x1000
pop es
pop ds
mov ah, 0xF
call set_vga_mask
xor si, si
main_thingy:
mov cx, 26
mov dx, 20
mov di, 0x7C00
.loop:
call hex_dump_8
add di, 20
add dx, 16
dec cx
test cx, cx
jnz .loop
inc si
and si, 0x1F
jmp main_thingy
hlt
; 32 entry sinewave lookup table
sinewave_lookup_table:
db 0x80, 0x98, 0xb0, 0xc6, 0xda, 0xea, 0xf5, 0xfd
db 0xff, 0xfd, 0xf5, 0xea, 0xda, 0xc6, 0xb0, 0x98
db 0x80, 0x67, 0x4f, 0x39, 0x25, 0x15, 0xa, 0x2
db 0x0, 0x2, 0xa, 0x15, 0x25, 0x39, 0x4f, 0x67
; args
; bp - string
; ax - length
; bx - x (characters)
; dx - y (pixels)
; cx - putch masks
; si - sinewave divider
; di - initial x of sin(x/32)
put_wavy_string:
pusha
push bx
push cx
mov cx, di
mov bx, si
mov si, 0x8000
call gen_sinewave
pop cx
pop bx
call putstr
popa
ret
; args
; bx - x (characters)
; dx - y (pixels)
; cx - putch masks
; gs:si - y offsets for each character
; gs:bp - string
putstr:
pusha
push dx
.loop:
pop dx
push dx
movsx ax, byte [gs:si]
inc si
add dx, ax
mov al, [gs:bp]
test al, al
jz .end
xor ah, ah
call putch
inc bx
inc bp
jmp .loop
.end:
inc sp
inc sp
popa
ret
; args:
; ah - mask
set_vga_mask:
push dx
mov dx, 0x3C4
mov al, 2
out dx, ax
pop dx
ret
; args
; al - value
; gs:di - offset
; cx - len
fill_buffer:
push es
push gs
pop es
pop gs
rep stosb
push es
push gs
pop es
pop gs
ret
; args:
; ax - char
; bx - x (characters)
; dx - y (pixels)
; cl - xor mask
; ch - and mask
; assumes:
; ds = 0xA000
; es = segment pointing to the beginning of a font
putch:
pusha
push cx
shl ax, 4
push ax
push bx
mov ax, dx
mov bx, 80
mul bx
mov bp, ax
pop bx
add bp, bx
pop bx
mov cx, 16
pop dx
.loop:
mov al, byte [es:bx]
inc bx
and al, dh
xor al, dl
mov byte [ds:bp], al
add bp, 80
dec cx
test cx, cx
jnz .loop
popa
ret
; args
; gs:si - destination buffer
; ax - destination buffer len
; cx - initial x of sin(x/32)
; bl - divisor
gen_sinewave:
pusha
xchg cx, ax
mov di, sinewave_lookup_table
.loop:
push di
add di, ax
movzx bp, byte [cs:di]
inc ax
and ax, 0x1f
pop di
push ax
mov ax, bp
div bl
mov [gs:si], al
pop ax
inc si
dec cx
test cx, cx
jnz .loop
popa
ret
int_hex_tab:
db "0123456789ABCDEF"
; args
; al - number
; gs:di - destination
byte_to_hex_str:
pusha
inc di
mov bl, al
and al, 0xF
xor ah, ah
mov si, int_hex_tab
push si
add si, ax
mov al, [cs:si]
mov [gs:di], al
dec di
mov al, bl
shr al, 4
pop si
add si, ax
mov al, [cs:si]
mov [gs:di], al
popa
ret
; args
; ax - number
; gs:di - destination
short_to_hex_str:
push ax
inc di
inc di
call byte_to_hex_str
dec di
dec di
xchg ah, al
call byte_to_hex_str
pop ax
ret
; args
; dx - y (pixels)
; si - sinewave offset
; gs:di - address
; notes
; we put the dest str buffer at 0x0000:0x9000 (gs:0x9000)
hex_dump_8:
pusha
push si
push di
mov di, 0x9000
mov cx, 66
mov ax, ' '
call fill_buffer
mov [gs:0x904e], byte 0
pop di
push di
mov ax, di
mov di, 0x9000
call short_to_hex_str
pop si
mov cx, 20
add di, 4
mov [gs:di], byte ':'
inc di
inc di
.loop:
mov al, [gs:si]
call byte_to_hex_str
inc si
add di, 3
dec cx
test cx, cx
jnz .loop
mov bx, 7
mov si, 16
pop di
mov cx, 0xFF00
mov bp, 0x9000
mov ax, 67
call put_wavy_string
popa
ret
times 510 - ($-$$) db 0
dw 0xaa55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment