Write Your Own Operating System Tutorial(4)

原创 2003年01月21日 09:06:00

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, then you may want to skip 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.

; ---------------------------------------------<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

; Print a null-terminated string on the screen

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


     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




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.  Then simply call this subroutine putstr.

You can create a string like this in your assembly file.

msg  db 'Hello, World!', 0

The zero on the end terminates the string.  Then you can print the string to the screen using the following instructions.

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, helowrld.asm, but it’s no fair peeking until you’ve tried!

Once you have finished, proceed to the next lesson where we will learn how to make our operating system interactive.

Write Your Own Operating System Tutorial(中文版)

Lesson 4: Hello, World你等待已久的时刻终于来临。写我们“第一个”程序已经是时候了。每一本像样的程序设计书籍都会有一个“Hello, World”程序,现在我们了解的足够多,可以写...
  • Fnoopy
  • Fnoopy
  • 2003年07月30日 08:17
  • 974

Write Your Own Operating System Tutorial(3)

Lesson 3: NASMIn this lesson we will learn to use an assembler to write our programs.  In previous l...
  • tangl_99
  • tangl_99
  • 2003年01月21日 09:06
  • 2146

Write Your Own Operating System Tutorial(6)

Lesson 6: Boot LoaderEverything we’ve done so far has been placed entirely inside the boot sector.  ...
  • tangl_99
  • tangl_99
  • 2003年01月21日 09:06
  • 2178

Write Your Own Operating System Tutorial(2)

Lesson 2: Making Our First Bootable DiskIn this lesson, we will learn how to create a boot program o...
  • tangl_99
  • tangl_99
  • 2003年01月20日 09:50
  • 1655

Write Your Own Operating System Tutorial(5)

Lesson 5: Let’s Make It InteractiveAll of this printing stuff to the screen is fun, but no operating...
  • tangl_99
  • tangl_99
  • 2003年01月21日 09:06
  • 1435

Write Your Own Operating System Tutorial(7)

Lesson 7: Start Saying Goodbye To BIOSNow that we have a boot loader that will load our operating sy...
  • tangl_99
  • tangl_99
  • 2003年01月21日 09:05
  • 1896

Write Your Own Operating System Tutorial(1)

Lesson 1: The Boot SectorIn this lesson we’ll learn about the contents of the boot sector so that we...
  • tangl_99
  • tangl_99
  • 2003年01月20日 09:43
  • 2223

Write Your Own Operating System Tutorial(中文版) -Lesson 5: 动起来!

 Lesson 5: 动起来!在屏幕上打印点东西是够乐的,但是如果操作系统一点都不能互动的话,还远不够好。让我们试着让它从键盘读取输入。我们将再一次使用BIOS的函数,来读取键盘。我们将使用0x16号...
  • aero_boy
  • aero_boy
  • 2009年02月08日 11:12
  • 577

Write Your Own Operating System Tutorial(中文版) - 简介

操作系统实作入门首先说说我们用到的工具。     既然要写操作系统,必不可少的就是需要汇编语言的编译器。如果你一时找不到tasm,masm等年头已久的东西,你可以试试nasm(netwide asse...
  • aero_boy
  • aero_boy
  • 2009年02月08日 10:50
  • 685

Write Your Own Operating System Tutorial(中文版)- Lesson 3: NASM

 Lesson 3: NASM    这节课我们将学着用一个汇编语言编译器来写我们的程序。前几课里我们使用的是DEBUG。把玩过这些之后,你很快就会发现用DEBUG来编写哪怕是一小段指令都是非常痛苦的...
  • aero_boy
  • aero_boy
  • 2009年02月08日 11:08
  • 911
您举报文章:Write Your Own Operating System Tutorial(4)