Skip to content

Instantly share code, notes, and snippets.

@therealdreg
Last active July 27, 2023 19:13
Show Gist options
  • Save therealdreg/8318a8666b5b1ae6c56274514b3640ff to your computer and use it in GitHub Desktop.
Save therealdreg/8318a8666b5b1ae6c56274514b3640ff to your computer and use it in GitHub Desktop.
Bochs masm32 macros for kernel debugging, magic breakpoint, E9 port hack, 8A00h 08AE0h
include \masm32\macros\macros.asm
include \masm32\include\masm32.inc
BochsPrintPW macro arg:VARARG
; https://c9x.me/x86/html/file_module_x86_id_222.html
; port e9 hack https://bochs.sourceforge.io/doc/docbook/user/bochsrc.html#AEN2523
nop
push eax
push edx
mov dx, 0e9h
FOR j,<arg>
IF @InStr(1,<j>,<!">) NE 0
FORC i,<j>
IFDIF <i>,<!">
mov al, @CatStr(<!'>, <i>, <!'>)
out dx, al
ENDIF
ENDM
ELSE
mov al, j
out dx, al
ENDIF
ENDM
mov al, 0ah
out dx, al
pop edx
pop eax
nop
endm
Example of use:
BochsPrintPW "Hi Dre.gggg", "eya" ,0ah, 0ffh, "woha"
-----
BochsPrint macro arg
; https://c9x.me/x86/html/file_module_x86_id_222.html
; port e9 hack https://bochs.sourceforge.io/doc/docbook/user/bochsrc.html#AEN2523
nop
push eax
push edx
mov dx, 0e9h
FORC i,<arg>
mov al, @CatStr(<!'>, <i>, <!'>)
out dx, al
ENDM
mov al, 0ah
out dx, al
pop edx
pop eax
nop
endm
Example of use:
BochsPrint <Hi Dre.gggg>
For print only a new line:
BochsPrint
-----
BochsPrintEx macro arg:VARARG
; https://c9x.me/x86/html/file_module_x86_id_222.html
; port e9 hack https://bochs.sourceforge.io/doc/docbook/user/bochsrc.html#AEN2523
nop
push eax
push edx
mov dx, 0e9h
FOR i,<arg>
mov al, i
out dx, al
ENDM
mov al, 0ah
out dx, al
pop edx
pop eax
nop
endm
Example of use:
BochsPrintEx 'H', 'i', ' ', 0ah, 'D', 'r', 'e', 'g', 0ffh, 0ah
----
BochsBP macro
; https://c9x.me/x86/html/file_module_x86_id_222.html
; https://bochs.sourceforge.io/doc/docbook/development/debugger-advanced.html
nop
push eax
push edx
mov ax, 8A00h
mov dx, 8A00h
out dx, ax
mov ax, 08AE0h
out dx, ax
pop edx
pop eax
nop
endm
Example of use:
BochsBP
----
BochsPrintBuff:
; push addr
; push bytes_to_print
; call BochsPrintBuff
; https://c9x.me/x86/html/file_module_x86_id_222.html
; port e9 hack https://bochs.sourceforge.io/doc/docbook/user/bochsrc.html#AEN2523
nop
push ebp
mov ebp, esp
push esi
push ecx
push eax
push edx
mov esi, dword ptr [ebp+0ch]
mov ecx, dword ptr [ebp+08h]
mov dx, 0e9h
cld
printdrge9:
lodsb
mov ah, al
shr al, 04h
add al, 30h
cmp al, 3Ah
jb @F
add al, 07h
@@::
out dx, al
and ah, 0fh
shr ax, 08h
add al, 30h
cmp al, 3Ah
jb @F
add al, 07h
@@::
out dx, al
loop printdrge9
mov al, 0ah
out dx, al
pop edx
pop eax
pop ecx
pop esi
leave
ret 08h
nop
Example of use:
.data
hi_dreg db "Hi Dreg!",00h, 0FFh, 90h,90h,90h,90h,90h,90h
size_hi_dreg = $ - offset hi_dreg
.code
start:
push offset hi_dreg
push size_hi_dreg
call BochsPrintBuff
Another example:
call @F
db "Hi Dreg!",00h, 0FFh, 90h,90h,90h,90h,90h,90h
@@:
call @F
@@:
pop eax
sub eax, [esp]
sub eax, 05h
push eax
call BochsPrintBuff
----
like C printf for masm32 pure assembly (for bochs e9 port hack use) supporting %c %u %x %s
out_b:
push eax
push edx
mov dx, 0e9h
mov al, [esp+0Ch]
out dx, al
pop edx
pop eax
ret 4
OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE
drgprintf PROC C arg1:DWORD, arg2:VARARG
pushad
push ebp
mov ebp, esp
lea ebx, [ebp + 8 + (8 * 4)]
mov esi, [ebx]
cld
loop_parser:
lodsb
cmp al, 0
je done
cmp al, '%'
je format
push eax
call out_b
jmp loop_parser
format:
lodsb
cmp al, 0
je done
add ebx, 4
cmp al, 'c'
jne @F
push [ebx]
call out_b
jmp loop_parser
@@:
cmp al, 'u'
jne @F
mov ecx, 10
call print_int
jmp loop_parser
@@:
cmp al, 'x'
jne @F
mov ecx, 16
call print_int
jmp loop_parser
@@:
cmp al, 's'
jne @F
mov eax, [ebx]
call print_string_byps
jmp loop_parser
print_int:
mov eax, [ebx]
push 0
mov edi, esp
sub esp, 0Ch
loop_print_int:
xor edx, edx
div ecx
add dl, '0'
cmp dl, 3Ah
jb @F
add dl, 7
@@:
dec edi
mov [edi], dl
test eax, eax
jnz loop_print_int
mov eax, edi
call print_string_byps
add esp, 10h
jmp loop_parser
print_string_byps:
@@:
mov cl, [eax]
cmp cl, 0
je @F
push ecx
call out_b
inc eax
jmp @B
@@:
ret
done:
leave
popad
ret
drgprintf ENDP
OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef
Example of use:
printf("hola drg %s adios drg %s a \n\n unsigned number: %u an hex number: %x", 69, 0x6A);
start:
push 6Ah
push 69
push offset bye_dreg
push offset hi_dreg
push offset format_string
call drgprintf
add esp, 4 * 5
db 20 dup(90h)
UCSTR format_string, "hola drg %s adios drg %s a ", 0ah, 0ah, " unsigned number: %u an hex number: %x", 0h
UCSTR hi_dreg, "Hi Dreg!", 0h
UCSTR bye_dreg, "Bye Dreg!", 0h
Another example:
start:
push 6Ah
push 69
push chr$("By Drg!", 0)
push chr$("Hi Drg!", 0)
push chr$("hola drg %s adios drg %s a ", 0ah, 0ah, " unsigned number: %u an hex number: %x", 0h)
call drgprintf
add esp, 4 * 5
Another example:
start:
fncx drgprintf, A"%c hola \n drg %s adios drg %s a unsigned number: %u an hex number: %x", 0ah, A"Hi Drg!", A"By Drg!", 69, 6AH
NOTES:
cfm$
Description
Format a quoted string using C style escape sequences.
Three versions are presented here.
1. acfm$() = ASCII only version
2. ucfm$() = UNICODE version
3. cfm$() = Either ASCII or UNICODE depending on if the __UNICODE__ equate is present in the source file.
All three versions operate in the same manner and have the same capacity.
;/********************************************************************/
;/* fncx - macro procedure */
;/* This macro enhanced the INVOKE-directive: */
;/* - It adds support for quoted ASCII or unicode strings. */
;/* The strings can be either enclosed by double quotes or by */
;/* single quotation marks. */
;/* The kind of created string (Unicode or ASCII) depends on the */
;/* __UNICODE__ equate. If this equte is defined and has a */
;/* nonzero value, a unicode string is created. However, creation*/
;/* of Unicode strings can be forced by using the 'L'-prefix: */
;/* L"my string" or L'my string' */
;/* ASCII strings can be forced by using the A-prefix: */
;/* A"my string" or A'my string' */
;/* MASM's reserved characters like exclamation marks, angel */
;/* brackets and single brackets [,...] can not be used. */
;/* (use fncx for escape sequences support) */
;/* - ADDR-expressions can be shorten by using a ampersand '&': */
;/* fn MessageBoxW,0,&wsz[0],L'xyz',0 */
;/* - Pointers to DWORDs can be dereferenced, if they are leaded */
;/* by '*' (like dereferencing in c/c++): */
;/* fnx MesageBox,0,*ppchar,... */
;/* - a optional destination can be specified in front of the */
;/* function: */
;/* fn dest=FncName,... */
;/* */
;/* Example: */
;/* fnx MessageBoxW,0,L"my string",&wsz[0],0 */
;/* fnx hWnd = CreateWindowEx,... */
;/* This macro behave like the fnx-macros, except, that it adds */
;/* support for escape sequences: */
;/* \\ -> "\" */
;/* \t -> tab */
;/* \n -> new line (13,10) */
;/* \x -> "!" */
;/* \a -> "(" */
;/* \b -> ")" */
;/* \l -> "<" */
;/* \r -> ">" */
;/* \p -> "%" */
;/* \A -> "&" */
;/* \q -> double quote '"' */
;/* \0 -> zero */
;/* Example: */
;/* fncx MessageBox,0,"my string\n",&wsz[0],0 */
;/* qWord, 2011 */
;/********************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment