Skip to content

Instantly share code, notes, and snippets.

@catilac
Created May 29, 2020 14:02
Show Gist options
  • Save catilac/99bb36c03e0f4f2ee076436bc72bbc60 to your computer and use it in GitHub Desktop.
Save catilac/99bb36c03e0f4f2ee076436bc72bbc60 to your computer and use it in GitHub Desktop.
Bootsector that prints hex
;
; String printing boot sector
;
[org 0x7c00] ; Tell the assembler where to load code
mov dx, 0x1fb6 ; store value to print in dx
call print_hex ; call the function
jmp $ ; hang
%include "print_string.asm"
%include "print_hex.asm"
; Data
HEX_OUT: db '0x0000', 0
; Padding and magic number
times 510-($-$$) db 0
dw 0xaa55
;
; print_hex.asm
;
; prints the value of DX as hex
print_hex:
pusha
mov bx, HEX_OUT ; store pointer to hex value
add bx, 0x5 ; move pointer to end of string
loop:
mov cl, dl ; copy dx value into cl
cmp cl, 0 ; check if 0
je end_loop ; if 0 then we're done
and cl, 0xF ; mask first half byte
cmp cl, 0xA ; compare to A
jl lt_A ; if < A go to lt_A
jmp gte_A ; if >= A go to gte_A
lt_A: ; add 48
add cl, 48 ; add 48 to bring it to ascii '0'
jmp end_cmp ; go to end of this code block
gte_A: ; add 55
add cl, 55 ; add 55 to bring it to ascii 'A'
jmp end_cmp ; go to end of this code block
end_cmp:
mov [bx], cl ; put value into dereferenced pointer
sub bx, 1 ; decrement pointer
shr dx, 4 ; shift right 4 bits to get the next half byte
jmp loop ; go back to start of loop
end_loop:
mov bx, HEX_OUT ; print the string pointed to
call print_string ; by BX
popa
ret
; print_string.asm
print_string:
pusha ; store all register values on stack
mov ah, 0x0e ; BIOS tele-type output
while:
mov al, [bx]
cmp al, 0 ; check for null value
je end_while ; jump to end if null
int 0x10 ; Output value in al
add bx, 1 ; Increment pointer
jmp while ; jump to emit loop
end_while:
popa ; restore all register values
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment