Advertisement
atm959

OS Thing Code

Jul 10th, 2019
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. BITS 16
  2. ORG 7C00h
  3.  
  4. start:
  5.     cli
  6.    
  7.     mov [drive], dl
  8.    
  9.     mov ah, 01h
  10.     mov ch, 3Fh
  11.     int 10h
  12.    
  13.     mov ah, 13h
  14.     mov al, 01h
  15.     mov bh, 00h
  16.     mov bl, 0Fh
  17.     mov cx, 0Ah
  18.     mov dh, 00h
  19.     mov dl, 00h
  20.     mov bp, loading
  21.     int 10h
  22.    
  23.     mov dl, [drive]
  24.    
  25.     mov ah, 02h
  26.     mov al, 2
  27.     mov ch, 0
  28.     mov dh, 0
  29.     mov cl, 2
  30.     mov bx, loadLocation
  31.     int 13h
  32.    
  33.     xor ax, ax
  34.     push ax
  35.     popf
  36.     mov ds, ax
  37.     lgdt [gdt_desc]
  38.     mov eax, cr0
  39.     or eax, 1
  40.     mov cr0, eax
  41.     jmp 08h:protectedModeBegin
  42.  
  43. loading:
  44.     db "Loading...", 0
  45.    
  46. drive: db 0
  47.  
  48. BITS 32
  49.  
  50. protectedModeBegin:
  51.     mov ax, 10h
  52.     mov ds, ax
  53.     mov ss, ax
  54.     mov esp, 90000h
  55.    
  56.     jmp loadLocation   
  57.  
  58. gdt:
  59. gdt_null:
  60.     dq 0
  61. gdt_code:
  62.     dw 0FFFFh
  63.     dw 0
  64.     db 0
  65.     db 10011010b
  66.     db 11001111b
  67.     db 0
  68. gdt_data:
  69.     dw 0FFFFh
  70.     dw 0
  71.     db 0
  72.     db 10010010b
  73.     db 11001111b
  74.     db 0
  75. gdt_end:
  76.  
  77. gdt_desc:
  78.     dw gdt_end-gdt
  79.     dd gdt
  80.  
  81. idtbegin:
  82. zerodivide:
  83.     dw 0000h
  84.     dw 0008h
  85.     db 00h
  86.     db 8Eh
  87.     dw 0000h
  88. idtend:
  89.  
  90. idt:
  91.     dw idtend-idtbegin
  92.     dq idtbegin
  93.  
  94.     times 510-($-$$) db 0
  95.     dw 0AA55h
  96.    
  97. loadLocation:
  98.     mov eax, zeroDivide_exception
  99.     and eax, 0000FFFFh
  100.     mov [zerodivide], ax
  101.     mov eax, zeroDivide_exception
  102.     and eax, 0FFFF0000h
  103.     mov [zerodivide+6], ax
  104.     lidt [idt]
  105.    
  106.     mov bl, 00h
  107.     div bl      ;Cause a divide-by-zero exception ;)
  108.    
  109.     call clearTextScreen
  110.  
  111.     mov si, whoopsie
  112.     mov ax, 01h
  113.     mov bx, 01h
  114.     call placeText
  115.     mov si, oops
  116.     mov ax, 02h
  117.     mov bx, 02h
  118.     call placeText
  119.     mov si, gurumeditation
  120.     mov ax, 03h
  121.     mov bx, 03h
  122.     call placeText
  123.     mov si, abortretryfail
  124.     mov ax, 04h
  125.     mov bx, 04h
  126.     call placeText
  127.     mov si, notatypewriter
  128.     mov ax, 05h
  129.     mov bx, 05h
  130.     call placeText
  131.     mov si, nocarrier
  132.     mov ax, 06h
  133.     mov bx, 06h
  134.     call placeText
  135.     mov si, pcloadletter
  136.     mov ax, 07h
  137.     mov bx, 07h
  138.     call placeText
  139.     mov si, lp0onfire
  140.     mov ax, 08h
  141.     mov bx, 08h
  142.     call placeText
  143.    
  144. .loop:
  145.     jmp .loop
  146.  
  147. lp0onfire:
  148.     db "lp0 on fire", 0
  149. pcloadletter:
  150.     db "PC LOAD LETTER", 0
  151. notatypewriter:
  152.     db "Not a typewriter", 0
  153. abortretryfail:
  154.     db "Abort, Retry, Fail?", 0
  155. gurumeditation:
  156.     db "Guru Meditation #00000004.0000AAC0", 0
  157. nocarrier:
  158.     db "NO CARRIER", 0
  159. oops:
  160.     db "thing(0): Oops", 0
  161. whoopsie:
  162.     db "whoopsie", 0
  163.  
  164. tempx: dw 0
  165. tempy: dw 0
  166. tempattrib: db 0
  167. tempval: dq 0
  168.  
  169. ;esi - Pointer to text
  170. ;ax - X Pos.
  171. ;bx - Y Pos.
  172. ;cx - Attrib.
  173. placeText:
  174.     mov [tempattrib], cx
  175.     mov [tempx], ax
  176.     mov ax, bx
  177.     mov bx, 160
  178.     mul bx
  179.     mov [tempy], ax
  180.     mov ax, [tempx]
  181.     mov bx, 2
  182.     mul bx
  183.     mov bx, ax
  184.     mov ax, [tempy]
  185.     add eax, ebx
  186.    
  187.     mov ecx, 0B8000h
  188.     add ecx, eax
  189. .placeTextLoop:
  190.     lodsb
  191.     cmp al, 00h
  192.     je .donePlacingText
  193.     mov [ecx], BYTE al
  194.     add ecx, 1h
  195.     mov dx, [tempattrib]
  196.     mov [ecx], dx
  197.     add ecx, 1h
  198.     jmp .placeTextLoop
  199. .donePlacingText:
  200.     ret
  201.    
  202. clearTextScreen:
  203.     mov ecx, 0B8000h
  204.     mov ax, (80*25)*2
  205. .loop:
  206.     mov [ecx], BYTE 00h
  207.     add ecx, 01h
  208.     sub ax, 01h
  209.     cmp ax, 00h
  210.     je .doneClearingTextScreen
  211.     jmp .loop
  212. .doneClearingTextScreen:
  213.     ret
  214.    
  215. divideByZero:
  216.     db "DIVIDE-BY-ZERO EXCEPTION", 0
  217. pcTxt:
  218.     db "PC: ", 0
  219. espTxt:
  220.     db "ESP: ", 0
  221. eaxTxt:
  222.     db "EAX: ", 0
  223. ebxTxt:
  224.     db "EBX: ", 0
  225. ecxTxt:
  226.     db "ECX: ", 0
  227. edxTxt:
  228.     db "EDX: ", 0
  229. hexTable:
  230.     db "0", 0
  231.     db "1", 0
  232.     db "2", 0
  233.     db "3", 0
  234.     db "4", 0
  235.     db "5", 0
  236.     db "6", 0
  237.     db "7", 0
  238.     db "8", 0
  239.     db "9", 0
  240.     db "A", 0
  241.     db "B", 0
  242.     db "C", 0
  243.     db "D", 0
  244.     db "E", 0
  245.     db "F", 0
  246. hexEnd:
  247.     db "H", 0
  248.    
  249. temppc: dq 0
  250. tempesp: dq 0
  251. tempeax: dq 0
  252. tempebx: dq 0
  253. tempecx: dq 0
  254. tempedx: dq 0
  255.  
  256. copyRegsToMem:
  257.     mov [tempeax], eax
  258.     mov [tempebx], ebx
  259.     mov [tempecx], ecx
  260.     mov [tempedx], edx
  261.     pop ebx ;Pop the return address from calling this subroutine into EBX
  262.     pop eax
  263.     mov [temppc], eax
  264.     push ebx ;Push the return address back onto the stack
  265.     mov [tempesp], esp
  266.     ret
  267.  
  268. zeroDivide_exception:
  269.     call copyRegsToMem
  270.  
  271.     call clearTextScreen
  272.     mov si, divideByZero
  273.     mov ax, 00h
  274.     mov bx, 00h
  275.     mov cx, 0Ch
  276.     call placeText
  277.  
  278.     call showRegisters
  279.    
  280.     jmp hang
  281.    
  282. showRegisters:
  283.     mov si, pcTxt
  284.     mov ax, 00h
  285.     mov bx, 01h
  286.     mov cx, 0Ch
  287.     call placeText
  288.     mov si, espTxt
  289.     mov ax, 00h
  290.     mov bx, 02h
  291.     mov cx, 0Ch
  292.     call placeText
  293.     mov si, eaxTxt
  294.     mov ax, 00h
  295.     mov bx, 03h
  296.     mov cx, 0Ch
  297.     call placeText
  298.     mov si, ebxTxt
  299.     mov ax, 00h
  300.     mov bx, 04h
  301.     mov cx, 0Ch
  302.     call placeText
  303.     mov si, ecxTxt
  304.     mov ax, 00h
  305.     mov bx, 05h
  306.     mov cx, 0Ch
  307.     call placeText
  308.     mov si, edxTxt
  309.     mov ax, 00h
  310.     mov bx, 06h
  311.     mov cx, 0Ch
  312.     call placeText
  313.  
  314.     mov ax, 05h
  315.     mov bx, 01h
  316.     mov ecx, [temppc]
  317.     call print32Bit
  318.     mov ax, 05h
  319.     mov bx, 02h
  320.     mov ecx, [tempesp]
  321.     call print32Bit
  322.     mov ax, 05h
  323.     mov bx, 03h
  324.     mov ecx, [tempeax]
  325.     call print32Bit
  326.     mov ax, 05h
  327.     mov bx, 04h
  328.     mov ecx, [tempebx]
  329.     call print32Bit
  330.     mov ax, 05h
  331.     mov bx, 05h
  332.     mov ecx, [tempecx]
  333.     call print32Bit
  334.     mov ax, 05h
  335.     mov bx, 06h
  336.     mov ecx, [tempedx]
  337.     call print32Bit
  338.     ret
  339.  
  340. ;ax - X Pos.
  341. ;bx - Y Pos.
  342. ;ecx - Value to print
  343. print32Bit:
  344.     mov cx, 0Fh
  345.     mov si, hexTable
  346.     call placeText
  347.     ret
  348.  
  349. hang:
  350.     jmp hang
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement