AmigaExamples/021.calling_c
alpine9000 ffc5f1f93d Got build working on newer OSX, included missing ADFs 2019-06-11 13:58:03 +10:00
..
bin Removed -phxass assembler argument 2016-03-20 21:11:56 +11:00
Makefile
README.md
c_code.c Got build working on newer OSX, included missing ADFs 2019-06-11 13:58:03 +10:00
calling_c.s Removed -phxass assembler argument 2016-03-20 21:11:56 +11:00
constants.i Updates for removal of -phxass option 2016-03-21 20:35:08 +11:00
gigi.pal
includes.i
init.s Removed -phxass assembler argument 2016-03-20 21:11:56 +11:00
test.c
utils.s

README.md

calling c code

Example showing how to call a simple C function.

Firstly, we look at the vbcc ABI to see how the arguments are passed:

By default, all function arguments are passed on the stack.

So if we have a function with the prototype:

void
PokeBitplanePointers(unsigned short* copper, 
                     unsigned char* bitplanes, 
                     unsigned short interlace, 
                     unsigned short numBitplanes, 
                     unsigned short screenWidthBytes)

We need to push each argument onto the stack in the reverse order of the declaration:

        move.l  #SCREEN_WIDTH_BYTES,-(sp) ; arguments are pushed onto the stack...                            
        move.l  #SCREEN_BIT_DEPTH,-(sp)   ; in reverse order.                                                 
        move.l  #0,-(sp)                  ; 
        pea     bitplanes                 ; make sure you push the address...                                 
        pea     copperList                ; of pointers, not the value.                                      
        jsr     _PokeBitplanePointers     ; C adds an _ to all global symbols.                                
        add.l   #20,sp                    ; Need to pop the arguments from the stack. 

And after we have made the call, make sure you unwind the arguments from the stack. As seen above.

If the function returned a scalar type up to 4 bytes, it would be returned in d0. If you want to return more complex types, it's best to make a test function call in C and check the non-optimised generated code to see how to process the return value.

The C version of PokeBitplanePointers is here.

try it