Skip to content

Instantly share code, notes, and snippets.

@iProgramMC
Created March 19, 2019 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iProgramMC/61d8aee14020a8ee045d486b85ba4b24 to your computer and use it in GitHub Desktop.
Save iProgramMC/61d8aee14020a8ee045d486b85ba4b24 to your computer and use it in GitHub Desktop.
; MAIN.ASM
bits 16
jmp kernel_main
%include "screen.asm"
kernel_main:
cli
; Code goes here
call set_video_mode
mov dl, 0x03
call fill_screen
mov bx, 0x02
mov ch, 0x04
mov dh, 'X'
mov dl, 0x0f
call print_char
jmp $
; SCREEN.ASM
%define VGA_SEG 0xa000
%define VGA_MODE13_WIDTH 320
%define VGA_MODE13_HEIGHT 200
; Set video mode 0x13
set_video_mode:
mov ah, 0x00 ; Service #0
mov al, 0x13 ; Mode 0x13
int 0x10 ; Trigger the BIOS interrupt
ret
; Plot from AX - x, BX - y
; DL register - color
plot:
push dx
push cx
push bx
push ax
mov [0x7e00], dl ; Temporarily store the pixel value in RAM
mov cx, ax ; Make a backup of AX to be used to set the base VRAM address
mov ax, 0xa000 ; Store 0xA000 in the ES register
mov es, ax
mov ax, bx ; Get the Y value
mov dx, VGA_MODE13_WIDTH ; Multiply it by width (according to pos = y * width + x)
mul dx
add ax, cx ; and add the X to it
mov bx, ax
mov dl, [0x7e00] ; and restore it.
; Now we got the offset, let's draw the pixel
mov [es:bx], dl
pop ax
pop bx
pop cx
pop dx
ret
; Fills the screen with pixel from AL
fill_screen:
mov ax, VGA_MODE13_WIDTH
mov bx, VGA_MODE13_HEIGHT
fill_screen_a:
fill_screen_b:
call plot
dec ax
cmp ax, 0xffff
jne fill_screen_b
mov ax, VGA_MODE13_WIDTH
dec bx
cmp bx, 0xffff
jne fill_screen_a
ret
%include "font.asm"
%include "text.asm"
; FONT.ASM
ascii_font:
db 0x00, 0x00, 0x00, 0x00, 0x00
db 0x3E, 0x5B, 0x4F, 0x5B, 0x3E
db 0x3E, 0x6B, 0x4F, 0x6B, 0x3E
db 0x1C, 0x3E, 0x7C, 0x3E, 0x1C
db 0x18, 0x3C, 0x7E, 0x3C, 0x18
......
db 0x00, 0x00, 0x00, 0x00, 0x00
; TEXT.ASM
%define CHARACTER_WIDTH 5
%define CHARACTER_HEIGHT 7
plot_internal_pc:
push dx
push cx
push bx
push ax
mov ax, bx
mov bh, 0x00
mov bl, ch
;add bl, cls
mov dl, 0x07
call plot
pop ax
pop bx
pop cx
pop dx
ret
do_column:
; let's just do it hard coded this time
; it's gonna be a hassle but alright
mov [0x7e40], dl
and dl, 0x80
cmp dl, 0x00
je dont_do_pix7
mov cl, 0x07
call plot_internal_pc
dont_do_pix7:
mov dl, [0x7e40]
and dl, 0x40
cmp dl, 0x00
je dont_do_pix6
mov cl, 0x06
call plot_internal_pc
dont_do_pix6:
mov dl, [0x7e40]
and dl, 0x20
cmp dl, 0x00
je dont_do_pix5
mov cl, 0x05
call plot_internal_pc
dont_do_pix5:
mov dl, [0x7e40]
and dl, 0x10
cmp dl, 0x00
je dont_do_pix4
mov cl, 0x04
call plot_internal_pc
dont_do_pix4:
mov dl, [0x7e40]
and dl, 0x08
cmp dl, 0x00
je dont_do_pix3
mov cl, 0x03
call plot_internal_pc
dont_do_pix3:
mov dl, [0x7e40]
and dl, 0x04
cmp dl, 0x00
je dont_do_pix2
mov cl, 0x02
call plot_internal_pc
dont_do_pix2:
mov dl, [0x7e40]
and dl, 0x02
cmp dl, 0x00
je dont_do_pix1
mov cl, 0x01
call plot_internal_pc
dont_do_pix1:
mov dl, [0x7e40]
and dl, 0x01
cmp dl, 0x00
je dont_do_pix0
mov cl, 0x00
call plot_internal_pc
dont_do_pix0:
mov dl, [0x7e40]
ret
; -------------------------------------
; Prints a character from DH register.
; Internal! AX - character addressing
; BX - x position
; CH - y position
; DH - input char
print_char:
push dx
push cx
push bx
push ax
mov al, dh
mov cl, 0x05
mul cl
mov cl, 0x04
mov bx, ax
loop1:
push bx
add bl, cl
mov dl, [ascii_font + bx]
pop bx
call do_column
dec cl
cmp cl, 0x00
jne loop1
pop ax
pop bx
pop cx
pop dx
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment