Page 1 of 1

Link ASM and C code

Posted: Thu May 16, 2019 8:03 am
by tcat
Hi,

I have a main programme in assembly, and I wish to add some functions with integer arithmetics, easier coded in C possibly (or Pascal).

I use mainly `GST Assembler' (first prog I had on my QL) and just started looking into `GST C'. Assembly has now some unresolved labels, thats where the `C' functions should go.

Is there any general recipe how to merge a final binary through a linker?

Many thanks
Tomas

Re: Link ASM and C code

Posted: Sat Jun 01, 2019 5:17 pm
by tcat
Hi,

I resorted to `ASM' coding only. `C' might still be a better option for porting code from high level languages. To make assembly easier, I use `named' local variables on the stack de/allocated with link..unlk pair of calls. Hope is reasonable, and correct.

EDIT offsets have to be negative, as stack grows down
EDIT $00 seems wrong, has to be -$02 for cf_x0, last word on the stack

Code: Select all

circf    link     a4,#-14           ;locals cf
cf_x0    equ     -$02
cf_y0    equ     -$04
cf_col   equ     -$06
cf_r     equ     -$08               ;radius
cf_x     equ     -$0a
cf_y     equ     -$0c
cf_r2    equ     -$0e               ;rad square
         move.w   d1,cf_x0(a4)      ;save locals
         move.w   d2,cf_y0(a4)
         ...
         unlk     a4
         rts
Tomas

Re: Link ASM and C code

Posted: Sat Jun 01, 2019 7:23 pm
by Derek_Stewart
Hi Tomas,

I would think you need to compile the Pascal or C programme to an object code module, then use the Linker to link all the object code modules to a single binary file.

Re: Link ASM and C code

Posted: Sat Jun 01, 2019 10:05 pm
by janbredenbeek
tcat wrote:Hi,

I have a main programme in assembly, and I wish to add some functions with integer arithmetics, easier coded in C possibly (or Pascal).
I use mainly `GST Assembler' (first prog I had on my QL) and just started looking into `GST C'. Assembly has now some unresolved labels, thats where the `C' functions should go.
Is there any general recipe how to merge a final binary through a linker?
It's much easier to code the main program in C and the timing-critical routines in assembly than the other way round. The C program needs libraries and startup code to initialise registers and variables where the libraries depend upon. Also a main() function MUST be present since the startup code will eventually call this to execute the main program, and the linker will complain when it's not there.
Thinking of it, it would be best to call the assembly from the main() function and call the C functions back from there. This requires certain registers to be preserved - the QC manual has a quite extensive section on interfacing with assembly.

Jan.

Re: Link ASM and C code

Posted: Sun Jun 02, 2019 12:04 am
by tofro
janbredenbeek wrote:
tcat wrote:Hi,

I have a main program in assembly, and I wish to add some functions with integer arithmetics, easier coded in C possibly (or Pascal).
It's much easier to code the main program in C and the timing-critical routines in assembly than the other way round.
That's actually true for C68, which uses a relocating loader and extensive startup code. GST C, being a "small C" derivate, is much simpler. As long as you don't use any library functions, you don't even need to link with C startup code, but can directly create _asm, then _rel files with the C compiler. You can treat such _rel files with C origin just like standard assembler files.
janbredenbeek wrote: The C program needs libraries and startup code to initialise registers and variables where the libraries depend upon. Also a main() function MUST be present since the startup code will eventually call this to execute the main program, and the linker will complain when it's not there..
If you don't use the C startup code and runtimes in your C program, you also don't need a main() function.
(Admitted, the C code isn't much more comfortable to write then than assembly)

Tobias

Re: Link ASM and C code

Posted: Sun Jun 02, 2019 4:37 pm
by tcat
Gents,

Thanks.
I seem getting a lot of advice. Honestly I have not tried linking various `_rel' object files. An intersting exercise I wish to do anyway. `C' runtime and some std lib may seem an overhead for a small project such as this one of mine.
I was also thinking to link various `_bin', or `_rel' produced by the assembler itself, as I have quite a few lables in the source already, making code modular can help to resolve duplicates.

Many thanks.
Tomas