title: Real-Mode x86 ASM: How are the Basics Done?
body: <p>I'm currently reading the <code>boot.s</code> file in the source for the first ever Linux kernel (assuming that 0.01 is indeed the first public release).</p>
  
  <p>I know C and ASM, the latter considerably less than the former. Even so, I seem to be able to understand and essentially grasp the code in the source files.</p>
  
  <p>This file has me confused though. I now realise that's because it's in real mode, not protected mode. Needless to say, I've never seen ASM code written in real mode before. Protected mode was the defacto mode x86 OSes ran on before I was even born, so it's to be expected.</p>
  
  <p>Here's a routine I want to comprehend better:</p>
  
  <pre><code>/*
   * This procedure turns off the floppy drive motor, so
   * that we enter the kernel in a known state, and
   * don't have to worry about it later.
   */
  kill_motor:
      push dx
      mov dx,#0x3f2
      mov al,#0
      outb
      pop dx
      ret
  </code></pre>
  
  <p>Looking up <code>outb</code>, I find it's used to pass bytes to ports on the computer. I'll hazard a guess based on C documentation that this scenario passes the 'stop motor' byte as the first argument, and  as the floppy drive port number as the second.</p>
  
  <p>Is this interface provided by the BIOS? Or by the floppy drive directly? I'm assuming the BIOS has frugal 'drivers' for very basic operation of all the fundamental devices.</p>
  
  <p>Here's where I'm stumped: it seems that numbers like <code>#0x3f2</code> are being pulled out of thin air. They're clearly hardware port numbers or something. This file is sprinkled with such numbers, with no explanation what they're referring to. Where can I find a comprehensive reference that shows all the hardware ports and control numbers they can receive from real mode? Also, it seems the file moves the kernel around in memory throughout the booting processes, with hard-coded memory addresses. Where can I find a guide for what memory address ranges are available to write over during real mode?</p>
  
  <p>I also read a comment by Linus about reprogramming interrupts to avoid a collision between the BIOS and internal hardware interrupts. I'm not going to lie, that went right over my head.</p>
  
  <p>Help would be great; Google seems sparse on the topic, in case you're wondering.</p>