Saturday, December 10, 2011

keil C Programming Tutorial: Interfacing C programs to Assembler

►Function Parameters

C51 make use of registers and memory locations for passing parameters. By default C function pass up to three parameters in registers and further parameters are passed in fixed memory locations. You can disable parameter passing in register using NOREGPARMS keyword. Parameters are passed in fixed memory location if parameter passing in register is disabled or if there are too many parameters to fit in registers.

►Parameter passing in registers

C functions may pass parameter in registers and fixed memory locations. Following table gives an idea how registers are user for parameter passing.
parameter passing to functions
Following example explains a little more clearly the parameter passing technique:
example parameter passing to functions

►Parameter passing in Fixed Memory Locations

Parameters passed to assembly routines in fixed memory lcoation use segments named

?function_name?BYTE : All except bit parameters are defined in this segment.

?function_name?BIT : Bit parameters are defined in this segment.

All parameters are assigned in this space even if they are passed using registers. Parameters are stored in the order in which they are declared in each respective segment.

The fixed memory locations used for parameters passing may be in internal data memory or external data memory depending upon the memory model used. The SMALL memory model is the most efficient and uses internal data memory for parameter segment. The COMPACT and LARGE models use external data memory for the parameter passing segments.

►Fucntion Return Values

Function return values are always passed using CPU registers. The following table lists the possible return types and the registers used for each.
function return values

►Example

Following example shows how these segment and function decleration is done in assembler.
CODE:
;Assembly program example which is compatible
;and called from any C program

;lets say asm_test.asm is file name

name
asm_test

;We are going to write a function

;add which can be used in c programs as

; unsigned long add(unsigned long, unsigned long);

; as we are passing arguments to function

;so function name is prefixed with '_' (underscore)


;code segment for function "add"

?PR?_add?asm_test segment code
;data segment for function "add"

?DT?_add?asm_test segment data

;let other function use this data space for passing variables

public
?_add?BYTE
;make function public or accessible to everyone

public
_add

;define the data segment for function add

rseg
?DT?_add?asm_test
?_add?BYTE:
parm1:  DS 4    ;First Parameter
parm2:  ds 4    ;Second Parameter

;either you can use parm1 for reading passed value as shown below

;or directly use registers used to pass the value.

rseg
?PR?_add?asm_test
_add:
;reading first argument

        mov parm1+3,r7
        mov parm1+2,r6
        mov parm1+1,r5
        mov parm1,r4
;param2 is stored in fixed location given by param2


;now adding two variables

        mov a,parm2+3
        add a,parm1+3
;after addition of LSB, move it to r7(LSB return register for Long)

        mov r7,a
        mov a,parm2+2
        addc a,parm1+2
;store second LSB

        mov r6,a
        mov a,parm2+1
        addc a,parm1+1
;store second MSB

        mov r5,a
        mov a,parm2
        addc a,parm1
;store MSB of result and return


;keil will automatically store it to

;varable reading the resturn value

        mov r4,a
        ret
        end
 

Now calling this above function from a C program is very simple. We make function call as normal function as shown below:
CODE:
extern unsigned long add(unsigned long, unsigned long);

void main(){

        unsigned long a;
        a = add(10,30);
        //a will have 40 after execution
        while(1);
}
 

4 comments:

  1. You just copied and pasted the example. What is the bloody use?
    What was needed was to explain the purpose of each item in the segment declaration and so forth. Perhaps you yourself do not understand and have never tried this code. Show me real world practical example, then I will believe that you know what you are talking about.

    ReplyDelete