Learning ASM, a basic question

Bare metal programming in PureBasic, for experienced users
User avatar
ostapas
Enthusiast
Enthusiast
Posts: 192
Joined: Thu Feb 18, 2010 11:10 pm

Learning ASM, a basic question

Post 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.
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Learning ASM, a basic question

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
ostapas
Enthusiast
Enthusiast
Posts: 192
Joined: Thu Feb 18, 2010 11:10 pm

Re: Learning ASM, a basic question

Post 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 :) )
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Learning ASM, a basic question

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Learning ASM, a basic question

Post 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!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
ostapas
Enthusiast
Enthusiast
Posts: 192
Joined: Thu Feb 18, 2010 11:10 pm

Re: Learning ASM, a basic question

Post by ostapas »

luis, ts-soft, sincere thanks, exactly what I was looking for.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Learning ASM, a basic question

Post 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()

Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
ostapas
Enthusiast
Enthusiast
Posts: 192
Joined: Thu Feb 18, 2010 11:10 pm

Re: Learning ASM, a basic question

Post 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!
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Learning ASM, a basic question

Post 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
User avatar
ostapas
Enthusiast
Enthusiast
Posts: 192
Joined: Thu Feb 18, 2010 11:10 pm

Re: Learning ASM, a basic question

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