1 ; org: all instructions from now on start in 7c00h
2 ; 7c00h is where the bios loads the 1st sector
3 ; assembler formatting:
7 ; at boot: real mode (like it's 8086)
8 ; we have to tell assembler that code is 16bit mode
12 %macro SETPAL 4 ; 4 = num arguments of macro (index rgb)
13 mov dx, 3c8h ; index register
21 mov al, %4 >> 2 ; shift to give the value from 0 - 255
28 ; initialize segment registers
35 ; bios sets in dl the num of the drive that
36 ; loaded you so if you overwrite dl you can reuse it
37 mov [saved_drive_num], dl
39 ; service 0: set videomode ah
40 ; param: which video mode (13h: 320x200, 256 colors) al
41 ; ax: ah (high 8 bits), al (low)
42 ; call 0 = set_videomode it expects the video mode in al
43 ; ah : 0, al: 13, ax:ahal
47 ; software interrupt 10h
48 ; what's the value of ah? al=video mode when ah=0
49 ; which set which video mode
54 ; load 2nd sector from boot device and jump to it
55 ; bios helpers, 13h = disk io
58 mov ah, 02h ; call 2: read sectors into memory
59 mov al, 3 ; number of sectors to read
60 mov ch, 0 ; low 8 bits of cylinder number
61 mov cl, 2 ; sector number that starts from 1
62 mov dh, 0 ; head number
63 mov dl, [saved_drive_num] ; 8bits
66 ; error check: if carry flag isn't set jump to loaded code
73 ; video ram is mapped in a0000
74 ; first 64000 bytes appear immediately on screen
75 ; mem addresses in real mode have 2 parts: segment and offset
76 ; bits overlap: segment is shifted by 4 and is added to the
77 ; overlapping offset (20 bits number = x86's addressable space = 1MB)
78 ; segment register for the segment: es, ds, cs, ss (and: fs, gs)
79 ; default register = ds (data segment), es = extra segment
80 ; cs = code segment cs:ip (it points where to read instructions and is
81 ; automatically set to 7c0 (7c00h)
82 ; offset can be paired with any register
93 ; dereferrence[] address
97 ; when cx is 0, 0 flag is set
104 ; assembler trick: write as many 0 needed to fill 510 bytes
106 times 510-($-$$) db 0
111 mov ax, 0a000h ; video segment points to video memory
113 mov bx, 0 ; video offset
119 test ax, 0ff00h ; lower 8 bits 0, highest 1, Z flag is set while < 256, test = and but doesnt change ax
123 mov [bx], al ; pixel written on screen
132 ; setup grayscale palette (64 first colors because ramdac uses 6 bits / color so 2^6)
133 ; ram dac (digital to analog converter) tis vga
134 ; in a ^ register which index we want to write
135 ; 3 writes in another ramdac register (data)
136 ; ramdac feature: when you need to set a palette
137 mov al, 0 ; first index
138 mov dx, 3c8h ; ramdac index registe
139 out dx, al ; because io port (address) > 255
140 inc dx ; ramdac data register: 3c9h
146 test al, 3fh ; test with 3fh to keep the lowest 6 bits of al
149 ; setup lovebug palette
150 SETPAL 64, 0, 255, 0 ; chr(64) = @
151 SETPAL 65, 0, 0, 0 ; A
152 SETPAL 66, 50, 50, 50 ; B
153 SETPAL 67, 57, 4, 4 ; C
154 SETPAL 68, 108, 9, 9 ; D
155 SETPAL 69, 164, 4, 4 ; E
157 ; disable all interrupts (for bios to not read the keyboard)
160 ; ds on 0 because rand uses it for accesses to mem
164 shr ax, 4 ; to use it as segment! (shift right)
167 ; draw to back buffer
168 xor bx, bx ; mov bx, 0 to clear the register
171 and ax, 3fh ; last six bits of eax (see rand)
172 mov [es:bx], al ; [] -> bx offset and default segment ds we change it to es
174 cmp bx, 64000 ; num pixels
178 mov di, (200 - 32) * 320 + 160 - 16
186 and ax, 3 ; random value in 0, 15
187 sub ax, 1 ; random value in -3, 12
196 and al, 8 ; the 4th bit is 1 when we are in the vertical blanking period
199 ; copy backbuffer to video memory
203 mov ds, ax ; ds points to backbuffer now (was in es)
207 xor di, di ; write in es:di
208 mov ecx, 16000 ; 16000 double words (dwords) = 64000 bytes
213 inc word [num_frames]
214 cmp word [num_frames], 200 - 32
217 in al, 64h ; 60h = keyb data port, 64h = keyb status port
218 and al, 1 ; 1 = OUTBUF_FULL = the keyb controller out buf is full
219 jz main_loop ; no key pressed, loop back
220 in al, 60h ; reads the keyb that was pressed to reset the flag
223 ; re-enable all interrupts
227 mov ax, 3 ; text mode
240 ; reads from ds:si writes to es:di (bug top left corner)
241 ; knows how many pixels on screen and how many in the lovebug
243 mov cx, 1024 ; 32 x 32 lovebug
253 dec cx ; dec the num_pixels we want to write
254 jz .end ; all pixels have been written
255 test cx, 1fh ; keep 5 least significant bits, 3 most significant bits 0
256 jnz .loop ; when this is 0 we are in a multiple of 32
257 add di, 320 - 32 ; screen width - bug width
276 ; converts digits to ascii - print
287 div cx ; ax contains cx / 10
305 ; random number generator
316 randmul dd 1103515245
320 str_frames db 'frames: ', 0
321 str_newline db 13, 10, 0 ; crlf carriage return line feed newline
323 ; db to write pixels transparent 255
331 db '@@@@BAAAB@@@@@BAAB@@@@@BAAAB@@@@'
332 db '@@@@@@@@AAAB@BAAAAB@BAAA@@@@@@@@'
333 db '@@@@@@@@@@@AAAAAAAAAA@@@@@@@@@@@'
334 db '@@@@@@@@@@@@AAAAAAAA@@@@@@@@@@@@'
335 db '@@@@@@@@@@@@AAAAAAAA@@@@@@@@@@@@'
336 db '@@@@@@@@@@@@BAAAAAAB@@@@@@@@@@@@'
337 db '@@@@@@@@@@@@@AAACAA@@@@@@@@@@@@@'
338 db '@@@@@@@@@@CDDEECDEEDDC@@@@@@@@@@'
339 db '@@@@@@@CDDEEEEEDCEEEEEDDC@@@@@@@'
340 db '@@@@@CDEEEEDCDECDEDCDEEEEDC@@@@@'
341 db '@@@@CDDCDEECACEDCECACEEDCDDC@@@@'
342 db '@@@CDECACEEDCDECDEDCDEECACEDC@@@'
343 db '@@@DEEDCDEEEEEEDCEEEEEEDCDEED@@@'
344 db '@@CEEEEEEEEEEEDCDDEEEEEEEEEEEC@@'
345 db '@@DEDCDEEEDCDECACCEDCDEEEDCDED@@'
346 db '@@EECACEEECACEDCDDECACEEECACEE@@'
347 db '@CEEDCDEEEDCDEEDCEEDCDEEEDCDEEC@'
348 db '@DEEEEEEEEEEEEDCCDEEEEEEEEEEEED@'
349 db 'CEEEEEEDCDEEEDCBACDEEEDCDEEEEEEC'
350 db 'CEDCDEECACEEDCAABACDEECACEEDCDEC'
351 db 'DECACEEDCDEDCAABAAACDEDCDEECACED'
352 db 'DEDCDEEEEEDCAAAABAAACDEEEEEDCDED'
353 db 'CEEEEEEEEECAAAABAAAAACEEEEEEEEEC'
354 db 'CEEEEEEEEDAAAAAABAAAAADEEEEEEEEC'
355 db '@DEDCDEEDCAAAAABAAAAAACDEEDCDED@'
356 db '@DECACEDCAAAAAAABAAAAAACDECACED@'
357 db '@DEDCDDCAAAAAAABAAAAAAAACDDCDED@'
358 db '@CEEEECAAAAAAAAABAAAAAAAACEEEEC@'
359 db '@@EEED@AAAAAAAABAAAAAAAAA@DEEE@@'
360 db '@@EEDC@@AAAAAAAABAAAAAAA@@CDEE@@'
361 db '@@DEC@@@@@AAAAABAAAAAA@@@@@CED@@'
362 db '@@@C@@@@@@@@AAAABAAA@@@@@@@@C@@@'
364 align 16 ; if the address is not a multiple of 16 add some 0 to become one (because we'll use backbuffer as a segment)