*Pb assembly debugger..

Bare metal programming in PureBasic, for experienced users
User avatar
CELTIC88
Enthusiast
Enthusiast
Posts: 147
Joined: Thu Sep 17, 2015 3:39 pm

*Pb assembly debugger..

Post by CELTIC88 »

hi all

I am a beginner in assembly and I created a debugger for assembly in pb

correct me if I'm wrong!

*Update

Code: Select all

EnableExplicit

CompilerIf #PB_Compiler_Debugger = 1  
  Procedure _Dasm_Debug(__asmDebug, Type,Size)
    Select Type
      Case 1
        Debug __asmDebug
      Case 2
        Debug PeekA(@__asmDebug)
      Case 3
        Debug PeekU(@__asmDebug)
      Case 4
        Debug PeekS(__asmDebug)
      Case 5
        Debug PeekS(__asmDebug,-1,#PB_Ascii)
      Case 6
        ShowMemoryViewer(__asmDebug,Size)
      Case 7
        Protected Flags.s = RSet(Bin(__asmDebug,#PB_Long), 32, "0")
        Protected AllFlags.s = ";;;;;;;;;;ID;VIP;VIF;AC;VM;RF;;NT;IOPL_h;IOPL_l;OF;DF;IF;TF;SF;ZF;;AF;;PF;;CF"
        Protected s,sf.s,rets.s
        For s = 1 To 32
          If Mid(Flags,s,1) = "1"
            sf = StringField(AllFlags,s,";")
            If sf
              rets = "[" + sf + "]"  + " " + rets
            EndIf
          EndIf
        Next
        Debug "Flags : " + rets
      Case 8
        
        EnableASM
        SUB eax, eax
        MOV eax, CS
        MOV dword [p.v___asmDebug] , eax
        Debug "[CS = " + Hex(__asmDebug) + "]"
        MOV eax, DS
        MOV dword [p.v___asmDebug] , eax
        Debug "[DS = " + Hex(__asmDebug) + "]"
        MOV eax, SS
        MOV dword [p.v___asmDebug] , eax
        Debug "[SS = " + Hex(__asmDebug) + "]"
        MOV eax, ES
        MOV dword [p.v___asmDebug] , eax
        Debug "[ES = " + Hex(__asmDebug) + "]"
        MOV eax, FS
        MOV dword [p.v___asmDebug] , eax
        Debug "[FS = " + Hex(__asmDebug) + "]"
        MOV eax, GS
        MOV dword [p.v___asmDebug] , eax
        Debug "[GS = " + Hex(__asmDebug) + "]"
        DisableASM
        
      Case 9
        Debug "Point code : "  + Str(__asmDebug)
    EndSelect
  EndProcedure
  
  Global __Dasm_Debug = @_Dasm_Debug()
CompilerEndIf

Macro Dasm_Save_SFPU_MXCSR_SSE
  PUSH    ebp
  MOV     ebp,esp
  SUB     esp,512
  AND     esp,-16
  !fxsave  [esp] ;saves the current state of the FPU, MXCSR register, and all the FPU AND SSE registers
EndMacro

Macro Dasm_Reload_SFPU_MXCSR_SSE
  !fxrstor [esp]
  MOV     esp,ebp
  POP     ebp
EndMacro

Macro Dasm_Debug(reg,Type,Size=0)
  CompilerIf #PB_Compiler_Debugger = 1  
    PUSHFD
    PUSHA
    MOV eax, reg
    PUSH dword Size
    PUSH dword Type
    PUSH eax
    CALL [v___Dasm_Debug]
    POPA
    POPFD
  CompilerEndIf
EndMacro

Macro Dasm_ShowAssembly()
  CompilerIf #PB_Compiler_Debugger = 1
    PUSHFD
    PUSHA
    EnableDebugger
    ShowAssemblyViewer()
    DisableDebugger
    POPA
    POPFD
  CompilerEndIf
EndMacro   ;call ShowAssembly 

Macro Dasm_Pause()
  CompilerIf #PB_Compiler_Debugger = 1  
    PUSHFD
    PUSHA
    EnableDebugger
    CallDebugger
    DisableDebugger
    POPA
    POPFD
  CompilerEndIf
EndMacro  ;CallDebugger 

Macro Dasm_Flags()
  CompilerIf #PB_Compiler_Debugger = 1  
    PUSHFD
    PUSHA
    MOV eax,[esp + 4*8]
    PUSH dword 0
    PUSH dword 7
    PUSH eax
    CALL [v___Dasm_Debug]
    POPA
    POPFD
  CompilerEndIf
EndMacro

Macro Dasm_ShowText(Txt)
  CompilerIf #PB_Compiler_Debugger = 1  
    PUSHFD
    PUSHA
    EnableDebugger
    Debug Txt
    DisableDebugger
    POPA
    POPFD
  CompilerEndIf
EndMacro

Macro Dasm_SegmentRegisters()
  CompilerIf #PB_Compiler_Debugger = 1  
    PUSHA
    PUSH dword 0
    PUSH dword 8
    PUSH dword 0
    CALL [v___Dasm_Debug]
    POPA
  CompilerEndIf
EndMacro

Macro Dasm_NextInstructionPointer() ;EIP
  CompilerIf #PB_Compiler_Debugger = 1  
    PUSH eax
    MOV eax , @f
    
    PUSHA
    PUSH dword 0
    PUSH dword 9
    PUSH eax
    CALL [v___Dasm_Debug]
    POPA
    
    POP eax
    !@@:
  CompilerEndIf
EndMacro

Macro Dasm_Byte(reg):Dasm_Debug(reg,2):EndMacro    ;debug byte
Macro Dasm_Word(reg):Dasm_Debug(reg,3):EndMacro    ;debug word
Macro Dasm_long(reg):Dasm_Debug(reg,1):EndMacro    ;debug long
Macro Dasm_StringU(reg):Dasm_Debug(reg,4):EndMacro ;debug string unicode
Macro Dasm_StringA(reg):Dasm_Debug(reg,5):EndMacro ;debug ascii
Macro Dasm_ShowMemory(reg,Size):Dasm_Debug(reg,6,Size):EndMacro   ;call ShowMemory 


Procedure __test(par)  
  EnableASM
  DisableDebugger ;Très important
  
  Dasm_ShowText("Asm code debugging started.")
  
  Dasm_ShowText("Asm Segment Registers:")
  Dasm_SegmentRegisters()
  
  MOV ecx, esp
  
  MOV eax ,[ecx + 4]
  
  PUSH eax
  XOR eax,eax
  
  ADD eax,2147483647
  
  Dasm_ShowText("Asm Next Instruction Pointer:")
  Dasm_NextInstructionPointer()
  ADD eax,1
  
  Dasm_ShowText("Asm Show Flags:")
  Dasm_Flags() ;overflow!
  
  Dasm_ShowAssembly()
  Dasm_ShowText(">> (Asm Pause) <<")
  Dasm_Pause()
  
  Dasm_ShowText("Asm Show EAX:")
  Dasm_long(eax)
  
  POP eax
  Dasm_ShowText("Asm Show 'STRING ASCII' EAX:")
  Dasm_StringA(eax)  
  
  Dasm_ShowText("Asm Dump Stack.")
  Dasm_ShowMemory(ecx,16)
  
  DisableASM
  EnableDebugger
EndProcedure

__test(Ascii("celtic"))

Last edited by CELTIC88 on Tue Jan 16, 2018 5:38 pm, edited 4 times in total.
interested in Cybersecurity..
Fred
Administrator
Administrator
Posts: 14296
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Pb assembly debugger..

Post by Fred »

You can also use the 'assembler' debug window in the debug IDE menu, when you are breaked, it should show all register content and flags
User avatar
CELTIC88
Enthusiast
Enthusiast
Posts: 147
Joined: Thu Sep 17, 2015 3:39 pm

Re: Pb assembly debugger..

Post by CELTIC88 »

thank you @Fred :)
interested in Cybersecurity..
User avatar
CELTIC88
Enthusiast
Enthusiast
Posts: 147
Joined: Thu Sep 17, 2015 3:39 pm

Re: *Pb assembly debugger..

Post by CELTIC88 »

update.
interested in Cybersecurity..
Post Reply