EGE 631 LHO 15 C with assembly language
EGE 631 LHO 15 C with assembly language 1
There are several reasons to call an assembly routine from your C program. You may have assembly code already written that you wish to use You may need to improve the speed of a particular function You may want to manipulate SFRs or memory-mapped I/O devices directly from assembly Segment Naming Conventions 2
Data Objects - Data objects are the variables and constants you declare in your C programs. The Cx 51 compiler generates a separate segment for each memory type for which a variable is declared. The following table lists the segment names generated for different variable data objects. 3
4
5
Parameter Passing in Registers - C functions may pass parameters in registers and fixed memory locations. A maximum of 3 parameters may be passed in registers. All other parameters are passed using fixed memory locations. The following tables define what registers are used for passing parameters. 6
The following examples clarify how registers are selected for parameter passing. 7
Function 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. 8
9
10
; ASM 1. SRC generated from: ASM 1. C NAME ASM 1 ? PR? _asmfunc 1? ASM 1 SEGMENT CODE PUBLIC _asmfunc 1 ; #pragma SRC ; #pragma SMALL ; unsigned int asmfunc 1 ( RSEG ? PR? _asmfunc 1? ASM 1 USING 0 _asmfunc 1: ; ---- Variable 'arg? 00' assigned to Register 'R 6/R 7' ---; SOURCE LINE # 4 ; SOURCE LINE # 6 ; return (1 + arg); ; SOURCE LINE # 7 MOV A, R 7 ADD A, #01 H MOV R 7, A CLR A ADDC A, R 6 MOV R 6, A ; } ; SOURCE LINE # 8 ? C 0001: RET ; END OF _asmfunc 1 END 11
$include (c 8051 f 020. inc); Register definition file. NAME MBTN ? PR? mbtn? MBTN SEGMENT CODE ? DT? mbtn? MBTN SEGMENT DATA OVERLAYABLE EXTRN CODE (rd_buttons) PUBLICmbtn RSEG ? DT? mbtn? MBTN ? mbtn? BYTE: last_button: DS 1 RSEG ? PR? mbtn? MBTN ; ******************************** ; FUNCTION: mbtn - C callable routine REMEMBERS WHICH BUTTON WAS ; PRESSED LAST. IF NO BUTTONS HAVE BEEN PRESSED ; ASSUMES P 5. 3 BUTTON WAS LAST ONE PRESSED. ; INPUTS: NONE ; OUTPUTS: R 7(3. . 0) - BITS ARE SET BASED ON LAST BUTTON PRESSED ; NOTE: ONE AND ONLY ONE BIT OF ACC(3. . 0) IS SET. ; R 7(7. . 0) = UNDEFINED ; CALLS: rd_buttons ; DESTROYS: ACC, PSW, R 7 ; 27 Bytes of code ; ******************************** 12
mbtn: USING 0 CALL rd_buttons ; GET BUTTONS MOV A, R 7 JZ FIX_BTNS ; JUMP IS NO BUTTONS PRESSED MOV last_button, A ; SAVE BUTTONS ; BE SURE ONE AND ONLY ONE BUTTON SAVED FIX_BTNS: MOV A, #01 ; SET BUTTON PRESSED BIT XCH A, last_button ; SWAP LOW NIBBLES SETB ACC. 3 ; BE SURE AT LEAST ONE BIT IS SET CHKBTN: RRC A ; CHECK FOR A 1 JC GOT 1 ; EXIT IF 1 XCH A, last_button ; GET LSTBTN AND RL A ; SHIFT BIT IN LSTBTN XCH A, last_button ; SAVE LSTBTN JMP CHKBTN ; KEEP LOOKING FOR FIRST 1 GOT 1: MOV R 7, last_button ; RETURN LAST BUTTON PRESSED RET END 13
- Slides: 13