segment_base.S 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. TODO get working. All tutorials I've seen so far just set it to 0 like real OSes :-(
  3. Example of the effect on a memory access of changing the segment base address .
  4. Expected output: "x\na\nb".
  5. Without segment manipulation, the output would be "a".
  6. */
  7. #include "common.h"
  8. BEGIN
  9. CLEAR
  10. STAGE2
  11. PROTECTED_MODE
  12. /* Sanity check 1: just print the output. */
  13. VGA_PRINT_STRING $output
  14. /* Sanity check 2: make a mov without any segment manipulation. */
  15. mov message, %cl
  16. mov %cl, output
  17. VGA_PRINT_STRING $output
  18. /* Now for the real action. */
  19. mov $gdt_data, %edx
  20. /* We are touching the 7th byte of the data entry. */
  21. add $3, %edx
  22. mov (%edx), %al
  23. /* Cache it for later. */
  24. mov %al, %bl
  25. /* Set the first bit of the descriptor memory. */
  26. xor $1, %al
  27. mov %al, (%edx)
  28. /*
  29. We must re-set ds because the segment descriptor is cached
  30. and this updates it:
  31. http://wiki.osdev.org/Descriptor_Cache
  32. */
  33. mov $DATA_SEG, %ax
  34. mov %ax, %ds
  35. /*
  36. This is the only memory access we will make with
  37. the modified segment, to minimize the effect on our IO.
  38. */
  39. mov message, %cl
  40. /* Restore the old segment. */
  41. /* TODO is this needed to take into account the new segmentation? */
  42. dec %edx
  43. mov %bl, (%edx)
  44. mov %ax, %ds
  45. /*
  46. TODO this sanity check is not printing "ab".
  47. It fails, so we're not restoring the old state properly.
  48. Likely blows up because video memory going wrong.
  49. */
  50. VGA_PRINT_STRING $message
  51. mov %cl, output
  52. VGA_PRINT_STRING $output
  53. jmp .
  54. message:
  55. .asciz "ab"
  56. output:
  57. .asciz "x"