; export symbols XDEF asm_main ; we use export 'Entry' as symbol. This allows us to ; reference 'Entry' either in the linker .prm file ; or from C/C++ later on ; include derivative specific macros INCLUDE 'mc9s12c32.inc' ; variable/data section MY_EXTENDED_RAM: SECTION ; Insert here your data definition. For demonstration, temp_byte is used. temp: DS.W 1 ; code section MyCode: SECTION ;************************************************************************** ;Parameter transfer between C and assembly for MetroWerks HC12 C Compiler: ; In the MAIN.C program,this assembly function is called: ; ; zz = asm_main(xx,yy,&ww); ; ; Note that xx,yy,and ww are integer variables. ; The leftmost argument (in this case, xx) is pushed on the stack ; The next argument (in this case, yy) is pushed on the stack. ; This process is repeated, working from left to right until the final ; (rightmost) input argument is reached. ; The rightmost argument (in this case, &ww) is left in the D register ; in order to make the program execute faster. Then as the subroutine ; asm_main is called via a JSR instruction, the return address (PC) gets pushed ; on the stack. Therefore, in this example, from inside the asm_main subroutine, ; xx is passed on the stack at 4,sp, yy is passed on the stack at 2,sp, ; and &ww, the address of variable ww, is passed in the D register. ; The asm_main program must return the int value of the ; function in register D (= x+y). (If the function were to return a char (8-bit) ; value, it would have to do so in register B,) The address passed as the rightmost ; argument must be written to with the result (= 2*(x+y)). After ; returning from the assembly program "asm_main" to the C program "main.c", ; The calling C program must clean the input arguments off of ; the stack. Because the rightmost input argument was not passed on the ; stack, there are only 2 x 2 = 4 bytes to be cleaned off of the stack, ; so the C compiler inserts an LEAS 4,SP instruction immediately after ; the JSR to "asm_main". ;****************************************************************************/ asm_main: tfr d,x ;x points to address of RAM location "ww" ldd 4,sp ;get leftmost input argument off stack addd 2,sp ;add it to next input argument std temp ;store (x+y) result in RAM location "temp" asld ;place 2*(x+y) to addr passed as 3rd (&w) std 0,x ldd temp ;return output value of function (= x+y) in register D RTS ; return to caller