Creating a function in assembly

Any assembly label is considered a name of a function. If an assembly function is not prototyped, it is assumed to follow the prototype void func(void). If the assembly function requires parameters or returns a value, then it must be prototyped.

Figure 6-3. An assembly function asm2.c


#asm
clear_bank2_ram
        LD  S,#2                ;select RAM bank 2
        LD  B,#RAM_BANK2_SIZE-1 ;set B to RAM bank size
clear_next
        LD   A,B
        IFEQ A,#0               ;test B for zero
        RET
        CLR  A                
        X    A,[B-]             ;clear [B]
        JP   clear_next
#endasm

void main(void)
{
        long l;

        clear_bank2_ram();
        init_long_global(0x1234);
        l=read_long_global();

        while(1);
}

    

If you pass one byte to a function, it is placed in A register before the CALL. If you pass two 8-bit values to a function, the first byte is placed in A and the second is placed in X. If you pass one 16-bit value to a function, the least significant byte is places in A and the most significant byte is placed in X.

Figure 6-4. An assembly function that requires a parameter asm2.c

void init_long_global(long);

#asm
init_long_global
        LD  S,#long_global>>8   ;select long_global RAM bank
        LD  B,#long_global      ;load B with long_global address
        X   A,[B+]              ;store A to long_global low
        X   A,X                 ;load X into A
        X   A,[B]               ;store X to long_global high
        RET                     ;return
#endasm


    

If your assembly language function needs to return an 8-bit value, the compiler expects it in the A register. If you need to return two bytes, they are expected in A:X. The most significant byte must be placed in X and the least significant byte must be placed in A.

Figure 6-5. An assembly function that returns a value asm2.c

long read_long_global(void);

#asm
read_long_global
        LD  S,#long_global>>8   ;select long_global RAM bank
        LD  B,#long_global+1    ;load B with long_global address
        LD  A,[B-]              ;load long_global high
        X   A,X                 ;store long_global high into X
        LD  A,[B]               ;load long_global low to A
        RET
#endasm