Low level debugging using VGA and serial ports

I often find myself reinventing low-level assembly code snippets to help debug bootstrap code and other strange system situations. This page is currently in miserable condition but I hope to iteratively improve it until it contains all the little tricks I’d like to be able to just paste into my current project.

Here are a few that I find useful.

Serial

.macro HexPrintAL
call reg2hex
ror $8, %eax
push %eax
call outchar // not shown here; should send one character to serial port; readily implemented based on SayASM
pop %eax

rol $8, %eax
push %eax
call outchar
pop %eax
.endm

——————————————————————————-
.macro SayEAX
pushal

push %eax
rol $8, %eax
HexPrintAL
pop %eax

push %eax
rol $16, %eax
HexPrintAL
pop %eax

push %eax
rol $24, %eax
HexPrintAL
pop %eax

push %eax
HexPrintAL
pop %eax

popal
.endm

——————————————————————————-
// this clobbers EAX and EDX but does not touch the data or stack segments
.macro SayAsm // output a fixed character to the serial port; useful as "I'm alive"
mov $0x3fd, %edx // control register for com1
1: in (%dx), %al // read status into AL
test $0x20, %al // check for ready
je 1b // keep trying
mov $0x3f8, %edx // data register for com1
mov $0x40, %al // output an @ character
out %al, (%dx)
.endm

——————————————————————————-
reg2hex: // convert byte in al to ascii equivalent in AX, e.g., 0x12 -> 0x3132
mov %al, %ah // duplicate byte
and $0x0f, %al // keep only low nibble
shr $4, %ah // shift right to access high nibble
and $0x0f, %ah // keep only low nibble
low:
cmp $0xa, %al
jge lowletter
lowdigit:
add $0x30, %al // convert digit from numerical value to ASCII
jmp high
lowletter:
add $0x57, %al // convert digit from numerical value to ASCII
high:
cmp $0xa, %ah
jge highletter
highdigit:
add $0x30, %ah
ret
highletter:
add $0x57, %ah // - 0xa + 'a'
ret

——————————————————————————-

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s