Develop a Bootloader In Assembly

放肆的年华 提交于 2019-12-04 09:30:21

You use int 0x13 to load in the required number of sectors and jump to the location you have place the new code. There is nothing you need to do in the second stage, but you will want to make sure that you set DS to be valid for wherever you load the code.

Example from my little OS archive:

  /* BIOS loads the sectors into es:bx */
  pushw    $STAGE1_WORKSEG
  popw     %es
  movw     $STAGE1_OFFSET, %bx

read_stage1:

  /* Try to read in a few sectors */
  movb     $0x2, %cl      /* Sector */
  movb     $0x0, %ch      /* Cylinder */
  movb     $0x0, %dh      /* Head */
  movb     $0x0, %dl      /* Drive */
  movb     $0x2, %ah      /* BIOS read function */

  /* How many sectors to load */
  movb     $STAGE1_SIZE, %al 
  int      $0x13
  jnc      read_stage1_done

  /* Reset drive */
  xorw     %ax, %ax
  int      $0x13
  jmp      read_stage1


read_stage1_done:

  /* Perform a long jump into stage1 */
  ljmp     $STAGE1_WORKSEG, $STAGE1_OFFSET

  call     halt

halt:
    /*
    * Function: halt
    * Synopsis: Sends the processor into a permanent halted status
    * Notes:
    *    The only way out of this is to manually reboot
    */
    hlt                     /* Halt the processor */
    jmp      halt

That's in GAS format so you'll want to reverse the operand order because it looks like you're using NASM from the times instruction. The variable names should be self-explanatory.

If you're developing a hobby OS then http://forum.osdev.org/ is a good place to get support from others doing the same thing. It's a bit more specialised than stackoverflow and a lot of OS stuff can be quite esoteric.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!