Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. ; Various sub-routines that will be useful to the boot loader code
  2.  
  3. ; Take a single value for input from a key
  4. ; OUTPUT in AL
  5. TakeInput:
  6. call Console_Read_Key
  7. cmp al, 48
  8. js Error_invalid_Input
  9. cmp al, 58
  10. jns Error_invalid_Input
  11.  
  12. mov ah, 0Eh
  13. int 10h
  14. sub al, 48
  15.  
  16. push ax
  17. call Console_Write_CRLF
  18. pop ax
  19. ret
  20.  
  21. ; Takes key values and stores into a 4 character input buffer
  22. TakeInputToBuffer:
  23. mov si, inputbuffer
  24. TakeInputToBufferLoop:
  25. push cx
  26. call Console_Read_Key
  27. cmp al, 48 ; If sign flag sets, we know number is out of low bound
  28. js Error_invalid_Input ; we also now have the decimal value of the input
  29. cmp al, 58
  30. jns Error_invalid_Input ; Since we subtracted AND passed the previous test, we need to
  31.  
  32. mov ah, 0Eh ; print the typed character
  33. int 10h
  34. sub al, 48 ; convert to decimal
  35. mov [si], al ; store in buffer
  36. inc si ; increment character pointer
  37. pop cx
  38. inc cx
  39. cmp cx, 4
  40. jnz TakeInputToBufferLoop
  41. push ax
  42. call Console_Write_CRLF ; drop line for formatting
  43. pop ax
  44. ret
  45.  
  46. ; Take a sector number and write it after the word "SECTOR"
  47. ; INPUT: the values stored in the input buffer
  48. Console_Write_SecName:
  49. push ax
  50. mov si, sector
  51. call Console_Write_16
  52. ret
  53.  
  54. sector db 'Sector: ', 0
  55. inputbuffer db '0000', 0
  56.  
  57. ; Parse input buffer contents into AX as an INT
  58. ; This is an unrolled loop, to avoid incrementing and adding a bunch
  59. ParseIntFromBuffer:
  60. mov si, inputbuffer
  61. mov bx, 0 ; RESET AX TO ZERO
  62.  
  63. mov ax, [si] ; Make ax the first value in the buffer
  64. inc si ; move the buffer pointer
  65. mov dx, 1000
  66. mul dx ; multiply by 1000 because this is char 1 of a 4 digit decimal
  67. add bx, ax
  68. ; Repeat but use BX so we can accumulate results
  69. mov ax, [si]
  70. inc si
  71. mov dx, 100
  72. mul dx
  73. add bx, ax
  74.  
  75. mov ax, [si]
  76. inc si
  77. mov dx, 10
  78. mul dx
  79. add bx, ax
  80.  
  81. mov ax, bx
  82.  
  83. add bx, [si] ; last value, multipl by 1 == just adding the number
  84.  
  85. ret
  86.  
  87. ; Writes a sector out 16 lines at a time
  88. ; outer loop will write address offset and load counter
  89. ; inner loop will print out the data as hex, then print a CRLF
  90. ; ASCII loop will print the data as ASCII
  91. Console_WriteSector:
  92. push ax
  93. OuterWriteLoop:
  94. mov bx, 0D000h ;reset the offset after reading
  95. mov cx, word [bpbBytesPerSector] ;LOOP COUNTER FOR SECTOR
  96. OuterLineLoop:
  97. cmp cx, 256d
  98. je Continue
  99. ReturnBlock:
  100. push cx ;push sector counter
  101. call Console_WriteAddress_16
  102. mov cx, 16d ;LOOP COUNTER FOR LINE
  103. HexLoop:
  104. push cx ;push the line loop counter
  105. call Console_WriteHex_8
  106. add bx, 1
  107. pop cx ;pop the line loop counter
  108. dec cx
  109. jnz HexLoop
  110. mov cx, 16d ;set ascii loop counter
  111. sub bx, 16d
  112. ASCIILoop:
  113. push cx ;should push the asci loop counter
  114. mov ax, [bx]
  115. call Console_Write_ASCII
  116. inc bx
  117. pop cx ;should pop the asii loop counter
  118. dec cx
  119. jnz ASCIILoop
  120. call Console_Write_CRLF
  121. pop cx ;pop the SECTOR counter
  122. sub cx, 16d
  123. jnz OuterLineLoop
  124. pop ax
  125. ret
  126.  
  127. Continue:
  128. mov si, continue_msg
  129. call Console_WriteLine_16
  130. call Console_Read_Key
  131. jmp ReturnBlock
  132.  
  133. ; Wait to read a key scan code
  134. ; Store it in AH
  135. Console_Read_Key:
  136. mov ah, 00h
  137. int 16h
  138. ret
  139.  
  140. ; Output Carriage-Return/Line-Feed (CRLF) sequence to screen using BIOS
  141. Console_Write_CRLF:
  142. mov ah, 0Eh ; Output CR
  143. mov al, 0Dh
  144. int 10h
  145. mov al, 0Ah ; Output LF
  146. int 10h
  147. ret
  148.  
  149. ; Write to the console using BIOS.
  150. ;
  151. ; Input: SI points to a null-terminated string
  152. Console_Write_16:
  153. mov ah, 0Eh ; BIOS call to output value in AL to screen
  154. Console_Write_16_Repeat:
  155. lodsb ; Load byte at SI into AL and increment SI
  156. test al, al ; If the byte is 0, we are done
  157. je Console_Write_16_Done
  158. int 10h ; Output character to screen
  159. jmp Console_Write_16_Repeat
  160.  
  161. Console_Write_16_Done:
  162. ret
  163.  
  164. ; Write string to the console using BIOS followed by CRLF
  165. ;
  166. ; Input: SI points to a null-terminated string
  167. Console_WriteLine_16:
  168. call Console_Write_16
  169. call Console_Write_CRLF
  170. ret
  171.  
  172. ; Write a single ascii character to the screen,
  173. ; anyhting lower that ASCII 33 is written as "_" to avoid special chars
  174. ; INPUT: AL will be written or replaced with a "_"
  175. Console_Write_ASCII:
  176. mov ah, 0eh
  177. cmp al, 33d
  178. js WriteUnderscore
  179. int 10h
  180. ret
  181. WriteUnderscore:
  182. mov al, 5fh
  183. int 10h
  184. ret
  185.  
  186. ; INPUT: BX is the 16Bit value to be written as Hex
  187. Console_WriteAddress_16:
  188. mov cx, 4
  189. mov dx, bx
  190. sub dx, 0D000h ; Sub the address, to get the offset into the buffer
  191. jmp Console_WriteHex_loop
  192. ; INPUT: BX is the 8Bit value to be written as Hex
  193. Console_WriteHex_8:
  194. mov cx, 2 ; this will be called more, so we let
  195. mov dx, [bx] ; it fall through to the write loop to avoid using JMP
  196. ror dx, 8 ;Correct for endianness
  197. Console_WriteHex_loop:
  198. rol dx, 4
  199. mov si, dx
  200. and si, 000Fh
  201. mov al, byte[si + HexChars]
  202. mov ah, 0Eh
  203. int 10h
  204. loop Console_WriteHex_loop
  205. ; Append a space
  206. mov al, 32d
  207. int 10h
  208. ret
  209.  
  210. ror dx, 8
  211. jmp Console_WriteHex_loop
  212.  
  213. HexChars db '0123456789ABCDEF'
  214.  
  215. ; Write an INT as a string
  216. ;INPUT: BX is the int to be written
  217. Console_Write_INT:
  218. mov ax, bx
  219. mov si, write_int_msg + 4 ; point SI to the start of the string message
  220.  
  221. ; the loop always occurs at least once as with a do-while
  222. Console_Write_INT_Repeat:
  223. xor dx, dx ; Divide by ten
  224. mov cx, 10 ; V
  225. div cx ; Divide by ten
  226.  
  227. add dl, 48
  228. mov [si], dl ; Move dl into the loc pointed to by SI
  229. dec si ; Decrement location pointed to by SI
  230. cmp ax, 0 ; check if ax is zero and set ZF to one
  231. jne Console_Write_INT_Repeat ; if ZF is one jump
  232. inc si
  233. call Console_WriteLine_16
  234. ret
  235.  
  236. write_int_msg: db ' ', 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement