Page 1 of 1

Learning ASM, a basic question

Posted: Wed Jul 31, 2013 5:43 pm
by ostapas
PB pushed me to learn some ASM and for obvious reasons I chose Fasm and use PB IDE and debugger for it. So far it goes not really smoothly :) Could somenone tell me, how can I define a variable using Fasm data directives and debug it using PB's Debug Output? If I do like this:

Code: Select all

! var db 255
Debug var
I just get an IMA.

Re: Learning ASM, a basic question

Posted: Wed Jul 31, 2013 7:43 pm
by luis
I'm not an ASM expert but I think it goes like this:

Debug var is trying to access the implicit declared "var" variable (since no enable explicit) and as explained in the docs that var will be internally know to PB (and fasm) as v_var, not var.
So is you remove your fasm line to prevent the crash, debug will print the value of the PB variable "var", mapped to "v_var" in fasm.

Moreover I think your "var db 255" is like poking a 255 binary number just in the middle of your PB program, in the code section. So the program will encounter a strange opcode when reaching that point and crash.

You can somewhat verify that by replacing 255 with 0x90, the opcode for NOP (no operation).

That is a valid opcode and it will be executed without crashing the program.

If I'm wrong please correct me.

Re: Learning ASM, a basic question

Posted: Wed Jul 31, 2013 8:06 pm
by ostapas
Many thanks for a reply!
You can somewhat verify that by replacing 255 with 0x90, the opcode for NOP (no operation).
I think you was right, it was executing binary data.
So is you remove your fasm line to prevent the crash, debug will print the value of the PB variable "var", mapped to "v_var" in fasm
Hm, didn't get your idea exactly, maybe you could elaborate on this a bit, if you got some spare minutes(I started to learn asm just today, so take it easy :) )

Re: Learning ASM, a basic question

Posted: Wed Jul 31, 2013 8:30 pm
by luis
Debug var for PB means, "print the value of the var v_var" (pb append a v_ to variable names, l_ to labels, etc.).

So to read from you FASM data and print through debug, you could do something like this:

Code: Select all

Define var ; this is the PB "var", it will translated to "v_var" to the ASM output to be sent to FASM

! mov eax, [var] ; this will copy your FASM "var" into a register
! mov [v_var], eax ; this will copy that value to the "v_var" the debug command can access

Debug var ; and here it is

End ; end to stop the program here

! var dd 1234567 ; your fasm data (double word to comply easily with register size)
But as I said, I'm not used to mix PB and ASM, so I'm sure you can get more appropriate answers from someone else.

Re: Learning ASM, a basic question

Posted: Wed Jul 31, 2013 8:37 pm
by ts-soft
You should change the first line of example from luis to:

Code: Select all

Define var.l
or var is an integer and can't hold the dd with x64!

Re: Learning ASM, a basic question

Posted: Wed Jul 31, 2013 8:50 pm
by ostapas
luis, ts-soft, sincere thanks, exactly what I was looking for.

Re: Learning ASM, a basic question

Posted: Wed Jul 31, 2013 10:37 pm
by idle
as a little fasm processor debug experiment it's not very useful, x64 only

Code: Select all

EnableASM 

var2 = 255 
var1 = 123 
mov rax, var2 
add rax,1
mov var2, rax

DisableASM

Global gDebug 

Macro debugASM(val)
   !  if val in <rax,rbx,rcx,rdx,rsp,rdi> 
   !     mov [v_gDebug],  val 
   !  else 
     !if defined v_#val
        ! push rax
        ! mov rax, [v_#val]
        !mov [v_gDebug], rax
        !pop rax 
    !else 
       !push rax
       !add rsp, 8
       !mov rax, [p.v_#val]
       !mov [v_gDebug], rax
       !sub rsp, 8 
       !pop rax 
     !end If    
  ! end If 
  Debug gDebug 
EndMacro 

Procedure foo()
   Protected b  
   b = 4 
  debugASM(b)
   
EndProcedure    

DebugASM(rax)
DebugAsm(var1)

foo()


Re: Learning ASM, a basic question

Posted: Wed Jul 31, 2013 11:17 pm
by ostapas
as a little fasm processor debug experiment it's not very useful, x64 only
A bit advanced for me, but I suspect, this will come in handy later. Thanks!

Re: Learning ASM, a basic question

Posted: Thu Aug 01, 2013 8:53 am
by Danilo
luis wrote:So to read from you FASM data and print through debug, you could do something like this:

Code: Select all

Define var ; this is the PB "var", it will translated to "v_var" to the ASM output to be sent to FASM

! mov eax, [var] ; this will copy your FASM "var" into a register
! mov [v_var], eax ; this will copy that value to the "v_var" the debug command can access

Debug var ; and here it is

End ; end to stop the program here

! var dd 1234567 ; your fasm data (double word to comply easily with register size)
If your variable is within the code section, you are not allowed to write to it by default. Better put data/variables in a data section.

Code: Select all

Define var.l ; this is the PB "var", it will translated to "v_var" to the ASM output to be sent to FASM

! mov eax, [var] ; this will copy your FASM "var" into a register
! mov [v_var], eax ; this will copy that value to the "v_var" the debug command can access

Debug var ; and here it is

! mov dword [var], 987654321 ; write to your var -> THIS WORKS ONLY IF YOUR VAR IS IN DATA SECTION

! mov eax, [var] ; this will copy your FASM "var" into a register
! mov [v_var], eax ; this will copy that value to the "v_var" the debug command can access
Debug var ; and here it is

End

DataSection
! var dd 1234567 ; your fasm data (double word to comply easily with register size)
EndDataSection

Re: Learning ASM, a basic question

Posted: Thu Aug 01, 2013 9:53 am
by ostapas
If your variable is within the code section, you are not allowed to write to it by default. Better put data/variables in a data section.
Thanks, understood.