title: How does this assembly bootloader code work?
body: <p>I have the following code in a file (<code>kernel.asm</code>):</p>
  
  <pre><code>bits 32
  section .text
          ;multiboot spec
          align 4
          dd 0x1BADB002               ;magic
          dd 0x00                     ;flags
          dd - (0x1BADB002 + 0x00)    ;checksum. m+f+c should be zero
  
  global start
  extern k_main                       ;this is defined in the c file
  
  start:
      cli                             ;block interrupts
      mov esp, stack_space            ;set stack pointer
      call k_main
      hlt                             ;halt the CPU
  
  section .bss
  resb 8192                           ;8KB for stack
  stack_space:
  </code></pre>
  
  <hr>
  
  <pre><code>align 4
  dd 0x1BADB002               ;magic
  dd 0x00                     ;flags
  dd - (0x1BADB002 + 0x00)    ;checksum. m+f+c should be zero
  </code></pre>
  
  <p>I have tested the code, linked it with the kernel and it works fine.</p>
  
  <p>What does <code>align 4</code> mean? I think it has something to do with memory.</p>
  
  <p>If the line <code>dd 0x1BADB002</code> defines a hexadecimal address meaning 'bad boot', why is it there if the operating system loads fine?</p>
  
  <p>The next line <code>dd 0x00</code>, I'm assuming sets all of the flags to 0?</p>
  
  <p><code>dd - (0x1BADB002 + 0x00)</code>: seems to be doing a similar thing to the second line adding 0 to the address <code>0xBADB002</code>. What does the minus mean before the brackets? Does the minus mean that something is being subtracted? If so, how can something be subtracted if there is nothing to subtract it from? Also, why is <code>0</code> being added to <code>0xBADB002</code>? Isn't it the same thing as <code>0xBADB002</code>? Does it make a difference if it is added or not?</p>
  
  <p>I'm also confused as to why this works, because it's in 32 bit and the computer starts in 16 bit real mode. Is the computer just executing the 32 bit code and calling the kernel?</p>
  
  <p>Thanks in advance</p>