atm959

OS Thing Code

Jul 12th, 2019
463
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, 4
  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.     dd 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, lp0onfire
  112.     mov ax, 00h
  113.     mov bx, 00h
  114.     mov cl, 0Fh
  115.     call placeText
  116.     mov si, pcloadletter
  117.     mov ax, 01h
  118.     mov bx, 01h
  119.     mov cl, 0Fh
  120.     call placeText
  121.     mov si, notatypewriter
  122.     mov ax, 02h
  123.     mov bx, 02h
  124.     mov cl, 0Fh
  125.     call placeText
  126.     mov si, abortretryfail
  127.     mov ax, 03h
  128.     mov bx, 03h
  129.     mov cl, 0Fh
  130.     call placeText
  131.     mov si, gurumeditation
  132.     mov ax, 04h
  133.     mov bx, 04h
  134.     mov cl, 0Fh
  135.     call placeText
  136.     mov si, nocarrier
  137.     mov ax, 05h
  138.     mov bx, 05h
  139.     mov cl, 0Fh
  140.     call placeText
  141.     mov si, oops
  142.     mov ax, 06h
  143.     mov bx, 06h
  144.     mov cl, 0Fh
  145.     call placeText
  146.     mov si, whoopsie
  147.     mov ax, 07h
  148.     mov bx, 07h
  149.     mov cl, 0Fh
  150.     call placeText
  151.    
  152. .loop:
  153.  
  154.     mov ax, 0Ah
  155.     mov bx, 0Ah
  156.     mov ecx, [lolVal]
  157.     call print32Bit
  158.  
  159.     add [lolVal], DWORD 1
  160.  
  161.     jmp .loop
  162.  
  163. lolVal:
  164.     dd 0
  165.  
  166. lp0onfire:
  167.     db "Microsoft MS-DOS 6.22 (C) Microsoft 1994", 0
  168. pcloadletter:
  169.     db "Lol JK, this isn't MS-DOS.", 0
  170. notatypewriter:
  171.     db "Text Mode", 0
  172. abortretryfail:
  173.     db "Whatever resolution this is.", 0
  174. gurumeditation:
  175.     db "80x25 characters?", 0
  176. nocarrier:
  177.     db "Protected Mode", 0
  178. oops:
  179.     db "IDT is set up.", 0
  180. whoopsie:
  181.     db "Noice", 0
  182.  
  183. tempx: dw 0
  184. tempy: dw 0
  185. tempattrib: db 0
  186. tempval: dq 0
  187.  
  188. ;esi - Pointer to text
  189. ;ax - X Pos.
  190. ;bx - Y Pos.
  191. ;cx - Attrib.
  192. placeText:
  193.     mov [tempattrib], cl
  194.     mov [tempx], ax
  195.     mov ax, bx
  196.     mov bx, 160
  197.     mul bx
  198.     mov [tempy], ax
  199.     mov ax, [tempx]
  200.     mov bx, 2
  201.     mul bx
  202.     mov bx, ax
  203.     mov ax, [tempy]
  204.     add eax, ebx
  205.    
  206.     mov ecx, 0B8000h
  207.     add ecx, eax
  208. .placeTextLoop:
  209.     lodsb
  210.     cmp al, 00h
  211.     je .donePlacingText
  212.     mov [ecx], BYTE al
  213.     add ecx, 1h
  214.     mov dx, [tempattrib]
  215.     mov [ecx], dx
  216.     add ecx, 1h
  217.     jmp .placeTextLoop
  218. .donePlacingText:
  219.     ret
  220.    
  221. clearTextScreen:
  222.     mov ecx, 0B8000h
  223.     mov ax, (80*25)*2
  224. .loop:
  225.     mov [ecx], BYTE 00h
  226.     add ecx, 01h
  227.     sub ax, 01h
  228.     cmp ax, 00h
  229.     je .doneClearingTextScreen
  230.     jmp .loop
  231. .doneClearingTextScreen:
  232.     ret
  233.    
  234. divideByZero:
  235.     db "DIVIDE-BY-ZERO EXCEPTION", 0
  236. pcTxt:
  237.     db "PC: ", 0
  238. espTxt:
  239.     db "ESP: ", 0
  240. eaxTxt:
  241.     db "EAX: ", 0
  242. ebxTxt:
  243.     db "EBX: ", 0
  244. ecxTxt:
  245.     db "ECX: ", 0
  246. edxTxt:
  247.     db "EDX: ", 0
  248. hexTable:
  249.     db "0123456789ABCDEF", 0
  250.    
  251. temppc: dd 0
  252. tempesp: dd 0
  253. tempeax: dd 0
  254. tempebx: dd 0
  255. tempecx: dd 0
  256. tempedx: dd 0
  257.  
  258. copyRegsToMem:
  259.     mov [tempeax], eax
  260.     mov [tempebx], ebx
  261.     mov [tempecx], ecx
  262.     mov [tempedx], edx
  263.     pop ebx ;Pop the return address from calling this subroutine into EBX
  264.     pop eax
  265.     mov [temppc], eax
  266.     push ebx ;Push the return address back onto the stack
  267.     mov [tempesp], esp
  268.     ret
  269.  
  270. zeroDivide_exception:
  271.     call copyRegsToMem
  272.  
  273.     call clearTextScreen
  274.     mov si, divideByZero
  275.     mov ax, 00h
  276.     mov bx, 00h
  277.     mov cx, 0Ch
  278.     call placeText
  279.  
  280.     call showRegisters
  281.    
  282.     jmp hang
  283.    
  284. showRegisters:
  285.     mov si, pcTxt
  286.     mov ax, 00h
  287.     mov bx, 01h
  288.     mov cx, 0Ch
  289.     call placeText
  290.     mov si, espTxt
  291.     mov ax, 00h
  292.     mov bx, 02h
  293.     mov cx, 0Ch
  294.     call placeText
  295.     mov si, eaxTxt
  296.     mov ax, 00h
  297.     mov bx, 03h
  298.     mov cx, 0Ch
  299.     call placeText
  300.     mov si, ebxTxt
  301.     mov ax, 00h
  302.     mov bx, 04h
  303.     mov cx, 0Ch
  304.     call placeText
  305.     mov si, ecxTxt
  306.     mov ax, 00h
  307.     mov bx, 05h
  308.     mov cx, 0Ch
  309.     call placeText
  310.     mov si, edxTxt
  311.     mov ax, 00h
  312.     mov bx, 06h
  313.     mov cx, 0Ch
  314.     call placeText
  315.  
  316.     mov ax, 05h
  317.     mov bx, 01h
  318.     mov ecx, [temppc]
  319.     call print32Bit
  320.     mov ax, 05h
  321.     mov bx, 02h
  322.     mov ecx, [tempesp]
  323.     call print32Bit
  324.     mov ax, 05h
  325.     mov bx, 03h
  326.     mov ecx, [tempeax]
  327.     call print32Bit
  328.     mov ax, 05h
  329.     mov bx, 04h
  330.     mov ecx, [tempebx]
  331.     call print32Bit
  332.     mov ax, 05h
  333.     mov bx, 05h
  334.     mov ecx, [tempecx]
  335.     call print32Bit
  336.     mov ax, 05h
  337.     mov bx, 06h
  338.     mov ecx, [tempedx]
  339.     call print32Bit
  340.     ret
  341.  
  342. tempVal: dd 0
  343. andVal: dd 0
  344. shiftVal: db 0
  345. counter: dd 0
  346.  
  347. tempX: dw 0
  348. tempY: dw 0
  349.  
  350. hexString: db "00000000H", 0
  351.  
  352. ;ax - X Pos.
  353. ;bx - Y Pos.
  354. ;ecx - Value to print
  355. print32Bit:
  356.     mov [tempX], ax
  357.     mov [tempY], bx
  358.     mov [tempVal], ecx
  359.     mov [shiftVal], BYTE 28
  360.     mov [andVal], DWORD 0F0000000h
  361.     mov [counter], BYTE 00h
  362.  
  363. .copyToStringLoop:
  364.     mov eax, [tempVal]
  365.     and eax, [andVal]
  366.     mov cl, [shiftVal]
  367.     shr DWORD eax, cl
  368.  
  369.     mov ebx, hexTable
  370.     add ebx, eax
  371.  
  372.     mov ecx, hexString
  373.     mov edx, [counter]
  374.     add ecx, edx
  375.     mov dl, [ebx]
  376.     mov [ecx], dl
  377.  
  378.     sub [shiftVal], BYTE 4
  379.     shr DWORD [andVal], BYTE 4
  380.     add [counter], DWORD 1
  381.  
  382.     cmp [counter], DWORD 8
  383.     jne .copyToStringLoop
  384.    
  385.     mov ax, [tempX]
  386.     mov bx, [tempY]
  387.     mov cl, 0Fh
  388.     mov si, hexString
  389.     call placeText
  390.  
  391.     ret
  392.  
  393. hang:
  394.     jmp hang
Advertisement
Add Comment
Please, Sign In to add comment