step 1: boot from floppy and clear screen
[bootboot] / bb.asm
1 ; org: all instructions from now on start in 7c00h
2 ; 7c00h is where the bios loads the 1st sector
3 ; assembler formatting:
4 ; column 0: labels
5 ; tab: commands
6         org 7c00h
7 ; at boot: real mode (like it's 8086)
8 ; we have to tell assembler that code is 16bit mode
9         bits 16
10
11 start:
12         mov sp, 7c00h
13
14 ; service 0: set videomode ah
15 ; param: which video mode (13h: 320x200, 256 colors) al
16 ; ax: ah (high 8 bits), al (low)
17         mov ax, 13h
18
19 ; calling video bios
20 ; software interrupt 10h
21         int 10h
22         call clearscreen
23
24 ; infinite loop
25 end:
26         jmp end
27
28 clearscreen:
29 ; video ram is mapped in a0000
30 ; first 64000 bytes appear immediately on screen
31 ; mem addresses in real mode have 2 parts: segment and offset
32 ; bits overlap: segment is shifted by 4 and is added to the
33 ; overlapping offset (20 bits number = x86's addressable space = 1MB)
34 ; segment register for the segment: es, ds, cs, ss (and: fs, gs)
35 ; default register = ds (data segment), es = extra segment
36 ; cs = code segment cs:ip (it points where to read instructions and is
37 ; automatically set to 7c0 (7c00h)
38 ; offset can be paired with any register
39 ; pair the registers
40         mov ax, 0a000h
41         mov ds, ax
42         mov di, 0
43 ; counter cx
44         mov cx, 64000
45         mov ax, 3
46 .loop_begin:
47 ; dereferrence[] address
48         mov [di], al
49         inc     di
50         dec cx
51 ; when cx is 0, 0 flag is set
52         jnz .loop_begin
53         ret
54
55 ; assembler trick: write as many 0 needed to fill 510 bytes
56 ; $ <- means here
57         times 510-($-$$) db 0
58         dw 0aa55h