English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 14 March 2021, 06:01   #1
Jobbo
Registered User

Jobbo's Avatar
 
Join Date: Jun 2020
Location: Lexington, MA
Posts: 195
Using GCC separate assembler files

Does anyone know the details of using separate .s files for assembler along with C/C++ when using the GCC assembler "as".

Can you point me to documentation about correct register saving/restoring procedures.

And to documentation explaining all the syntax for things like exposing and sharing variables and functions between assembler and C/C++.

I've been doing plenty of inline asm and that is fine. I'm asking about separate files because at a certain size a routine becomes a pain to manage with the inline stuff. But at the same time I don't want to do the wrong thing and end up with slightly worse performance due to bad interfacing.

I'm aware some people use "vasm" rather than "as". I'm not sure why, the code I can write in .s files seems about the same as I remember.

Anyway here is an example of how I've managed to get things working and the some questions.

In C to call my routine, something like this:

Code:
	extern int* Buffer;
	Buffer = bufferFromSomewhere;	// Set pointer that asm code sees.

	register short _a ASM("d0") = a;	// Get some vars into the right regs.
	register int _b ASM("d1") = b;
	register int _c ASM("d2") = c;

	asm volatile(
	"	movem.l	%%a5,(%%sp)	\n"
	"	jsr		Routine		\n"
	"	movem.l	(%%sp)+,%%a5	\n"
	:
	"+d"	(_b),	// _b gets modified.
	"+d"	(_c)	// _c gets modified.
	:
	"d"	(_a)	// _a is unmodified.
	:
	"d4", "d5", "d6", "a0", "a1", "a2",	// Lets say I trash these.
	"cc",
	"memory"
	);
Then in the .s file something like:

Code:
	| This is how I expose the function.
	| Doesn't seem to need the .type things, but I've seen that done.
	| Anyone know what it does?
	.type		Routine,function
	.global	Routine

Routine:
	| Do stuff here with all the inputs.
	rts

	.global	Buffer	| This is how I expose the buffer pointer.
Buffer:
	dc.l	0
Jobbo is offline  
Old 14 March 2021, 07:10   #2
alpine9000
Registered User

 
Join Date: Mar 2016
Location: Australia
Posts: 867
Which gcc are you using ? The details are different if you're using for example bebbo's vs Bartman's
alpine9000 is offline  
Old 14 March 2021, 07:25   #3
thellier
Registered User
 
Join Date: Sep 2011
Location: Paris/France
Posts: 249
Say you want a fonction foo() to be in asm
Then write a stand alone foo.c containing that single fonction and the other c sources as usual
Compile to asm foo.c
gcc -S foo.c
You obtain a foo.s ready to be hand edited as asm
Then build the binary with all the *.o and this .s
thellier is offline  
Old 14 March 2021, 08:27   #4
bebbo
botcher

 
Join Date: Jun 2016
Location: Hamburg/Germany
Posts: 528
Quote:
Can you point me to documentation about correct register saving/restoring procedures.
On the Amiga the called function saves all modified registers but not d0/d1/a0/a1/fp0/fp1.

Quote:
And to documentation explaining all the syntax for things like exposing and sharing variables and functions between assembler and C/C++.
the doc for inline assembly is there https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html


Code:
movem.l	%%a5,(%%sp)
should be
Code:
movem.l	%%a5,-(%%sp)
depending on the used compiler you could also write in C:
Code:
extern int Routine(register short a ASM("d0"), register int b ASM("d1"), register short c ASM("d2"));
otherwise you might use macros to move the asm out of your C code...
... have a look into inline/macros.h to get inspiration.

and simply call that function. Note that the asm function needs an additional leading underscore if you use the C prototype with asm registers.
Code:
.global	_Routine
_Routine:
   movem.l d2/whatever,-(sp)
...
   move.l ...,d0
   movem.l (sp)+,d2/whatever
bebbo is offline  
Old 14 March 2021, 18:33   #5
Jobbo
Registered User

Jobbo's Avatar
 
Join Date: Jun 2020
Location: Lexington, MA
Posts: 195
Thanks for the info. I'm using Bartman's VSCode setup.

I'm very familiar with the inline documentation. Was hoping to find something similar to cover all the separate .s file assembly options and requirements.

Like I said I can make things work but would like to have reference to gain a full understanding.

I did post on the github for Bartman's setup and he got back with some other advice and a link to this https://sourceware.org/binutils/docs/as/

So I'm probably good now, but still interested to hear any new advice and any opinions on this versus using vasm.

Thanks!
Jobbo is offline  
Old 14 March 2021, 18:40   #6
Jobbo
Registered User

Jobbo's Avatar
 
Join Date: Jun 2020
Location: Lexington, MA
Posts: 195
Here's a very specific question.

How can I access C/C++ globals from inside my .s file?

It's trivial to expose a global from the .s file to the C/C++ using .global, but what about going the other direction?
Jobbo is offline  
Old 14 March 2021, 18:43   #7
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 304
Quote:
Originally Posted by Jobbo View Post
Thanks for the info. I'm using Bartman's VSCode setup.

I'm very familiar with the inline documentation. Was hoping to find something similar to cover all the separate .s file assembly options and requirements.

Like I said I can make things work but would like to have reference to gain a full understanding.

I did post on the github for Bartman's setup and he got back with some other advice and a link to this https://sourceware.org/binutils/docs/as/

So I'm probably good now, but still interested to hear any new advice and any opinions on this versus using vasm.

Thanks!
%I'm %sure %there's %nothing %wrong %with %the %gnu %assembler %once %you're %used %to %its %syntax.
Ernst Blofeld is offline  
Old 14 March 2021, 18:55   #8
bebbo
botcher

 
Join Date: Jun 2016
Location: Hamburg/Germany
Posts: 528
Quote:
Originally Posted by Ernst Blofeld View Post
%I'm %sure %there's %nothing %wrong %with %the %gnu %assembler %once %you're %used %to %its %syntax.

m68k-amigaos-as does not need the percent sign for registers. The tradeoff is: you can't use symbols having register names.
bebbo is offline  
Old 14 March 2021, 18:55   #9
bebbo
botcher

 
Join Date: Jun 2016
Location: Hamburg/Germany
Posts: 528
Quote:
Originally Posted by Jobbo View Post
Here's a very specific question.

How can I access C/C++ globals from inside my .s file?

It's trivial to expose a global from the .s file to the C/C++ using .global, but what about going the other direction?

use the mangled names to refer to these.

e.g.
Code:
       bsr __Z3fooi
to call the C++ function
Code:
    int foo(int);
bebbo is offline  
Old 14 March 2021, 19:13   #10
Jobbo
Registered User

Jobbo's Avatar
 
Join Date: Jun 2020
Location: Lexington, MA
Posts: 195
Thanks. I was asking for vars but should be the same. Didn’t occur to me to use the mangled name.

In general it seems like it’ll be better to go the other way so the asm code can access these vars pc relative. I imagine that will break for c vars depending on where the linker puts them.
Jobbo is offline  
Old 14 March 2021, 19:40   #11
Jobbo
Registered User

Jobbo's Avatar
 
Join Date: Jun 2020
Location: Lexington, MA
Posts: 195
Quote:
Originally Posted by Ernst Blofeld View Post
%I'm %sure %there's %nothing %wrong %with %the %gnu %assembler %once %you're %used %to %its %syntax.
--register-prefix-optional
Jobbo is offline  
Old 14 March 2021, 21:15   #12
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 304
Quote:
Originally Posted by Jobbo View Post
--register-prefix-optional
I also had issues with at least the exg and movem instructions last time I tried to do anything significant with the gnu assembler. Unfortunately I can't remember what those issues were or whether I solved them, but you might want to include some in your tests.
Ernst Blofeld is offline  
Old 14 March 2021, 22:07   #13
Jobbo
Registered User

Jobbo's Avatar
 
Join Date: Jun 2020
Location: Lexington, MA
Posts: 195
Quote:
Originally Posted by Ernst Blofeld View Post
I also had issues with at least the exg and movem instructions last time I tried to do anything significant with the gnu assembler. Unfortunately I can't remember what those issues were or whether I solved them, but you might want to include some in your tests.

With the inline asm I definitely had issues with movem.


At some point, that would be out of my control the compiler would start to ignore my specific register settings for the inputs. The code would then do weird things until I could track down what just happened.



Then I would have to resort to code that directly used named registers. At which point you might as well be writing code in a separate file.


No problems so far though with .s files.
Jobbo is offline  
Old 15 March 2021, 08:18   #14
pipper
Registered User

 
Join Date: Jul 2017
Location: San Jose
Posts: 317
ADoom and DoomAttack both mix assembly and gcc. In particular DoomAttack shows how to share functions and variables between C and assembly.

https://github.com/mheyer32/ADoom

https://github.com/mheyer32/DoomAttack

In this case VAsm is used to produce hunk object files that naturally link with Bebbo’s GCC.
pipper is offline  
Old 11 April 2021, 16:07   #15
Jobbo
Registered User

Jobbo's Avatar
 
Join Date: Jun 2020
Location: Lexington, MA
Posts: 195
Does anyone know how to use the GCC assembler to add locally scoped labels?

If I recall correctly, in other assemblers you just add a dot in front of the label and it'll scope to the nearest previous none-dot label.

This doesn't seem to work with the GCC assembler and I can't figure out what to do instead.

For those who have given up and are using Vasm, does the debugger still understand how to single step through that code?
Jobbo is offline  
Old 11 April 2021, 20:42   #16
bebbo
botcher

 
Join Date: Jun 2016
Location: Hamburg/Germany
Posts: 528
Quote:
Originally Posted by Jobbo View Post
Does anyone know how to use the GCC assembler to add locally scoped labels?

If I recall correctly, in other assemblers you just add a dot in front of the label and it'll scope to the nearest previous none-dot label.

This doesn't seem to work with the GCC assembler and I can't figure out what to do instead.

For those who have given up and are using Vasm, does the debugger still understand how to single step through that code?

use <number>$ for local labels:

Code:
void foo() {
  asm("1$:");
  asm("bra 1$");
}
void faa() {
  asm("1$:");
  asm("bra 1$");
}
bebbo is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Assembler files and C in GCC / bartman's amiga-debug zero Coders. C/C++ 11 12 March 2021 16:17
GCC Assembler return value question... NovaCoder Coders. Asm / Hardware 0 23 June 2012 07:11
where are header files located in GCC ? Morbane Coders. General 0 05 January 2012 11:42
Linking to assembler objects using C++ gcc (StormV4) NovaCoder Coders. General 6 09 December 2009 19:35
Assembler files in the zone redblade Coders. General 0 06 September 2004 03:51

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 15:40.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Page generated in 0.09396 seconds with 13 queries