Link ASM and C code

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

Link ASM and C code

Postby tcat » Thu May 16, 2019 8:03 am

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: 558
Joined: Fri Jan 18, 2013 5:27 pm
Location: Prague, Czech Republic

Re: Link ASM and C code

Postby tcat » Sat Jun 01, 2019 5:17 pm

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
QL Wafer Drive
Posts: 1464
Joined: Mon Dec 20, 2010 11:40 am
Location: Runcorn, Cheshire, UK

Re: Link ASM and C code

Postby Derek_Stewart » Sat Jun 01, 2019 7:23 pm

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
Gold Card
Posts: 333
Joined: Wed Jan 21, 2015 4:54 pm
Location: Hilversum, The Netherlands

Re: Link ASM and C code

Postby janbredenbeek » Sat Jun 01, 2019 10:05 pm

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
QL Wafer Drive
Posts: 1640
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Link ASM and C code

Postby tofro » Sun Jun 02, 2019 12:04 am

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: 558
Joined: Fri Jan 18, 2013 5:27 pm
Location: Prague, Czech Republic

Re: Link ASM and C code

Postby tcat » Sun Jun 02, 2019 4:37 pm

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



Who is online

Users browsing this forum: No registered users and 3 guests