You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.6 KiB

5 years ago
  1. [bits 16]
  2. [org 0x0600]
  3. %define STAGE1_BASE 0x7c00
  4. %define STAGE2_BASE 0x0600
  5. %define STACK_POINTER_MAX 0x2000
  6. %macro HALT 0
  7. %%h:
  8. cli
  9. hlt
  10. jmp %%h
  11. %endmacro
  12. %macro CRLF 0
  13. push ax
  14. mov ax, 0xe00|13
  15. int 0x10
  16. mov al, 10
  17. int 0x10
  18. pop ax
  19. %endmacro
  20. segment Stage1 vstart=STAGE1_BASE
  21. Stage1:
  22. ; Setup segments
  23. cli
  24. xor ax, ax
  25. xor bx, bx
  26. xor cx, cx
  27. xor dx, dx
  28. xor si, si
  29. xor di, di
  30. times 32-($-$$) nop
  31. times 7 inc ax
  32. times 40 nop
  33. call PrintAxHex
  34. call PrintCRLF
  35. HALT
  36. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  37. PrintAx:
  38. PrintAxHex:
  39. ; Prints the contens of ax as a hexadecimal number
  40. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  41. pusha ; save registers
  42. mov dx, ax
  43. mov ah, 0xE ; Teletype output
  44. mov cx, 4 ; 4 nibbles in a 16 bit word
  45. .PrintLoop:
  46. rol dx, 4 ; rotate to next nibble
  47. mov al, dl ; we copy to al because we need to mask only the low 4 bits
  48. and al, 0xF ; Do the masking
  49. add al, '0' ; convert to ASCII
  50. ; If we are greater than 9 ascii...
  51. cmp al, '9'
  52. jbe .SkipDiffToAsciiA
  53. add al, 'A'-('9'+1) ; ...add 7 to make digits 10 to 15 be represented as 'A' to 'F'
  54. .SkipDiffToAsciiA:
  55. int 0x10 ; BIOS call 'output'
  56. loop .PrintLoop
  57. popa ; restore registers
  58. ret
  59. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  60. PrintAxDec:
  61. ; Prints the contens of ax as a decimal number
  62. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  63. pusha
  64. mov bx, 10 ; Divisor
  65. xor cx, cx ; Digit count starts at 0
  66. .LoopDivide: ; finds digits and pushes them to stack
  67. test ax, ax
  68. jz .BreakLoopDivide
  69. xor dx, dx
  70. div bx ; dx = (dx:ax)%bx, ax = (dx:ax)/bx
  71. push dx
  72. inc cx ; Increase digit count
  73. jmp .LoopDivide
  74. .BreakLoopDivide:
  75. .LoopPrint:
  76. pop ax
  77. add al, '0' ; Convert to ascii
  78. mov ah, 0xE
  79. int 0x10
  80. loop .LoopPrint
  81. popa
  82. ret
  83. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  84. PrintCRLF:
  85. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  86. pusha
  87. mov ah, 0xE
  88. mov al, 13
  89. int 0x10
  90. mov al, 10
  91. int 0x10
  92. popa
  93. ret
  94. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  95. times 510-($-$$) db 0 ; Zero-pad to boot signature
  96. db 0x55, 0xAA ; Boot signature