We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi All Here is my template to create quickly a function in assemlbe and call in C51 Enjoy ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Template of a function writen in assembly then call by C51 compiler ; Created by Huynh Vinh Ha - Vietnam, date 11/09/2004 ; declare fname in C51: extern unsigned char fname(unsigned char x,char y) ; replace fname with actual name of your function ; replace MAIN with actual name of main program which call you function ; Input 1st parameter put in R7, 2nd in R5, 3rd R3 ( R7/R6 for int var) ; Return value ;bit : carry flag ;char R7 ;int R6.R7 ;long R4..R6 ;float R4..R6 ;pointer R1..R3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ?PR?_fname?MAIN SEGMENT CODE ; code location ?DT?_fname?MAIN SEGMENT DATA OVERLAYABLE ; data segment PUBLIC _fname ; public name RSEG ?DT?_fname?MAIN ?_fname?BYTE: ; declare local variables used in this function x?040: DS 1 ; byte type variable z?041: DS 1 ; byte clk bit 90H.0 ; bit type var RSEG ?PR?_fname?MAIN _fname: ; begin function USING 0 ; MOV x?040,R7 ; get parameter to local variable x ; ; ------ do something ; ------- ; ACALL DELAY ; call internal subroutine if present ; MOV R7,return_value ; RET ; internal subroutine inside, for example ;DELAY: NOP ; RET END ; END OF _fname
This template makes various assumptions, and will only work for the specific return type & parameters shown. A far better approach is just to create a "dummy" 'C' function with the required return type, parameters, etc, and then compile it using the SRC directive. That way, you're guaranteed to get the correct result!
A far better approach is just to create a "dummy" 'C' function with the required return type, parameters, etc, and then compile it using the SRC directive. That way, you're guaranteed to get the correct result! Andy, isn't it sad that we have to post this every week? Erik
Or you could just read the manual. The C calling conventions are documented and easily understandable, especially if all the parameters fit into registers.