Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Various sub-routines that will be useful to the boot loader code
- ; Take a single value for input from a key
- ; OUTPUT in AL
- TakeInput:
- call Console_Read_Key
- cmp al, 48
- js Error_invalid_Input
- cmp al, 58
- jns Error_invalid_Input
- mov ah, 0Eh
- int 10h
- sub al, 48
- push ax
- call Console_Write_CRLF
- pop ax
- ret
- ; Takes key values and stores into a 4 character input buffer
- TakeInputToBuffer:
- mov si, inputbuffer
- TakeInputToBufferLoop:
- push cx
- call Console_Read_Key
- cmp al, 48 ; If sign flag sets, we know number is out of low bound
- js Error_invalid_Input ; we also now have the decimal value of the input
- cmp al, 58
- jns Error_invalid_Input ; Since we subtracted AND passed the previous test, we need to
- mov ah, 0Eh ; print the typed character
- int 10h
- sub al, 48 ; convert to decimal
- mov [si], al ; store in buffer
- inc si ; increment character pointer
- pop cx
- inc cx
- cmp cx, 4
- jnz TakeInputToBufferLoop
- push ax
- call Console_Write_CRLF ; drop line for formatting
- pop ax
- ret
- ; Take a sector number and write it after the word "SECTOR"
- ; INPUT: the values stored in the input buffer
- Console_Write_SecName:
- push ax
- mov si, sector
- call Console_Write_16
- ret
- sector db 'Sector: ', 0
- inputbuffer db '0000', 0
- ; Parse input buffer contents into AX as an INT
- ; This is an unrolled loop, to avoid incrementing and adding a bunch
- ParseIntFromBuffer:
- mov si, inputbuffer
- mov bx, 0 ; RESET AX TO ZERO
- mov ax, [si] ; Make ax the first value in the buffer
- inc si ; move the buffer pointer
- mov dx, 1000
- mul dx ; multiply by 1000 because this is char 1 of a 4 digit decimal
- add bx, ax
- ; Repeat but use BX so we can accumulate results
- mov ax, [si]
- inc si
- mov dx, 100
- mul dx
- add bx, ax
- mov ax, [si]
- inc si
- mov dx, 10
- mul dx
- add bx, ax
- mov ax, bx
- add bx, [si] ; last value, multipl by 1 == just adding the number
- ret
- ; Writes a sector out 16 lines at a time
- ; outer loop will write address offset and load counter
- ; inner loop will print out the data as hex, then print a CRLF
- ; ASCII loop will print the data as ASCII
- Console_WriteSector:
- push ax
- OuterWriteLoop:
- mov bx, 0D000h ;reset the offset after reading
- mov cx, word [bpbBytesPerSector] ;LOOP COUNTER FOR SECTOR
- OuterLineLoop:
- cmp cx, 256d
- je Continue
- ReturnBlock:
- push cx ;push sector counter
- call Console_WriteAddress_16
- mov cx, 16d ;LOOP COUNTER FOR LINE
- HexLoop:
- push cx ;push the line loop counter
- call Console_WriteHex_8
- add bx, 1
- pop cx ;pop the line loop counter
- dec cx
- jnz HexLoop
- mov cx, 16d ;set ascii loop counter
- sub bx, 16d
- ASCIILoop:
- push cx ;should push the asci loop counter
- mov ax, [bx]
- call Console_Write_ASCII
- inc bx
- pop cx ;should pop the asii loop counter
- dec cx
- jnz ASCIILoop
- call Console_Write_CRLF
- pop cx ;pop the SECTOR counter
- sub cx, 16d
- jnz OuterLineLoop
- pop ax
- ret
- Continue:
- mov si, continue_msg
- call Console_WriteLine_16
- call Console_Read_Key
- jmp ReturnBlock
- ; Wait to read a key scan code
- ; Store it in AH
- Console_Read_Key:
- mov ah, 00h
- int 16h
- ret
- ; Output Carriage-Return/Line-Feed (CRLF) sequence to screen using BIOS
- Console_Write_CRLF:
- mov ah, 0Eh ; Output CR
- mov al, 0Dh
- int 10h
- mov al, 0Ah ; Output LF
- int 10h
- ret
- ; Write to the console using BIOS.
- ;
- ; Input: SI points to a null-terminated string
- Console_Write_16:
- mov ah, 0Eh ; BIOS call to output value in AL to screen
- Console_Write_16_Repeat:
- lodsb ; Load byte at SI into AL and increment SI
- test al, al ; If the byte is 0, we are done
- je Console_Write_16_Done
- int 10h ; Output character to screen
- jmp Console_Write_16_Repeat
- Console_Write_16_Done:
- ret
- ; Write string to the console using BIOS followed by CRLF
- ;
- ; Input: SI points to a null-terminated string
- Console_WriteLine_16:
- call Console_Write_16
- call Console_Write_CRLF
- ret
- ; Write a single ascii character to the screen,
- ; anyhting lower that ASCII 33 is written as "_" to avoid special chars
- ; INPUT: AL will be written or replaced with a "_"
- Console_Write_ASCII:
- mov ah, 0eh
- cmp al, 33d
- js WriteUnderscore
- int 10h
- ret
- WriteUnderscore:
- mov al, 5fh
- int 10h
- ret
- ; INPUT: BX is the 16Bit value to be written as Hex
- Console_WriteAddress_16:
- mov cx, 4
- mov dx, bx
- sub dx, 0D000h ; Sub the address, to get the offset into the buffer
- jmp Console_WriteHex_loop
- ; INPUT: BX is the 8Bit value to be written as Hex
- Console_WriteHex_8:
- mov cx, 2 ; this will be called more, so we let
- mov dx, [bx] ; it fall through to the write loop to avoid using JMP
- ror dx, 8 ;Correct for endianness
- Console_WriteHex_loop:
- rol dx, 4
- mov si, dx
- and si, 000Fh
- mov al, byte[si + HexChars]
- mov ah, 0Eh
- int 10h
- loop Console_WriteHex_loop
- ; Append a space
- mov al, 32d
- int 10h
- ret
- ror dx, 8
- jmp Console_WriteHex_loop
- HexChars db '0123456789ABCDEF'
- ; Write an INT as a string
- ;INPUT: BX is the int to be written
- Console_Write_INT:
- mov ax, bx
- mov si, write_int_msg + 4 ; point SI to the start of the string message
- ; the loop always occurs at least once as with a do-while
- Console_Write_INT_Repeat:
- xor dx, dx ; Divide by ten
- mov cx, 10 ; V
- div cx ; Divide by ten
- add dl, 48
- mov [si], dl ; Move dl into the loc pointed to by SI
- dec si ; Decrement location pointed to by SI
- cmp ax, 0 ; check if ax is zero and set ZF to one
- jne Console_Write_INT_Repeat ; if ZF is one jump
- inc si
- call Console_WriteLine_16
- ret
- write_int_msg: db ' ', 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement