Advertisement
Guest User

P&D OS - MBR - 18/02/2019

a guest
Feb 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;; A tiny, working bootloader for x86 PCs. Has a few subroutines
  2. ;; so it's slightly less useless than just printing "hello world".
  3. ;;
  4. ;; writeup here: http://joebergeron.io/posts/post_two.html
  5. ;;
  6. ;; Joe Bergeron, 2016.
  7. ;;
  8. ;;
  9.         bits 16
  10.  
  11.         mov ax, 07C0h
  12.         mov ds, ax
  13.         mov ax, 07E0h           ; 07E0h = (07C00h+200h)/10h, beginning of stack segment.
  14.         mov ss, ax
  15.         mov sp, 2000h           ; 8k of stack space.
  16.  
  17.         call cleanscreen
  18.  
  19.         push 0000h
  20.         call movecursor
  21.         push 1
  22.         call cleanstack
  23.  
  24.         push welcome_msg
  25.         call printmsg
  26.         push 1
  27.         call cleanstack
  28.  
  29.         cli
  30.         hlt
  31.  
  32. cleanstack:
  33.         push bp
  34.         mov bp, sp
  35.         pusha
  36.  
  37.         mov bx, 2
  38. .clean_param:
  39.         mov ax, [bp + 4]
  40.         mul bx
  41.         add bp, ax
  42.         mov word [bp + 4], 0
  43.         sub bp, ax
  44.         test ax, 0
  45.         jz .clean_last
  46.         jmp .clean_param
  47.  
  48. .clean_last:
  49.         mov word [bp + 4], 0 ; clean `cleanstack` param also...
  50.  
  51.         popa
  52.         mov sp, bp
  53.         pop bp
  54.  
  55.         ret
  56.  
  57. cleanscreen:
  58.         push bp
  59.         mov bp, sp
  60.         pusha
  61.  
  62.         mov ah, 07h             ; tells BIOS to scroll down window
  63.         mov al, 00h             ; clean entire window
  64.         xor bh, bh              ; set bh to 0: default...
  65.         ;mov bh, 07h            ; white on black
  66.         xor cx, cx              ; specifies top left of screen as (0,0)
  67.         mov dh, 18h             ; 18h = 24 rows of chars
  68.         mov dl, 4fh             ; 4fh = 79 cols of chars
  69.         int 10h                 ; calls video interrupt
  70.  
  71.         popa
  72.         mov sp, bp
  73.         pop bp
  74.  
  75.         ret
  76.  
  77. movecursor:
  78.         push bp
  79.         mov bp, sp
  80.         pusha
  81.  
  82.         mov dx, [bp + 4]        ; get the argument from the stack.
  83.         mov ah, 02h             ; set cursor position
  84.         mov bh, 00h             ; page 0 - doesn't matter, we're not using double-buffering
  85.         int 10h
  86.  
  87.         popa
  88.         mov sp, bp
  89.         pop bp
  90.  
  91.         ret
  92.  
  93. printmsg:
  94.         push bp
  95.         mov bp, sp
  96.         pusha
  97.         mov si, [bp+4]          ; grab the pointer to the data
  98.         mov bh, 00h             ; page number, 0 again
  99.         mov bl, 00h             ; foreground color, irrelevant - in text mode
  100.         mov ah, 0Eh             ; print character to TTY
  101.  .char:
  102.         mov al, [si]            ; get the current char from our pointer position
  103.         add si, 1               ; keep incrementing si until we see a null char
  104.         or al, 0
  105.         je .return              ; end if the string is done
  106.         int 10h                 ; print the character if we're not done
  107.         jmp .char               ; keep looping
  108.  .return:
  109.         popa
  110.         mov sp, bp
  111.         pop bp
  112.  
  113.         ret
  114.  
  115.  
  116. welcome_msg:
  117.         db "Welcome to P&D OS", 0
  118.  
  119. booting_msg:
  120.         db "Booting...", 0
  121.  
  122. ; Write 0s until 512 - 2 (1st bootsector size - look forward).
  123. times 510-($-$$) db 0
  124.  
  125. ; END OF BOOTLOADER (required).
  126. dw 0xAA55
  127.  
  128. ; END OF BOOTLOADER
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement