title: Where to place the stack and load the kernel
body: <p>I'm new to operating system development and I'm curious about a problem I've run into while developing my own bootloader. My operating system is going to be written in assembly and will run in 16-bit real mode.</p>
  
  <p>I know what the stack is and I have the impression that it grows downwards into the memory. Correct me if I'm mistaken. I know how to load a basic kernel into the memory from a floppy disc and I don't believe that is the problem.  </p>
  
  <p>The problem I'm running into is that I'm unsure where to place the stack and load my kernel into memory. I've tried creating my stack like this and I'm running into problems:</p>
  
  <pre><code>mov ax, 0x0000
  mov ss, ax
  mov sp, 0xFFFF
  </code></pre>
  
  <p>I'm loading my kernel at <code>0x1000:0x0000</code>. When I <em>PUSH</em> and later <em>POP</em> the volatile registers in my <code>print</code> function, my kernel just hangs the <strong>second</strong> time I do <code>call print</code>. This is my <code>print</code> function:</p>
  
  <pre><code>print:
      push ax
      push bx
      push cx
  
      mov al, [si]
      cmp al, 0
      je p_Done
  
      cmp al, 9
      je p_Tab
  
      mov ah, 0xE
      int 0x10
  
      cmp al, 10
      je p_NewLine
     p_Return:
      inc si
      jmp print
    p_Tab:
      xor cx, cx
     p_Tab_Repeat:
      cmp cx, 8
      je p_Return
      mov ah, 0xE
      mov al, " "
      int 0x10
      inc cx
      jmp p_Tab_Repeat
    p_NewLine:
      xor bx, bx
      mov ah, 0x3
      int 0x10
  
      mov dl, 0x00
      mov ah, 0x2
      int 0x10
      jmp p_Return
    p_Done:
      pop cx
      pop bx
      pop ax
      ret
  </code></pre>
  
  <p>These are the lines I want to display:</p>
  
  <pre><code>db "Kernel successfully loaded!", 10, 0
  db 9, "Lmao, just a tab test!", 10, 0
  </code></pre>
  
  <p>This is the output I get when my kernel runs (the <code>_</code> is the cursor):</p>
  
  <pre><code>Kernel successfully loaded!
  _
  </code></pre>
  
  <p>It successfully prints the first line, but hangs while printing the second. If I remove the <em>PUSH</em> and <em>POP</em> statements it works just fine. Why does my kernel hang when I attempt save and restore registers in my <code>print</code> function? Where should I place my stack and where should I load my kernel?</p>