Simple Question on Inline ASM

Just starting out? Need help? Post your questions and find answers here.
User avatar
newtheogott
Enthusiast
Enthusiast
Posts: 120
Joined: Sat Apr 26, 2003 2:52 pm
Location: Germany, Karlsruhe
Contact:

Simple Question on Inline ASM

Post by newtheogott »

Coming back after long time not using Purebasic, I must say that the Editor has made a significant developement.
The compiler was also improved, and especially i like that its available in 32 and 64 bit.

In the compiler, i am missing primarily a second pass (i still have to declare things before using them - is that really state of the art 2009?), and i don't understand why there is no GOSUB / RETURN inside of Procedures.

Just trying to get it done with the Inline ASM like this:

Code: Select all

 Procedure hey()
    
    a.l = 1
  CALL l_asm_0001
    Debug a 
    
    Goto over 
         
    asm_0001: 
   ; a+10
   a=a+10
    RET 
    over:
    
    Debug a
  EndProcedure

hey()
Now i wonder that the compiler did not increase the "a" by 10 this way.
What i am missing here?
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Re: Simple Question on Inline ASM

Post by Rings »

very simple,
normaly variables are hold on ths stack in procedures,
and ther stack varies if there is a gosub-return (Call and RET in asm).
So declare your variable as STATIC (STATIC a.l )=and it will not been on the stack.
and increase with 10 .

if i'm wrong fred, feel free to correct me.
SPAMINATOR NR.1
User avatar
newtheogott
Enthusiast
Enthusiast
Posts: 120
Joined: Sat Apr 26, 2003 2:52 pm
Location: Germany, Karlsruhe
Contact:

Re: Simple Question on Inline ASM

Post by newtheogott »

Thanks for the Info. In the result, you are right. If i declare the variable as STATIC, it works.

a=11

But then this means, there is still no chance of having a "GOSUB" inside Procedures. :(

Because down there all variables will not work, or even do strange things, until i have returned from the "CALL".

What if i try to access Purebasic variables inside a INLINE-ASM Code where i have been using a CALL instuction? Then it just won't work, if i see this right. This would mean that this is actually not supported.

Is there a supported workaround, maybe to save the stack somewhere, access all variables and restore it before the RET?
Fred
Administrator
Administrator
Posts: 18297
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Simple Question on Inline ASM

Post by Fred »

There is no gosub in procedure for this very reason: variable are on the stack an there is no reliable way to find out there position as PB use direct stack indexing to save a register. That said, is gosub really a must-have (in 2009) ? ;)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Simple Question on Inline ASM

Post by Psychophanta »

You might find interesting this old thread:
http://www.purebasic.fr/english/viewtop ... f=3&t=8502

By the way, i wonder how did Mark Sibly do for Blitz Basic in amiga. There worked any call from inside a function: Inside to inside, inside to outside and viceversa. I guess that he didn't use the standard stack but another stack.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
newtheogott
Enthusiast
Enthusiast
Posts: 120
Joined: Sat Apr 26, 2003 2:52 pm
Location: Germany, Karlsruhe
Contact:

Re: Simple Question on Inline ASM

Post by newtheogott »

said, is gosub really a must-have (in 2009) ?
Fred, let me say first that after long time I like to see the good developments Purebasic made since i visited it last.
About the GOSUB, there are few cases where its just faster if you can use it, because it does not use the Stack-Frame a Procedure call with Parameters makes.
As its by Design not supported from Purebasic, the discussion on this topic is worthless.

So lets talk about the alternatives. MACROS could be taken as an alternative.
When trying the Macros, i realized that they actually seem not to support "Macro Local Labels" and Macro local Variables.

Code: Select all

MACRO Test()
MyLabel:

IF (a=b)
  GOTO MyLabel
endif
ENDMACRO
Using this MACRO, i could only use it ONCE because it contains a Label, that has to be unique.
Labels are in Purebasic not even Procedure-Local but Global, if my Tests were right.
I'd prefer Labels to be generally Procedure-local, as I would never want to Jump from outside into a Procedure.
Therefore I do not need the Label outside. But i could take the same Label in man Procedures.
In big Projects, i find myself making Lables like that: Label_0002 to avoid Duplicates.

So about this my wishlist for Purebasic contains here (not the GSUB as it seem unpossible) but these points:
- procedure-local labels (I don't know if this is possible)
- macro - local labels and variables (this one is easy to realize, just add a number in the preprocessor, each time the macro is called)
could use a #MACROTEMP a,b,d ; Directive

I'll make some more suggestions from my standpoint in another post.
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: Simple Question on Inline ASM

Post by citystate »

rather than using a Macro to simulate GOSUB, why don't you just use a procedure with shared variables? wouldn't the functionality be simulated in this way?
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: Simple Question on Inline ASM

Post by Foz »

Fred wrote:That said, is gosub really a must-have (in 2009) ? ;)

Code: Select all

1954 GOSUB 2009
...
2009 RETURN : REM --- Panic! Should not be here!
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Simple Question on Inline ASM

Post by Thorium »

citystate wrote:rather than using a Macro to simulate GOSUB, why don't you just use a procedure with shared variables? wouldn't the functionality be simulated in this way?
No. He want this feature for speed optimization. The procedure call is much slower than a macro, even if you use shared variables. However a gosub would be a Call and calls are also slow. Using macros would be the best.
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: Simple Question on Inline ASM

Post by citystate »

newtheogott wrote:So lets talk about the alternatives. MACROS could be taken as an alternative.
When trying the Macros, i realized that they actually seem not to support "Macro Local Labels" and Macro local Variables.

Code: Select all

MACRO Test()
MyLabel:

IF (a=b)
  GOTO MyLabel
endif
ENDMACRO
Using this MACRO, i could only use it ONCE because it contains a Label, that has to be unique.
could you do something like this instead?

Code: Select all

MACRO Test(num)
Mylabel#num:
IF (a=b)
  GOTO MyLabel#num
endif
ENDMACRO
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Simple Question on Inline ASM

Post by blueznl »

I would like to see an example where Gosub is an essential. Could you give one? This is interesting to me, as I cannot think of one where it would matter :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Fred
Administrator
Administrator
Posts: 18297
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Simple Question on Inline ASM

Post by Fred »

I never use Gosub, so i don't think it's essential. Now, that's a matter a coding, and we all respect that :).
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Simple Question on Inline ASM

Post by freak »

Fred wrote:I never use Gosub, so i don't think it's essential. Now, that's a matter a coding, and we all respect that :).
The DocMaker source still has a few Gosub and it was written by you :P
They are not essential though... We're just too lazy to change it.

Thorium wrote:
citystate wrote:rather than using a Macro to simulate GOSUB, why don't you just use a procedure with shared variables? wouldn't the functionality be simulated in this way?
No. He want this feature for speed optimization. The procedure call is much slower than a macro, even if you use shared variables. However a gosub would be a Call and calls are also slow. Using macros would be the best.
This is not necessarily true. By blowing up the code size with macros you can easily make the code slower due to caching effects. Calls have become really cheap in today's CPUs (because of branch prediction with call-return stacks etc). So worrying about this kind of thing is a waste of time and even dangerous. Write your code, benchmark it and then you can start thinking about these kinds of optimizations if you really need them (chances are you won't).

newtheogott wrote:About the GOSUB, there are few cases where its just faster if you can use it, because it does not use the Stack-Frame a Procedure call with Parameters makes.
A call to a procedure with no arguments is the exact same thing as a Gosub. Both just push a return address and jump to the target location. There are only further stack operations if the procedure has local variables (which Gosub does not have of course).
quidquid Latine dictum sit altum videtur
User avatar
newtheogott
Enthusiast
Enthusiast
Posts: 120
Joined: Sat Apr 26, 2003 2:52 pm
Location: Germany, Karlsruhe
Contact:

Re: Simple Question on Inline ASM

Post by newtheogott »

A call to a procedure with no arguments is the exact same thing as a Gosub. Both just push a return address and jump to the target location. There are only further stack operations if the procedure has local variables (which Gosub does not have of course).
I think this is the best sollution. As this is by-Design the way it shall be done in Purebasic any discussion does not make sense.
As said in other systems the use of GOSUB makes sense to me. In Purebasic I'll use PROCEDURES as structure-element in these cases and give the needed variables as parameter (BYREF).
Now I'll have to take a look how to give Parameters BYREF (by Reference) and BYVAL. How that works in Purebasic.
--Theo Gottwald
-----------------------------------------
http://www.it-berater.org * http://www.fa2.de * http://www.smart-package.com
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: Simple Question on Inline ASM

Post by citystate »

glad I could help :D
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
Post Reply