Keil Logo

Compact Model Example

In the compact model, parameters passed in fixed memory locations are stored in external data memory. The parameter passing segment for variables is located in the pdata area.

The following are two assembly code examples. The first shows you how the example function is called from assembly. The second example displays the assembly code for the example function.

Calling a C function from assembly.

EXTRN CODE    (_function)         ; Ext declarations for function names
EXTRN XDATA   (?_function?BYTE)   ; Seg for local variables
EXTRN BIT     (?_function?BIT)    ; Seg for local bit variables
.
.
.
        MOV   R6,#HIGH intval           ; int  a
        MOV   R7,#LOW  intval           ; int  a
        MOV   R5,#charconst             ; char b
        SETB  ?_function?BIT+0          ; bit  c
        MOV   R0,#?_function?BYTE+3     ; Addr of 'v_d' in the passing area
        MOV   A,longval+0               ; long d
        MOVX  @R0,A                     ; Store parameter byte
        INC   R0                        ; Inc parameter passing address
        MOV   A,longval+1               ; long d
        MOVX  @R0,A                     ; Store parameter byte
        INC   R0                        ; Inc parameter passing address
        MOV   A,longval+2               ; long d
        MOVX  @R0,A                     ; Store parameter byte
        INC   R0                        ; Inc parameter passing address
        MOV   A,longval+3               ; long d
        MOVX  @R0,A                     ; Store parameter byte
        MOV   C,bitvalue
        MOV   ?_function?BIT+1,C        ; bit  e
        LCALL _function
        MOV   intresult+0,R6            ; Store int
        MOV   intresult+1,R7            ; Retval
.
.
.

Assembly code for the example function:

NAME          MODULE                  ; Name of the program module
?PR?FUNCTION?MODULE  SEGMENT  CODE    ; Seg for program code in 'function';
?PD?FUNCTION?MODULE  SEGMENT  XDATA OVERLAYABLE IPAGE
                                      ; Seg for local vars in 'function'
?BI?FUNCTION?MODULE  SEGMENT  BIT   OVERLAYABLE
                                      ; Seg for local bit vars in 'function'

PUBLIC        _function, ?_function?BYTE, ?_function?BIT
                                      ; Public symbols for C function call

RSEG          ?PD?FUNCTION?MODULE     ; Segment for local variables
?_function?BYTE:                      ; Start of the parameter passing seg
v_a:    DS   2                        ;   int variable: v_a
v_b:    DS   1                        ;   char variable: v_b
v_d:    DS   4                        ;   long variable: v_d
.
.                                     ; Additional local variables
.

RSEG          ?BI?FUNCTION?MODULE     ; Segment for local bit variables
?_function?BIT:                       ; Start of the parameter passing seg
v_c:    DBIT  1                       ;   bit variable: v_c
v_e:    DBIT  1                       ;   bit variable: v_e
.
.                                     ; Additional local bit variables
.

RSEG          ?PR?FUNCTION?MODULE          ; Program segment
_function:    MOV   R0,#?_function?BYTE+0  ; Special function prolog
              MOV   A,R6                   ; and epilog is not
              MOVX  @R0,A                  ; necessary.  All
              INC   R0                     ; vars can immediately
              MOV   A,R7                   ; be accessed
              MOVX  @R0,A
              INC   R0
              MOV   A,R5
              MOVX  @R0,A
.
.
.
              MOV   R6,#HIGH retval        ; Return value
              MOV   R7,#LOW  retval        ; int constant
              RET


  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.