Strange problem with labels

Bare metal programming in PureBasic, for experienced users
User avatar
doctorized
Enthusiast
Enthusiast
Posts: 774
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Strange problem with labels

Post by doctorized »

If I say:

Code: Select all

aa.l
EnableASM
mov eax, 1
cmp eax, 1
je l_nxt
mov eax, 5
nxt:
mov aa, eax
DisableASM
Debug aa
I get output 1 as expected but if I say:

Code: Select all

Procedure test()
aa.l
EnableASM
mov eax, 1
cmp eax, 1
je l_nxt
mov eax, 5
nxt:
mov aa, eax
DisableASM
Debug aa
EndProcedure

test()
I get undefined symbol 'l_nxt' error. Why? What is the right expression?
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Strange problem with labels

Post by Shield »

If the label is defined in a procedure, then its prefix is 'l_procedurename_', in lowercase.
Manual is actually wrong here, it should be ll_:

Code: Select all

Procedure test()
aa.l
EnableASM
mov eax, 1
cmp eax, 1
je 	ll_test_nxt
mov eax, 5
nxt:
mov aa, eax
DisableASM
Debug aa
EndProcedure

test()
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
Keya
Addict
Addict
Posts: 1828
Joined: Thu Jun 04, 2015 7:10 am

Re: Strange problem with labels

Post by Keya »

another option is to !inline them directly to the assembler:

Code: Select all

aa.l
EnableASM
mov eax, 1
cmp eax, 1
!je nxt         ;<--
mov eax, 5
!nxt:           ;<--
mov aa, eax
DisableASM
Debug aa
User avatar
doctorized
Enthusiast
Enthusiast
Posts: 774
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Strange problem with labels

Post by doctorized »

Thanx for the replies. I had tried ll_ but I didn't know that procedure name should be included.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3734
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Strange problem with labels

Post by wilbert »

Personally I prefer the inline approach Keya demonstrated.
As far as I remember, the way labels are named hasn't always been the same across different PB versions.
It also looks inconsistent to me that the label you have to use for the jump instruction is different form the label declaration itself.
By coding manually, I have full control and know things are working as they should.
macOS 10.15 Catalina, Windows 10
User avatar
doctorized
Enthusiast
Enthusiast
Posts: 774
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Strange problem with labels

Post by doctorized »

wilbert wrote:Personally I prefer the inline approach Keya demonstrated.
As far as I remember, the way labels are named hasn't always been the same across different PB versions.
It also looks inconsistent to me that the label you have to use for the jump instruction is different form the label declaration itself.
By coding manually, I have full control and know things are working as they should.
Yes, you are right. The way labels work in asm have changed at least two times the last years. I would like to have something more "stable", something that will last regardless PB changes. Is Keya's approach this kind of "stable"?
Helle
Enthusiast
Enthusiast
Posts: 177
Joined: Wed Apr 12, 2006 7:59 pm
Location: Germany
Contact:

Re: Strange problem with labels

Post by Helle »

We hope so :) !
In procedures use label with point as local label. See:

Code: Select all

Procedure test()
aa.l
EnableASM
mov eax, 1
cmp eax, 1
!je nxt
mov eax, 5
!nxt:
mov aa, eax
DisableASM
Debug aa
EndProcedure

Procedure test1()
aa.l
EnableASM
mov eax, 1
cmp eax, 1
!je nxt
mov eax, 5
!nxt:
mov aa, eax
DisableASM
Debug aa
EndProcedure

test()
test1()
But:

Code: Select all

Procedure test()
aa.l
EnableASM
mov eax, 1
cmp eax, 1
!je .nxt
mov eax, 5
!.nxt:
mov aa, eax
DisableASM
Debug aa
EndProcedure

Procedure test1()
aa.l
EnableASM
mov eax, 1
cmp eax, 1
!je .nxt
mov eax, 5
!.nxt:
mov aa, eax
DisableASM
Debug aa
EndProcedure

test()
test1()
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Strange problem with labels

Post by Shield »

If it's just about one or two local labels, I prefer using anonymous labels:

Code: Select all

Procedure.i foobar()
	
	!jmp @f
	MessageRequester("skip", "skip")
	!@@:
	
	ProcedureReturn 0	
EndProcedure

foobar()
Use @f to jump forward or @b to jump backwards.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
sys64802
Enthusiast
Enthusiast
Posts: 105
Joined: Sat Sep 12, 2015 6:55 pm

Re: Strange problem with labels

Post by sys64802 »

Yes the fact you must decorate your labels it's clunky, particularly inside EnableASM/DisableASM

request http://www.purebasic.fr/english/viewtop ... 56#p451756

Are anon labels available on mac too ? Don't know if nasm or yasm is used on mac, but in any case I'm not sure if anon labels are supported there.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3734
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Strange problem with labels

Post by wilbert »

The dot syntax Helle mentioned seems to work fine on OSX.
Anonymous labels (@f with @@) are not supported on OSX. Nasm/Yasm doesn't support this.
If you want your Asm code to be cross platform compatible, don't use them.
macOS 10.15 Catalina, Windows 10
sys64802
Enthusiast
Enthusiast
Posts: 105
Joined: Sat Sep 12, 2015 6:55 pm

Re: Strange problem with labels

Post by sys64802 »

wilbert wrote: Anonymous labels (@f with @@) are not supported on OSX. Nasm/Yasm doesn't support this.
Thank you, but out of curiosity it's nasm or yasm used on osx ? :)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3734
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Strange problem with labels

Post by wilbert »

sys64802 wrote:Thank you, but out of curiosity it's nasm or yasm used on osx ? :)
Both :shock:
PureBasic uses yasm for the 64 bit version of PureBasic on OSX and nasm for the 32 bit version.
Don't ask me why because I have no idea why Fred made this choice.
To me it would be more logical to choose one of the two and use it for both 32 and 64 bit.

Edit:
It seems also possible to use Fasm on OSX with the help of the objconv tool from Agner Fog.
http://www.agner.org/optimize/objconv-instructions.pdf
https://github.com/JohnDDuncanIII/fasm-osx
macOS 10.15 Catalina, Windows 10
sys64802
Enthusiast
Enthusiast
Posts: 105
Joined: Sat Sep 12, 2015 6:55 pm

Re: Strange problem with labels

Post by sys64802 »

Thank you. :shock:

Didn't know about objconv, that guy is crazy. Awesome. But I don't know enough to fully understand the limitations I think. Way over my head.

Still awesome from what I can understand.
User avatar
doctorized
Enthusiast
Enthusiast
Posts: 774
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Strange problem with labels

Post by doctorized »

sys64802 wrote:Thank you. :shock:

Didn't know about objconv, that guy is crazy. Awesome. But I don't know enough to fully understand the limitations I think. Way over my head.

Still awesome from what I can understand.
What's the awesome? :?: What's objconv?
User avatar
Tristano
Enthusiast
Enthusiast
Posts: 191
Joined: Thu Nov 26, 2015 6:52 pm
Location: Italy
Contact:

Re: Strange problem with labels

Post by Tristano »

There have also been around Mac ports of FAsm. Here is one:

https://github.com/JohnDDuncanIII/fasm

the repository contains a flat assembler version that will run on Macintosh OS X 10.6-10.11 (both x86 and x86_64).

Even though this isn't an officially supported FAsm version, and won't be guaranteed to be always updated along with latest official FAsm releases, it would be nice if in the upcoming PureBasic versions there was an option to use the FAsm compiler on Mac, instead of Yasm/MASM. I wonder if this is possible ...

If Mac where to support FAsm then there would no longer be the cross-compatiblity issue that is currently introduced by the presence of ASM code in PB sources.
The PureBASIC Archives: FOSS Resources:
Post Reply