Debugging assembler code...

Bare metal programming in PureBasic, for experienced users
User avatar
Michael Vogel
Addict
Addict
Posts: 2582
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Debugging assembler code...

Post by Michael Vogel »

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: Select all

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
User avatar
Keya
Addict
Addict
Posts: 1866
Joined: Thu Jun 04, 2015 7:10 am

Re: Debugging assembler code...

Post by Keya »

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
Thorium
Addict
Addict
Posts: 1268
Joined: Sat Aug 15, 2009 6:59 pm

Re: Debugging assembler code...

Post by Thorium »

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
User avatar
Michael Vogel
Addict
Addict
Posts: 2582
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Debugging assembler code...

Post by Michael Vogel »

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...
jas
New User
New User
Posts: 5
Joined: Sun Jan 26, 2014 7:32 pm

Re: Debugging assembler code...

Post by jas »

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.
Post Reply