14 March 2021, 06:01 | #1 |
Registered User
Join Date: Jun 2020
Location: Druidia
Posts: 386
|
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" ); 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 |
14 March 2021, 07:10 | #2 |
Registered User
Join Date: Mar 2016
Location: Australia
Posts: 881
|
Which gcc are you using ? The details are different if you're using for example bebbo's vs Bartman's
|
14 March 2021, 07:25 | #3 |
Registered User
Join Date: Sep 2011
Location: Paris/France
Posts: 274
|
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 |
14 March 2021, 08:27 | #4 | ||
bye
Join Date: Jun 2016
Location: Some / Where
Posts: 680
|
Quote:
Quote:
Code:
movem.l %%a5,(%%sp) Code:
movem.l %%a5,-(%%sp) Code:
extern int Routine(register short a ASM("d0"), register int b ASM("d1"), register short c ASM("d2")); ... 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 |
||
14 March 2021, 18:33 | #5 |
Registered User
Join Date: Jun 2020
Location: Druidia
Posts: 386
|
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! |
14 March 2021, 18:40 | #6 |
Registered User
Join Date: Jun 2020
Location: Druidia
Posts: 386
|
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? |
14 March 2021, 18:43 | #7 | |
<optimized out>
Join Date: Sep 2020
Location: <optimized out>
Posts: 321
|
Quote:
|
|
14 March 2021, 18:55 | #8 |
bye
Join Date: Jun 2016
Location: Some / Where
Posts: 680
|
|
14 March 2021, 18:55 | #9 | |
bye
Join Date: Jun 2016
Location: Some / Where
Posts: 680
|
Quote:
use the mangled names to refer to these. e.g. Code:
bsr __Z3fooi Code:
int foo(int); |
|
14 March 2021, 19:13 | #10 |
Registered User
Join Date: Jun 2020
Location: Druidia
Posts: 386
|
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. |
14 March 2021, 19:40 | #11 |
Registered User
Join Date: Jun 2020
Location: Druidia
Posts: 386
|
|
14 March 2021, 21:15 | #12 |
<optimized out>
Join Date: Sep 2020
Location: <optimized out>
Posts: 321
|
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.
|
14 March 2021, 22:07 | #13 | |
Registered User
Join Date: Jun 2020
Location: Druidia
Posts: 386
|
Quote:
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. |
|
15 March 2021, 08:18 | #14 |
Registered User
Join Date: Jul 2017
Location: San Jose
Posts: 652
|
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. |
11 April 2021, 16:07 | #15 |
Registered User
Join Date: Jun 2020
Location: Druidia
Posts: 386
|
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? |
11 April 2021, 20:42 | #16 | |
bye
Join Date: Jun 2016
Location: Some / Where
Posts: 680
|
Quote:
use <number>$ for local labels: Code:
void foo() { asm("1$:"); asm("bra 1$"); } void faa() { asm("1$:"); asm("bra 1$"); } |
|
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 |
|
|