栏目搜索
 
 
 
 
你的位置:首页 > 程序设计相关资料 > WriteYourOwnOperatingSystemTutorial(3) >
 

WriteYourOwnOperatingSystemTutorial(3)

发布者:[本站编辑] | 来源:[]

WriteYourOwnOperatingSystemTutorial(3)_电脑维修资料库

lesson 3: nasmin this lesson we will learn to use an assembler to write our programs.  in previous lessons we have assembled them using debug.  after playing around with this for a while, you will quickly see that it would be a pain to use debug to create a program of more than a handful of instructions (even harder to modify).  we need a simpler way.  we will start by using the “netwide assembler” (nasm).  go to the official web page at http://www.octium.net/nasm/ and download a copy of the assembler.
now we'll use this assembler to create the same “operating system” that we did at the end of lesson 2.  download the boot program h.asm and take a look at it.  the first instruction should be somewhat familiar by now.  this is the instruction to jump over the boot record data.  in this case, it's a jump to the label begin.  after the jump instruction is 20 bytes of data.  this is the data that i read off my floppy disk using the debug program.  these values should work fine.  if you want, try replacing the data with the data from your own disk.  most of it should be the same.
(note: keep in mind that numbers made up of more than 1 byte will look “byte swapped” when viewed in debug because on the intel architecture, the least significant byte is stored at the lowest memory address and vice versa.  the bytes will look backwards.)
the code starting at the label begin should look similar to the code we wrote for lesson 2.  it simply prints the letter 'h' to the screen and loops forever.  at the bottom of this file you will see first a check to make sure the code all fits within 512 bytes (the size of one sector), then the line beginning with the word “times” adds zeros to the end of the file to pad the executable to 510 bytes.  finally, the two-byte signature 0x55, 0xaa is added to the end of the file.  assemble the file at the command prompt with the following command.
nasmw h.asm –o h.bin
this assembles the assembly file to a pure binary executable h.bin.  check the size of the binary file.  if we have done things right, it should be exactly 512 bytes.  this is exactly the size needed to fit in the boot sector of the floppy.
now we need to copy this file onto our floppy.  with the floppy in the disk, run debug, and type the following commands
debug
-n h.bin
-l 0
 
this loads our file into memory starting at address 0.  use the dump (d) and unassemble (u) commands if you wish to confirm that our file has assembled and been loaded correctly.  you will be able to see the few instructions that we have written.  notice that the file has been correctly padded with zeros up until bytes 0x1fe and 0x1ff at the end of the sector.  also note that debug fills the cx register with the number of bytes loaded from the file.  (display the contents of the registers with the r command.)
with the floppy disk in the drive, write the file to the disk with the usual command.
-w 0 0 0 1
reboot the computer with the floppy and see the program in action.  try some more things with the source code, now.  for example, maybe modify the code to print more characters.  when you are ready, proceed to the next lesson where we will create a “hello, world” operating system.
</t

 
 
  推荐文章
 
     暂无
 
 
  随机资讯
 
     暂无