Link ASM and C code

Anything QL Software or Programming Related.
Post Reply
tcat
Super Gold Card
Posts: 633
Joined: Fri Jan 18, 2013 5:27 pm
Location: Prague, Czech Republic

Link ASM and C code

Post 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


tcat
Super Gold Card
Posts: 633
Joined: Fri Jan 18, 2013 5:27 pm
Location: Prague, Czech Republic

Re: Link ASM and C code

Post 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
Last edited by tcat on Mon Jun 03, 2019 10:58 am, edited 3 times in total.


Derek_Stewart
Font of All Knowledge
Posts: 3929
Joined: Mon Dec 20, 2010 11:40 am
Location: Sunny Runcorn, Cheshire, UK

Re: Link ASM and C code

Post 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.


Regards,

Derek
User avatar
janbredenbeek
Super Gold Card
Posts: 629
Joined: Wed Jan 21, 2015 4:54 pm
Location: Hilversum, The Netherlands

Re: Link ASM and C code

Post 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.


User avatar
tofro
Font of All Knowledge
Posts: 2685
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Link ASM and C code

Post 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


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
tcat
Super Gold Card
Posts: 633
Joined: Fri Jan 18, 2013 5:27 pm
Location: Prague, Czech Republic

Re: Link ASM and C code

Post 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


Post Reply