PowerBASIC Forums
  Source Code
  Have a look at what the compiler output

Post New Topic  Post A Reply
profile | register | preferences | faq | search

UBBFriend: Email This Page to Someone! next newest topic | next oldest topic
Author Topic:   Have a look at what the compiler output
Marco Pontello
Member
posted March 03, 2004 04:42 PM     Click Here to See the Profile for Marco Pontello     Edit/Delete Message   Reply w/Quote
' ------------------------------------------------------------------
' 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

------------------
Try TrID file identifier! Over 990 filetypes and counting...
Give a powerfull scriptable Lua interface to your application with PowerBLua

[This message has been edited by Marco Pontello (edited September 15, 2006).]

IP: Logged

Marco Pontello
Member
posted March 03, 2004 04:43 PM     Click Here to See the Profile for Marco Pontello     Edit/Delete Message   Reply w/Quote
This is the output of the code above:

-----------------------------------------------
Dump:

C7 C6 01 00 00 00 83 FE 32 0F 8C 0A 00 00 00 83
FE 34 0F 8D 01 00 00 00 90 FF C6 81 FE E8 03 00
00 7E E3

04012B0h: MOV ESI,01h
04012B6h: CMP ESI,032h
04012B9h: JL 04012C9h
04012BFh: CMP ESI,034h
04012C2h: JGE 04012C9h
04012C8h: NOP
04012C9h: INC ESI
04012CBh: CMP ESI,03E8h
04012D1h: JLE 04012B6h
04012D3h:

-- Finished!
-----------------------------------------------

------------------
Try TrID file identifier! Over 990 filetypes and counting...
Give a powerfull scriptable Lua interface to your application with PowerBLua

[This message has been edited by Marco Pontello (edited March 03, 2004).]

IP: Logged

Fred Buffington
Member
posted March 04, 2004 02:40 PM     Click Here to See the Profile for Fred Buffington     Edit/Delete Message   Reply w/Quote
Looks like the start of a good hex editor. With the addition of
showing ascii code of all over chr$(31) to the right and the
ability to edit then save... the changes.

------------------
Client Writeup for the CPA

IP: Logged

Haakon Birkeland
Member
posted February 12, 2005 04:02 AM     Click Here to See the Profile for Haakon Birkeland     Edit/Delete Message   Reply w/Quote
MOV ESI,01h
CMP ESI,032h
JL 04012C9h
CMP ESI,034h
JGE 04012C9h
NOP
INC ESI
CMP ESI,03E8h
JLE 04012B6h

And it would then be possible to use the generated Assembler code inline in PB?
Would that give any benefit, speedwise? - Cause it ain't redering it more readeable..

<<Sorry, I did what I shouldn't do here and now its to late..>>

------------------
Haakon 8o)

[This message has been edited by Haakon Birkeland (edited February 12, 2005).]

IP: Logged

All times are EasternTime (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | PowerBASIC BASIC Compilers

Copyright © 1999-2006 PowerBASIC, Inc. All Rights Reserved.


Ultimate Bulletin Board 5.45c