Disk Operating System/Hello, World!

From Wikiversity
Jump to navigation Jump to search

Objective[edit | edit source]

Hello, World![edit | edit source]

Going Deeper[edit | edit source]

So far we have a working bootloader that just hangs the computer. Obviously, you'd anticipate it to do more than that and it can. By using a BIOS interrupt, we can display a single ASCII character on the screen. Specifically, we'll be using Int 10h, which is the BIOS interrupt that interacts with the screen. There is a list of int 10h functions on Wikipedia if you need further reference.

The code below prints a ASCII character on the screen and hang the computer.

	org 0x7C00

	; Prints a uppercase A.
	mov ah, 0x0E
	mov al, 'A'
	mov bh, 0x00
	int 10h

	; This hangs the computer.
	cli
	hlt

	; Fills our bootloader with zero padding.
	times 510-($-$$) db 0

	; This is our boot sector signature, without it,
	; the BIOS will not load our bootloader into memory.
	dw 0xAA55

In the above code, ah contains the BIOS function, al contains the character to print which, in this case, is A, and bh contains the the active page number (you don't need to worry about that right now, just keep it set to zero).

Now that we have a way to print characters onto the screen we can make a string printing function. We'll have to setup some things up and change things around for this to work, though.

	org 0x7C00

	; Prints a string
	mov si, string
	call print_string

	; Set direction flag.
	cld

	; This hangs the computer.
	cli
	hlt


print_string:
	; In SI = String.
	; Out = Nothing.

	; Setup int 10h parameters.
	mov ah, 0x0E
	mov bh, 0x00

.loop:
	; Load a byte from SI into AL
	; and then increase SI.
	lodsb

	; If AL contains a null-terminating
	; character, then stop printing.
	cmp al, 0x00
	je .done

	int 10h

	jmp .loop

.done:
	; Return control to the caller.
	ret


string: db "Hello, world!", 0


	; Fills our bootloader with zero padding.
	times 510-($-$$) db 0

	; This is our boot sector signature, without it,
	; the BIOS will not load our bootloader into memory.
	dw 0xAA55

Whoa, that's a lot of new code! Let's break it down. cld clears the direction flag, which only matters to our lodsb. We need to move string in si and we need to call print_string. In the function, print_string, we setup the int 10h parameters and then we go into a loop. lodsb grabs a byte from si and puts it into al. Then depending on the direction flag, know as the DF; lodsb will increase or decrease si. cld clears the DF, which makes lodsb increase si, hence why it's in our code. The print_string function also checks for a null-terminating character and if it finds it, it returns control to the caller. The label string points to the string we want to print. It might sounds a bit complicated, but it really just complex.

Assignments[edit | edit source]

Completion status: About halfway there. You may help to clarify and expand it.