; Today's code runs in 16-bit mode. We will print "hello world!" onto the screen by calling BIOS.
;
; Give credits to http://3zanders.co.uk/2017/10/16/writing-a-bootloader/
; Comments partially added by Yu Wang
; -------------------------------------------------------------------

bits 16                 ; tell NASM this is 16 bit code
org 0x7c00              ; tell NASM to start outputting stuff at offset 0x7c00, 
                        ; Or to say, load the following code to memory address 0x7c00

boot:
    mov si, hello       ; point si register to hello label memory location
    mov ah, 0x0e        ; 0x0e means 'Write Character in TTY mode'

.loop:
    lodsb               ; load string [DS:SI]into AL
    
    ; check whether it's the end of the string (seeing an zero)
    or al,al            ; is al == 0 ?  Note that AL OR AL = AL. Simply to set the flags
    jz halt             ; if (al == 0) jump to halt if Zero Flag (ZF) is set
                        ; another way to write is 
                        ; CMP   AL, 0       ; compare AL
                        ; JE    HALT        ; Jump if Equal

    int 0x10            ; runs BIOS interrupt 0x10 - Video Services for printing the character in AL
    jmp .loop
halt:
    cli                 ; clear interrupt flag
    hlt                 ; halt execution

hello: db "Hello world!",0

times 510 - ($-$$) db 0 ; pad remaining 510 bytes with zeroes
                        ; the above line is equivalent to RESB 510 - ($-$$)
dw 0xaa55               ; magic bootloader magic - marks this 512 byte sector bootable for no reason! Checked by system.