1 / 13

EGE 631

EGE 631. LHO 15 C with assembly language. 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

daphne
Download Presentation

EGE 631

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EGE 631 LHO 15 C with assembly language

  2. 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

  3. Data Objects - Data objects are the variables and constants you declare in your C programs. The Cx51 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.

  4. 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.

  5. The following examples clarify how registers are selected for parameter passing.

  6. 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.

  7. ; ASM1.SRC generated from: ASM1.C NAME ASM1 ?PR?_asmfunc1?ASM1 SEGMENT CODE PUBLIC _asmfunc1 ; #pragma SRC ; #pragma SMALL ; unsigned int asmfunc1 ( RSEG ?PR?_asmfunc1?ASM1 USING 0 _asmfunc1: ;---- Variable 'arg?00' assigned to Register 'R6/R7' ---- ; SOURCE LINE # 4 ; SOURCE LINE # 6 ; return (1 + arg); ; SOURCE LINE # 7 MOV A,R7 ADD A,#01H MOV R7,A CLR A ADDC A,R6 MOV R6,A ; } ; SOURCE LINE # 8 ?C0001: RET ; END OF _asmfunc1 END

  8. $include (c8051f020.inc); Register definition file. NAME MBTN ?PR?mbtn?MBTN SEGMENT CODE ?DT?mbtn?MBTN SEGMENT DATA OVERLAYABLE EXTRN CODE (rd_buttons) PUBLIC mbtn 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 P5.3 BUTTON WAS LAST ONE PRESSED. ; INPUTS: NONE ; OUTPUTS: R7(3..0) - BITS ARE SET BASED ON LAST BUTTON PRESSED ; NOTE: ONE AND ONLY ONE BIT OF ACC(3..0) IS SET. ; R7(7..0) = UNDEFINED ; CALLS: rd_buttons ; DESTROYS: ACC, PSW, R7 ; 27 Bytes of code ;***************************************************************

  9. mbtn: USING 0 CALL rd_buttons ;GET BUTTONS MOV A,R7 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 GOT1 ;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 GOT1: MOV R7,last_button ;RETURN LAST BUTTON PRESSED RET END

More Related