News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Handy debugging macro.

Started by Damos, September 16, 2009, 10:25:07 AM

Previous topic - Next topic

Damos

This is a 64 bit debugging macro i've been working on(fasm syntax, sorry):
section '.data' data readable writeable
tempRSP dq ?
wantexit dq 0
table db "RAX  %016X",10,0
table2 db "RBX  %016X",10,0
db "RCX  %016X",10,0
db "RDX  %016X",10,0
db "RSI  %016X",10,0
db "RDI  %016X",10,0
db "RBP  %016X",10,0
db "RSP  %016X",10,0
db "R8   %016X",10,0
db "R9   %016X",10,0
db "R10  %016X",10,0
db "R11  %016X",10,0
db "R12  %016X",10,0
db "R13  %016X",10,0
db "R14  %016X",10,0
db "R15  %016X",10,0
flags db "O D S Z A P C",10,0
fres db "             ",10,0

macro debug
{
pushf
.if [wantexit] =0
mov [tempRSP],RSP
push r15 r14 r13 r12 r11 r10 r9 r8 [tempRSP] rbp rdi rsi rdx rcx rbx rax
mov rcx,STD_OUTPUT_HANDLE
call [GetStdHandle]
mov rcx,rax
xor rdx,rdx
call [SetConsoleCursorPosition]
mov rdi,rsp
mov r15,rsp
sub rsp,28h
and rsp,-10h
mov rsi,table
mov rbp,16
.repeat
mov rcx,rsi
mov rdx,[rdi]
call [printf]
add rsi,table2-table
add rdi,8
dec rbp
.until ZERO?
mov ecx,flags
call [printf]
mov rax,[rdi]
test rax,1
.if ZERO?
mov [fres+12],' '
.else
mov [fres+12],'^'
.endif
test rax,1 shl 2
.if ZERO?
mov [fres+10],' '
.else
mov [fres+10],'^'
.endif
test rax,1 shl 4
.if ZERO?
mov [fres+8],' '
.else
mov [fres+8],'^'
.endif
test rax,1 shl 6
.if ZERO?
mov [fres+6],' '
.else
mov [fres+6],'^'
.endif
test rax,1 shl 7
.if ZERO?
mov [fres+4],' '
.else
mov [fres+4],'^'
.endif
test rax,1 shl 10
.if ZERO?
mov [fres+2],' '
.else
mov [fres+2],'^'
.endif
test rax,1 shl 11
.if ZERO?
mov [fres],' '
.else
mov [fres],'^'
.endif
mov ecx,fres
call [printf]
call [_getch]
.if al=1bh
inc [wantexit]
call [PostQuitMessage]
.endif
mov rsp,r15
pop rax rbx rcx rdx rsi rdi rbp r8 r8 r9 r10 r11 r12 r13 r14 r15
mov rsp,[tempRSP]
.endif
popf
}


I've found it very useful so far.
If you want to try it out you need the cruntime library and you need to assemble as a console proggy (even if it is GUI).
What you basically get is a console with all the registers and flags displaying there values at the point at which the macro is inserted, press any key and it will continue running. Press escape and it executes a PostQuitMessage() and then ignores itself thereafter, Exiting the program.
Thinking of showing the contents of each register next to the register value first, maybe with a macro argument that will give you ascii, raw bytes, or floats etc.
Sorry my code is in fasm syntax instead of masm. but, I just love fasm and the very limited experience I had with ML64 put me right off. Should be easy to port though.
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction. - Albert Einstien