Advertisement
MichaelPetch

SO 76928196

Aug 18th, 2023 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. ORG 0x7c00 ; add to offsets
  2.  
  3. start:
  4. .next:
  5. xor ax, ax ; make it zero
  6. mov ds, ax ; DS=0
  7. mov ss, ax ; stack starts at seg 0
  8. mov sp, 0x9c00 ; 2000h past code start,
  9. ; making the stack 7.5k in size
  10.  
  11. cli ; no interrupts
  12. push ds ; save real mode
  13.  
  14. lgdt [gdtinfo] ; load gdt register
  15.  
  16. mov eax, cr0 ; switch to pmode by
  17. or al,1 ; set pmode bit
  18. mov cr0, eax
  19. jmp 0x8:pmode
  20.  
  21. pmode:
  22. mov bx, 0x10 ; select descriptor 2
  23. mov ds, bx ; 10h = 10000b
  24.  
  25. and al,0xFE ; back to realmode
  26. mov cr0, eax ; by toggling bit again
  27. jmp 0x0:unreal ; This also flushes the IPFQ and sets CS to 0
  28. ; If you don't do this CS will be 0x08 with a base of 0
  29. jmp .flushipfq
  30. .flushipfq: ; Flush IPFQ (Instruction Pre-Fetch Queue)
  31.  
  32.  
  33. unreal:
  34. pop ds ; get back old segment
  35. sti ; Enabling interrupts with a bad CS will cause problems
  36.  
  37. call 0x00:farfunction ; Make a far call
  38. goodreturn:
  39. mov bx, 0x57 << 8 | 'G'; attrib/char 'G'
  40. mov eax, 0x0b8000 ; note 32 bit offset
  41. mov word [ds:eax], bx
  42.  
  43. .endloop:
  44. hlt
  45. jmp .endloop ; loop forever
  46.  
  47. farfunction:
  48. retf ; Returns to the CS:IP pushed on stack by far call
  49. ; A CS of 0x8 instead of 0x0 will return 8*16 bytes
  50. ; further in memory than expected.
  51.  
  52. times 128+goodreturn-$ db 0
  53. ; Put this code 128 bytes from label `goodreturn`
  54. badreturn:
  55. mov bx, 0x57 << 8 | 'B'; attrib/char 'B'
  56. mov eax, 0x0b8000 ; note 32 bit offset
  57. mov word [ds:eax], bx
  58.  
  59. .endloop:
  60. hlt
  61. jmp .endloop ; loop forever
  62.  
  63. gdtinfo:
  64. dw gdt_end - gdt - 1 ;last byte in table
  65. dd gdt ;start of table
  66.  
  67. gdt: dd 0,0 ; entry 0 is always unused
  68. codedesc: db 0xff, 0xff, 0, 0, 0, 10011010b, 00000000b, 0
  69. flatdesc: db 0xff, 0xff, 0, 0, 0, 10010010b, 11001111b, 0
  70. gdt_end:
  71.  
  72. times 510-($-$$) db 0 ; fill sector w/ 0's
  73. dw 0xAA55 ; Required by some BIOSes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement