It is currently Sat Mar 06, 2021 3:50 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Learning ASM, a basic question
PostPosted: Wed Jul 31, 2013 5:43 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Feb 18, 2010 11:10 pm
Posts: 192
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:
! var db 255
Debug var
I just get an IMA.


Top
 Profile  
Reply with quote  
 Post subject: Re: Learning ASM, a basic question
PostPosted: Wed Jul 31, 2013 7:43 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3698
Location: Italy
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.

_________________
[ My little PureBasic review ]


Top
 Profile  
Reply with quote  
 Post subject: Re: Learning ASM, a basic question
PostPosted: Wed Jul 31, 2013 8:06 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Feb 18, 2010 11:10 pm
Posts: 192
Many thanks for a reply!
Quote:
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.
Quote:
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 :) )


Top
 Profile  
Reply with quote  
 Post subject: Re: Learning ASM, a basic question
PostPosted: Wed Jul 31, 2013 8:30 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3698
Location: Italy
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:
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.

_________________
[ My little PureBasic review ]


Top
 Profile  
Reply with quote  
 Post subject: Re: Learning ASM, a basic question
PostPosted: Wed Jul 31, 2013 8:37 pm 
Offline
Always Here
Always Here
User avatar

Joined: Thu Jun 24, 2004 2:44 pm
Posts: 5756
Location: Berlin - Germany
You should change the first line of example from luis to:
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Learning ASM, a basic question
PostPosted: Wed Jul 31, 2013 8:50 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Feb 18, 2010 11:10 pm
Posts: 192
luis, ts-soft, sincere thanks, exactly what I was looking for.


Top
 Profile  
Reply with quote  
 Post subject: Re: Learning ASM, a basic question
PostPosted: Wed Jul 31, 2013 10:37 pm 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3574
Location: New Zealand
as a little fasm processor debug experiment it's not very useful, x64 only


Code:
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()



Top
 Profile  
Reply with quote  
 Post subject: Re: Learning ASM, a basic question
PostPosted: Wed Jul 31, 2013 11:17 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Feb 18, 2010 11:10 pm
Posts: 192
Quote:
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: Learning ASM, a basic question
PostPosted: Thu Aug 01, 2013 8:53 am 
Offline
Addict
Addict
User avatar

Joined: Sat Apr 26, 2003 8:26 am
Posts: 2999
Location: Planet Earth
luis wrote:
So to read from you FASM data and print through debug, you could do something like this:

Code:
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Learning ASM, a basic question
PostPosted: Thu Aug 01, 2013 9:53 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Feb 18, 2010 11:10 pm
Posts: 192
Quote:
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye