; org: all instructions from now on start in 7c00h ; 7c00h is where the bios loads the 1st sector ; assembler formatting: ; column 0: labels ; tab: commands org 7c00h ; at boot: real mode (like it's 8086) ; we have to tell assembler that code is 16bit mode bits 16 start: mov sp, 7c00h ; initialize segment registers mov ax, 0 mov ds, ax mov es, ax mov ss, ax ; save dx value mov [saved_drive_num], dl ; service 0: set videomode ah ; param: which video mode (13h: 320x200, 256 colors) al ; ax: ah (high 8 bits), al (low) mov ax, 13h ; calling video bios ; software interrupt 10h int 10h mov ax, 3 call clearscreen ; load 2nd sector from boot device and jump to it ; bios helpers, 13h = disk io mov ax, 0 mov ds, ax mov ah, 02h ; call 2: read sectors into memory mov al, 1 ; number of sectors to read mov ch, 0 ; low 8 bits of cylinder number mov cl, 2 ; sector number that starts from 1 mov dh, 0 ; head number mov dl, [saved_drive_num] ; 8bits mov bx, sector_2 int 13h ; error check: if carry flag isn't set jump to loaded code jnc sector_2 .inf_loop: jmp .inf_loop clearscreen: ; video ram is mapped in a0000 ; first 64000 bytes appear immediately on screen ; mem addresses in real mode have 2 parts: segment and offset ; bits overlap: segment is shifted by 4 and is added to the ; overlapping offset (20 bits number = x86's addressable space = 1MB) ; segment register for the segment: es, ds, cs, ss (and: fs, gs) ; default register = ds (data segment), es = extra segment ; cs = code segment cs:ip (it points where to read instructions and is ; automatically set to 7c0 (7c00h) ; offset can be paired with any register ; pair the registers push ax mov ax, 0a000h mov ds, ax mov di, 0 pop ax ; counter cx mov cx, 64000 .loop_begin: ; dereferrence[] address mov [di], al inc di dec cx ; when cx is 0, 0 flag is set jnz .loop_begin ret saved_drive_num: db 0 ; declare byte 0 ; assembler trick: write as many 0 needed to fill 510 bytes ; $ <- means here times 510-($-$$) db 0 dw 0aa55h sector_2: mov ax, 5 call clearscreen .inf_loop: jmp .inf_loop