Write Your Own Operating System Tutorial (4)

zhaozj2021-02-08  248

Lesson 4: Hello, World

Now is the time you've all been waiting for. Finally we get to the classic "first" program. Every decent programming book has a "Hello, World" program, and now we know enough to make a "Hello, World" operating System. if you have done Some Experimenting On Your Own and Have Already Done this Lesson. We Will Create A Function To Print A String and Use It To Display Our message.

It will get tedious to print one character at a time to the screen, so we'll create a function to print a zero-terminated string to the screen. This is just a simple loop that prints all the character in a string one at a Time.

; ---------------------------------------------

PRINT A NULL-TERMINATED STRING ON THE Screen

; ---------------------------------------------

Putstr:

Lodsb; al = [DS: Si]

OR Al, Al; Set Zero Flag IF AL = 0

Jz PutStrd; Jump to Putstrd if Zero Flag is set

MOV AH, 0x0E; Video Function 0EH (Print Char)

MOV BX, 0x0007; Color

INT 0x10

JMP Putstr

Putstrd:

Retn

NOW, a little on how to use this function. First you have to load the address of the first character of the string into the register si. The SIMPLY CALL THIS SUBROUTINE PUTSTR.

You can create a string like this in your assembly.

MSG DB 'Hello, World!', 0

.................

MOV Si, MSG; Load Address of Message

Call Putstr; Print The Message

There is just one more thing that needs to be set up before this will work. The address msg, loaded into the register SI, is actually an offset off the beginning of the beginning of the segment that is pointed to by the register DS. So , before you can use the address msg, you must set up the current data segment. For now, we will use flat addressing from the bottom of physical RAM. to set the data segment to start from the bottom, set the DS register to zero . The following two instructions will do this.xor ax, ax; zero Out AX

MOV DS, AX; SET DATA Segment to Base of Ram

Try putting all of these parts together using the file h.asm from Lesson 3 as a starting point. Then, using the same method described in Lesson 3, assemble your file, copy it to your floppy disk and boot with it. Have fun. If You Get Stuck, You CAN Look at My Solution, Helowrd.asm, But it's no fair peeking unsil you've Tried!

Once You Have Finished, Proceed To The Next Lesson Where We Will Learn How To make ur operation system interactive.

转载请注明原文地址:https://www.9cbs.com/read-3161.html

New Post(0)