PureBasic Forum
http://forums.purebasic.com/english/

Debugging assembler code...
http://forums.purebasic.com/english/viewtopic.php?f=35&t=67618
Page 1 of 1

Author:  Michael Vogel [ Sat Jan 28, 2017 2:26 pm ]
Post subject:  Debugging assembler code...

Hm, the internal debugger is not perfect for debugging assembler code - I would like to be able to switch between hex and decimal values at least (which would be also fine for the constant viewer)...

I am using the following for checking (32 bit) assembler code, does anyone else have a better tool (integrated into the PureBasic environment)?

Code:
Global *a,*b
Global ma.i,mb,i

; Define ShowAsm

   #Hex=1

   EnableASM

   #Asm=0
   #F=20
   #R=65+#Hex!1*10
   #I=120
   #X=905
   #Y=300

   Global AsmInfo.s
   Global AsmMem=@mb

   OpenWindow(#Asm,0,0,#X,#Y,"ASM Information")
   ListIconGadget(#Asm,0,0,#X,#Y,"Info",#I)
   AddGadgetColumn(#Asm,1,"C",#F)
   AddGadgetColumn(#Asm,2,"P",#F)
   AddGadgetColumn(#Asm,3,"A",#F)
   AddGadgetColumn(#Asm,4,"Z",#F)
   AddGadgetColumn(#Asm,5,"S",#F)
   AddGadgetColumn(#Asm,6,"D",#F)
   AddGadgetColumn(#Asm,7,"O",#F)
   AddGadgetColumn(#Asm,8,"EAX",#R)
   AddGadgetColumn(#Asm,9,"EBX",#R)
   AddGadgetColumn(#Asm,10,"ECX",#R)
   AddGadgetColumn(#Asm,11,"EDX",#R)
   AddGadgetColumn(#Asm,12,"EDI",#R)
   AddGadgetColumn(#Asm,13,"ESI",#R)
   AddGadgetColumn(#Asm,14,"EBP",#R)
   AddGadgetColumn(#Asm,15,"ESP",#R)
   AddGadgetColumn(#Asm,16,"Mem Info",#I)

   Macro WaitKey
      Repeat
         Select WaitWindowEvent()
         Case #WM_CHAR
            If EventwParam()=#ESC : End : Else : Break : EndIf
         Case #PB_Event_CloseWindow
            End
         EndSelect
      ForEver
   EndMacro
   Macro OX(number)
      RSet(Hex(number&$FFFFFFFF),8,"0")
   EndMacro
   Macro ShowAsm(Text)

      Global VarAX.l,VarBX.l,VarCX.l,VarDX.l,VarDI.l,VarSI.l,VarBP.l,VarSP.l
      Global gg,gn,FA.b,FC.b,FD.b,FO.b,FP.b,FS.b,FZ.b

      !PUSHFD
      !PUSHAD
      !MOV [v_VarAX],eax
      !MOV [v_VarBX],ebx
      !MOV [v_VarCX],ecx
      !MOV [v_VarDX],edx
      !MOV [v_VarDI],edi
      !MOV [v_VarSI],esi
      !MOV [v_VarBP],ebp
      !MOV [v_VarSP],esp
      !PUSHFD
      !POP eax
      !MOV [v_FC],0
      !SHR eax,1
      !ADC [v_FC],0
      !MOV [v_FP],0;      Parity-Flag
      !SHR eax,2
      !ADC [v_FP],0
      !MOV [v_FA],0;      Auxiliary-Flag
      !SHR eax,2
      !ADC [v_FA],0
      !MOV [v_FZ],0;      Zero-Flag
      !SHR eax,2
      !ADC [v_FZ],0
      !MOV [v_FS],0;      Sign-Flag
      !SHR eax,1
      !ADC [v_FS],0
      !MOV [v_FD],0;      Direction-Flag
      !SHR eax,3
      !ADC [v_FD],0
      !MOV [v_FO],0;      Overflow-Flag
      !SHR eax,1
      !ADC [v_FO],0

      Delay(1)
      AsmInfo=Text+#LF$+Str(FC)+#LF$+Str(FP)+#LF$+Str(FA)+#LF$+Str(FZ)+#LF$+Str(FS)+#LF$+Str(FD)+#LF$+Str(FO)+#LF$
      CompilerIf #Hex
         AsmInfo+Ox(VarAX)+#LF$+Ox(VarBX)+#LF$+Ox(VarCX)+#LF$+Ox(VarDX)+#LF$+Ox(VarDI)+#LF$+Ox(VarSI)+#LF$+Ox(VarBP)+#LF$+Ox(VarSP)+#LF$
      CompilerElse
         AsmInfo+Str(VarAX)+#LF$+Str(VarBX)+#LF$+Str(VarCX)+#LF$+Str(VarDX)+#LF$+Str(VarDI)+#LF$+Str(VarSI)+#LF$+Str(VarBP)+#LF$+Str(VarSP)+#LF$
      CompilerEndIf

      If AsmMem
         AsmInfo+Ox(AsmMem)+"="+Ox(PeekL(AsmMem))
      Else
         AsmInfo+Text
      EndIf

      AddGadgetItem(#Asm,-1,AsmInfo)
      If gg
         If gg&1 : SetGadgetItemColor(#Asm,gg,#PB_Gadget_BackColor,$f0e0e0) : EndIf
         For gn=1 To 15+Bool(AsmMem)
            If GetGadgetItemText(#Asm,gg,gn)<>GetGadgetItemText(#Asm,gg-1,gn)
               SetGadgetItemColor(#Asm,gg,#PB_Gadget_BackColor,#Yellow,gn)
            EndIf
         Next gn
      EndIf
      gg+1

      Waitkey
      !POPAD
      !POPFD

   EndMacro

; EndDefine

Procedure.l GifCopyPattern(*ct,*ptr_output_buffer)

   ShowAsm("init")
   !mov ecx,[p.p_ct]
   ShowAsm("mov ecx,[p.p_ct]")
   !mov edx,[p.p_ptr_output_buffer]
   ShowAsm("mov edx,[p.p_ptr_output_buffer]")
   !movzx eax,word [ecx+8]
   ShowAsm("movzx eax,word [ecx+8]")
   !add [edx],eax
   ShowAsm("add [edx],eax")
   !mov edx,[edx]
   ShowAsm("mov edx,[edx]")
   !gifdecoder_copypattern_loop:
   ShowAsm(":")
   !sub edx,4
   ShowAsm("sub edx,4")
   !mov eax,[ecx+4]
   ShowAsm("mov eax,[ecx+4]")
   !mov [edx],eax
   ShowAsm("mov [edx],eax")
   !mov ecx,[ecx]
   ShowAsm("mov ecx,[ecx]")
   !and ecx,ecx
   ShowAsm("and ecx,ecx")
   !jnz gifdecoder_copypattern_loop
   ShowAsm("done")
   
   ProcedureReturn

EndProcedure

*a=@ma
*b=@mb

ma=0
mb=$4

GifCopyPattern(*a,*b)
WaitKey

Author:  Keya [ Sat Jan 28, 2017 2:31 pm ]
Post subject:  Re: Debugging assembler code...

x64dbg and Ollydbg are friendly and helping me learn a lot and solve problems, they're not PB IDE-integrated but then i guess nothing else is! i dont really find integration integral though for working on an isolated piece of asm. and its superfast to get straight to where you want to be with !int3

Author:  Thorium [ Wed Feb 01, 2017 10:10 am ]
Post subject:  Re: Debugging assembler code...

Keya wrote:
x64dbg and Ollydbg are friendly and helping me learn a lot and solve problems, they're not PB IDE-integrated but then i guess nothing else is! i dont really find integration integral though for working on an isolated piece of asm. and its superfast to get straight to where you want to be with !int3

Thats exactly how i debug asm code in PB. So +1

Author:  Michael Vogel [ Wed Feb 01, 2017 7:59 pm ]
Post subject:  Re: Debugging assembler code...

Thanks, good choice for doing things outside PureBasic.

My hope is, that also the IDE's internal "Asm Debugger" will get an upgrade. Actually, it is nearly useless and the trivial example above gives already much more information for doing first debugging steps...

Author:  jas [ Sat Sep 16, 2017 1:38 am ]
Post subject:  Re: Debugging assembler code...

OR if there was a way to get debug information emitted so other debuggers can be used. This has been the reason I stopped using PureBasic over and over the past decade.

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/