' ------------------------------------------------------------------
' Declarations to use DisAsm.DLL by Vanja Fuckar with PowerBASIC
' (see: http://web.vip.hr/inga.vip/test.htm)
'
' Free code by Marco Pontello - 2004
' (Types & Declare translated form the VB examples included with DisAsm.DLL)
' ------------------------------------------------------------------#DIM ALL
TYPE RegType
Reg_Kind AS BYTE
' 1=8 bits \ 2=16 bits \ 3=32 bits \ 4=MMX \ 5=XMM \ 6=Float stack
' 7=Segment \ 8=Debug \ 9=Control \ 10=Test
Reg_Ptr_Kind AS BYTE
' 1=Byte PTR \ 2=Word PTR \ 3=Dword PTR \ 4=Qword PTR \ 5=mmword ptr
' 6=xmmword PTR \ 7=FWord PTR \ 8=tbyte PTR \ 9=NULL PTR (LEA)
Reg_Type AS BYTE
' 0-7= direct register index \ 16 register=byte && 7
' 32 REGISTER=(BYTE && 63)/8 \ 64=[32/16 address only] \ 128=[USING x86 relatives]
Reg_BaseAsReg AS BYTE
' 1=Register only (BASE exposed)!
END TYPE
TYPE RegStruct
Seg_Type AS LONG
Bas AS LONG
Index AS LONG
Scale AS LONG
Displacements AS LONG
Displacement_Type AS LONG
Reg_Kind AS RegType
Ptr_Type AS LONG
END TYPE
TYPE ImmStruct
Value_Lo AS LONG
Value_Hi AS LONG
Value_Type AS LONG
' 1=byte \ 2=word \ 4=dword \ 8=bytetoword \ 16=bytetodword
' 32=absjump \ 64=shortjump \ 128=longjump
END TYPE
TYPE DisAsmStruct
Instruction_Prefix AS LONG
Instruction AS LONG
Reg1 AS RegStruct
Reg2 AS RegStruct
Reg_Reg AS LONG
' 1=from ptr
Imm AS ImmStruct
Instruction_Length AS LONG
END TYPE
DECLARE FUNCTION Disasm LIB "disasm.dll" ALIAS "DisAssemble"_
(pOpCodesBuffer AS LONG,_
BYVAL pVAddress AS LONG,_
pDisBuffer AS LONG,_
pDisAsmS AS DisAsmStruct,_
BYVAL pMode AS LONG) AS LONG
' Equates for pMode: OR desired options
%DISASM_SHOWOPCODES = 1
%DISASM_SHOWADDRESS = 2
' ------------------------------------------------------------------
' This sample use DisAsm to dissassemble a block of PowerBASIC code
' between two labels. Useful to see how the compiler works
' ------------------------------------------------------------------
FUNCTION PBMAIN
DIM CodeBlock AS STRING
DIM i AS LONG, c AS LONG
CodeBlock$ = PEEK$(CODEPTR(Label1), CODEPTR(Label2) - CODEPTR(Label1))
STDOUT "Dump:"
PRINT
FOR i = CODEPTR(Label1) TO CODEPTR(Label2) -1
c = c + 1
IF c = 17 THEN
c = 1
PRINT
END IF
STDOUT RIGHT$("00" + HEX$(PEEK(i)), 2) & " ";
NEXT i
PRINT
PRINT
DIM DisBlock AS STRING
DIM Address AS LONG
DIM Offset AS LONG
DIM DisLen AS LONG
DIM DisA AS DisAsmStruct
DisBlock$ = SPACE$(128)
Address = CODEPTR(Label1)
OffSet = 0
DO
STDOUT HEX$(Address + OffSet, 7) & "h: ";
IF Offset => LEN(CodeBlock$) THEN
STDOUT
EXIT DO
END IF
DisLen = DisAsm(BYVAL STRPTR(CodeBlock$) + OffSet, Address + OffSet,_
BYVAL STRPTR(DisBlock$), DisA, 0) ' 0 = Disassembled only
STDOUT LEFT$(DisBlock$, DisLen)
OffSet = OffSet + DisA.Instruction_Length
LOOP
STDOUT
STDOUT "-- Finished!"
WAITKEY$
EXIT FUNCTION
' ------------------------------------------------------------------
' Code to disassemble
Label1:
' ------------------------------
REGISTER lA AS LONG
FOR lA = 1 TO 1000
IF lA = > 50 AND lA < 52 THEN
! NOP
END IF
NEXT la
' ------------------------------
Label2:
END FUNCTION