org 7c00h

;Address of video memory
VIDEO_START=0xB8000

;80 columns, 25 rows, 2 bytes per char
VIDEO_LENGTH=80*25*2

;End of video memory (first address of next memory block)
VIDEO_END=VIDEO_START+VIDEO_LENGTH

init:
    cli
    mov eax,VIDEO_END ;Make eax point to after the video memory
    mov bl,' '        ;The space character is used to clear the screen
    mov bp,text       ;Address of string (used in the printloop)

;Clear screen by writing spaces over the 80*25 field
space:
    sub eax,2           ;Subtracting 2 at a time to jump over the character attributes
    mov [eax], bl
    cmp eax,VIDEO_START ;Repeat until at video start
    jne space

;Value in EAX is now VIDEO_START because we went backwards
;We could go forwards as well, but since we want to write text,
;going backwards means we save a "mov eax,VIDEO_START" instruction

printloop:
    mov bl,[bp]     ;Copy character from our text to video memory
    mov [eax], bl   ;You cannot do "mov [mem],[mem]" directly,
                    ;Instead you need to do memory --> register --> memory

    add eax,2       ;Adding 2 instead of 1 skips over the attribute
    inc bp          ;Point to next character of our string

    cmp byte [bp],0 ;Check if memory is a zero and
    jnz printloop   ;continue if it's not

;Turn our space heater off
done:
    hlt
    jmp done

;String that is printed
text:
    db "Hello, World!",0

;Write this binary as a boot sector
bootsect:
    db (init+510-bootsect) dup 0F4h
    db 55h,0AAh
