Skip to content

Instantly share code, notes, and snippets.

@hikiko
Created September 29, 2019 19:02
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 hikiko/7f0a7ad462d8213c49bd12829021ff87 to your computer and use it in GitHub Desktop.
Save hikiko/7f0a7ad462d8213c49bd12829021ff87 to your computer and use it in GitHub Desktop.
Asm/DOS: setting custom palette
; vi:filetype=nasm:
bits 16
; DOS loads COM programs at offset 100h (256) of the program segment
; therefore we need to let the assembler know, that everything should
; start from address 100h
org 100h
; video bios (10h) call 00h: set video mode
; ah: 00h, al: video mode number
mov ax, 13h ; mode 13h (320x200 8bpp)
int 10h
; video memory 64000 bytes at a0000h (linear address)
; in seg:off notation, we can use seg: a000, offs 0 - 64000
mov ax, 0a000h
mov es, ax ; we'll use es, to allow us to call rep stosd/movsd later
xor di, di ; initialize offset to 0
; setup palette
xor cl, cl
palloop:
mov al, cl ; palette index
mov ah, cl
not ah
mov bl, ah
mov bh, cl
call setpal
inc cl
jnz palloop ; when cl overflows back to 0, stop looping
xor cx, cx ; init y to 0
yloop: xor bx, bx ; init x to 0
xloop: mov ax, bx
cmp ax, 256
jb skipclear
xor ax, ax
skipclear:
mov [es:di], al
inc di
inc bx
cmp bx, 320
jnz xloop ; as long as ax is < 320, jump to xloop
inc cx
cmp cx, 200
jnz yloop ; as long as cx is < 200, jump to yloop
; read the keyboard controller, and wait for ESC
kbwait:
in al, 60h ; read scancode (if available)
dec al ; esc == 1, so dec sets zero flag if escape is pressed
jnz kbwait ; if zero flag is not set, jump back to kbwait
; change back to text mode (mode 3)
mov ax, 3
int 10h
; DOS terminate syscall
mov ax, 4c00h
int 21h
DAC_INDEX equ 3c8h
DAC_DATA equ 3c9h
; set palette function
; al: palette index
; ah: red
; bl: green
; bh: blue
setpal:
mov dx, DAC_INDEX
out dx, al
inc dx ; dx = DAC_DATA
mov al, ah
shr al, 2 ; 8bits -> 6bits color
out dx, al
mov al, bl
shr al, 2
out dx, al
mov al, bh
shr al, 2
out dx, al
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment