Home

Mixed C and Assembly Atmel XMEGA

image

Contents

1. ASSEMBLY FUNCTION DEFINITIONS AND USE Assembly functions are defined the same way they are in a standard assembly project using standard labels The ret command is still used to return from a function call to an assembly function Within the Assembler file s calls between functions within the same file require no special consideration However when dealing with calls to assembler functions from C code the function must be define a special way BR ER KO HK OK HK OK HK KK KK TNT TIAL TZA TIONS 4 2 2 2 2 He 2 KK OK 2 OK OO OK extern void MAIN_ASM C Prototype that defines an externally defined function Main function required in primary C file of project int main void MAIN_ASM Single call to Vector Add Function Figure 5 Assembly Function Use c File RR A A REEE KEE REE 2 KO KK KE KDR TMARY CODE 8 EKEK EE EK EE He he ee ee 2 ee ee HR HK global MAIN_ASM The assembly function must be declared as global MAIN_ASM ret Return to call from C code Figure 6 Assembly Function Use s File The above two examples show both the c file and the s file descriptions for connection to an assembly function In the c file a C Prototype is created for the assembly function The use of the extern keyword shows that the function is externally defined The assembly function is later called in the c file In the s file the assembly function is defined by the global directive to ma
2. Static Library Project Name ExampleMixedC Location d users testhdocuments atmel studio 6 1 v Browse Solution name ExampleMixedC V Create directory for solution a e Once the project is open the standard c file will be opened with the template main function C code may be written using the main function as the starting point Assembly may be used in a new s file added to the project or directly inserted using inline assembly commands within the c file TO CREATE S ASSEMBLY FILE All files for the project are displayed in the Solution Explorer Right click on the project name in the Solution Explorer highlighted in figure and choose Add gt New Item in context menu In the new Add New Item window choose to create an Assembly File This new file will then be displayed in the Solution Explorer University of Florida EEL 4744 Dr Eric M Schwartz Department of Electrical and Computer Engineering Josh Weaver PhD Candidate Page 10 11 Revision 0 12 Feb 14 Mixed C and Assembly Atmel XMEGA Solution Explorer vax aS Solution ExampleMixedC 1 project gt Sa Dependencies Sa Output Files Sj Libraries D ExampleMixedC c n Default Search Installed Templates Assembly File ayes Hr A blank Assembly s file C File Include File XML File Figure 10 Add New Item Window University of Florida EEL 4744 Dr Eric M Schw
3. being generated Specifically a programmer may desired for a C program to call an Assembly function or vice versa For Atmel this happens in either two cases the use of s assembly files or using inline assembly commands Note that for Atmel the GCC AVR GCC compiler is used to combine c cpp or s files to create a project C ASSEMBLER When creating a C project if working with long and complex pieces of assembly code is required it is more desirable to express the code in a dedicated assembler file The portions of C code and Assembly code can be used to communicate between each other so nothing is lost allowing that the programmer pays close attention to the use of registers Typically Assembler files are very useful in cases of optimizing large pieces of code such as communication with external devices or Interrupt Service Routines Program Flash Data Section in Code Memory Space section text A byte 1 2 3 4 5 6 B byte xA OxBO OxC xDO O xE OxFO section text global MAIN_ASM The assembly function must be declared as global MAIN_ASM ldi R18 N Load the number of values ldi ZL 108 VA Load the address of program memory for VA ldi ZH hi8 VA call VADD Call the ASM function for vector addition ret Return to call from C code Figure 1 Assembler File s example INLINE ASSEMBLY In cases where smaller pieces of assembly code are used inline assembly commands directl
4. 18 IOPORT gt gt 8 asm ST Z R18 Figure 8 Inline Assembly with Operands example In the example the STS command used to define PORTK_DIRSET uses a simple placeholder operand 0 showing that the defined operand will come later in the template Then in the output operand section PORTK_DIRSET is defined as an output only memory m location address Later in the example the LDI command used to define EBI_CSO0_BASEADDR uses another simple placeholder operand 0 The operand is later shown to be an input operand defined as a constant 1 The constant is defined by using the C Pointer amp modifier on EBI_CSO0O_BASEADDR The amp designated for C to return the address of EBI_CSO0_BASEADDR University of Florida EEL 4744 Dr Eric M Schwartz Department of Electrical and Computer Engineering Josh Weaver PhD Candidate Page 9 11 Revision 0 12 Feb 14 Mixed C and Assembly Atmel XMEGA STARTING A C PROJECT To begin a C Project must be first started within Atmel Studio After opening Atmel Studio choose New Project and make sure to choose the GCC C Executable Project option Recent Templates n Default Search Installed Templates P Installed Templates GCC C Executable Project a C C Creates an AVR 8 bit or AVR ARM 32 bit C Arduino Boards project GCC C Static Library Project Type C C Atmel Boards User Boards Assembler GCC C Executable Project Atmel Studio Solution GCC C
5. University of Florida EEL 4744 Dr Eric M Schwartz Department of Electrical and Computer Engineering Josh Weaver PhD Candidate Page 1 11 Revision 0 12 Feb 14 Mixed C and Assembly Atmel XMEGA KEY WORDS Compiler Inline Assembly GNU Assembler GCC AVR GCC RESOURCES GNU Assembler Resource http sourceware org binutils docs 2 23 1 as index html AVR LibC Inline ASM Cookbook http www nongnu org avr libc user manual inline_asm html Atmel APPNote AT 1886 http Awww atmel com images doc42055 pdf Atmel APPNote AVR1000 http www atmel com Images doc8075 pdf ION WO DIOL Os BLO Beyond standard assembly programming for a specific device some devices allow the use of the C programming language to create code that will control the device When a computer program is completed in a source language such as C it must then be converted to a target language to control the desired device To do this a compiler is used to transform the source code into the desired object code e g assembly language During the conversion process the compiler attempts to optimize the order and use of object code Given that a compiler tries to be quick or simply may be poorly built it is possible that the resulting object code may not be as fast or efficient as desired A programmer who is familiar with assembly may want to take advantage of their skills to improve the resulting assembly code given by using assembly within the C program
6. artz Department of Electrical and Computer Engineering Josh Weaver PhD Candidate Page 11 11 Revision 0 12 Feb 14 Mixed C and Assembly Atmel XMEGA TROUBLESHOOTING GARBAGE AT END OF LINE If you receive an error with the following comments e required e constant value required e garbage at end of line Check any lines with preprocessor directives that begin with e g include define If comments follow the directive they must be in the form of C C style comments Any comments using assembler structure with a semicolon will result in an error DEFINING A VARIABLE WITH INLINE ASM DOES NOT WORK Using the standard method of a define will not connect correctly when used within an asm command asm define test r18 To define the register correctly you can use the command register unsigned char CTR asm ri8 Notice you will get a warning call clobbered register used for global register variable enabled by default depending on the register you use
7. at uses this should clear the register before calling compiler generated code The rest of the registers are defined as being call saved or call used call saved registers are those that a called C function may leave unaltered however assembly functions called from C should save and restore the contents of the register using the stack call used registers are available for any code to use but if calling a C function these registers should be saved since compiler generated code will not attempt to save them A table is give below to quickly define how each register is to be considered Table 1 Register Interfaces between C and Assembly Register Description Assembly code called from C Assembly code that calls C code r0 Temporary Save and restore if using Save and restore if using rl Always Zero Must clear before returning Must clear before returning r2 r17 r28 call saved Save and restore if using Can freely use r29 r18 r27 r30 call used Can freely use Save and restore if using r31 University of Florida EEL 4744 Dr Eric M Schwartz Department of Electrical and Computer Engineering Josh Weaver PhD Candidate Page 3 11 Revision 0 12 Feb 14 Mixed C and Assembly Atmel XMEGA COMMANDS AND CONSTRAINTS All commands that are given through the instruction set are available in the mixed C Assembly setup Below is a subset of the instruction set but are many of the primary comma
8. diate out Lr Out To I O Location pop T Pop Register from Stack push T Push Register on Stack rol T Rotate Left Through Carry University of Florida Department of Electrical and Computer Engineering Page 4 11 EEL 4744 Revision 0 Mixed C and Assembly Atmel XMEGA Mnemonic Constraint Meaning ror T Rotate Right Through Carry sbc T T Subtract with Carry sbci d M Subtract immediate with Carry sbi LI Set Bit in I O Register sbic LI Skip if Bit in I O Register Cleared sbiw w I Subtract Immediate from Word sbr d M Set Bit s in Register sbre r I Skip if Bit in Register Cleared sbrs r I Skip if Bit in Register Set ser d Set Register st e r Store Indirect st e T Store Indirect Post Increment st e T Store Indirect Pre Increment std b r Store Indirect with Displacement sts label r Store Direct to Data Space sub T T Subtract without Carry subi d M Subtract Immediate swap T Swap Nibbles xch Z E Exchange Though the commands are like standard assembly commands some of the constraints are described slightly differently For instance some commands that you could typically use register definitions such as XH or XL must now use their specific register declarations r27 or r26 A description of the various constraints as well as operand definitions seen in other C Assembly structures is shown in Table 3 Table 3 Command Operand Constraints Dr Er
9. ic M Schwartz Josh Weaver PhD Candidate Constraint Type Range of Values a Simple upper register PO o2 123 b Pointer Register y Z d Upper Register rl6 r31 i Constant 1 Lower Register ORIS m Memory n Value is known at compile q Stack Pointer SPH SPL T Any Register rO to r31 s Pointer Register X y Z t Scratch Temp Register rO w Upper Register pairs r24 r26 r28 r30 xi Pointer Register x x r27 r26 y Pointer Register y y r29 r28 Z Pointer Register z z r31 130 G Floating point Constant 0 0 I 6 bit positive constant 0 to 63 J 6 bit negative constant 63 to 0 M 8 bit integer constant 0 to 255 0 9 Identical to the specified operand Notice that the X register is r27 r26 y register is r29 r28 and z register is r31 r30 University of Florida EEL 4744 Dr Eric M Schwartz Department of Electrical and Computer Engineering Josh Weaver PhD Candidate Page 5 11 Revision 0 12 Feb 14 Mixed C and Assembly Atmel XMEGA SYNTAX When working with Assembler files within C there are small changes from what is expected in an assembly file e In code segments org is not needed since the compiler handles code placement e In data segments org directives are offset from the last location used by the compiler in placement e The s extension is used for assembler files e Lines using preprocessor directives defined by m
10. ke the function available to other partial programs that are linked within the project Notice that within the assembly function the ret command is still used to return from the function when it is complete INLINE ASSEMBLY USAGE Many of the rules and usage defined within the C Assembler section are used when executing inline assembly commands The following sections will detail the differences in Syntax usage as well as define the templates used when working with inline assembly University of Florida EEL 4744 Dr Eric M Schwartz Department of Electrical and Computer Engineering Josh Weaver PhD Candidate Page 7 11 Revision 0 12 Feb 14 Mixed C and Assembly Atmel XMEGA SYNTAX As described inline assembly makes use of the asm command also defined as __asm or __asm__ In special circumstances where it is important that the command should not be optimized away i e not moved from the defined position in memory the volatile modifier should be added asm volatile Notice that the volatile modifier does not need to be used at every asm call The template for the asm call follows asm volatile asm template output operand list list input operand clobber list The asm template follows the standard assembly instruction structure defined in Table 2 Commands in AVR GCC The assembly instructions are incased in quotes The asm template may use expressions to define placeholders replaced by operands defined in the o
11. lorida EEL 4744 Dr Eric M Schwartz Department of Electrical and Computer Engineering Josh Weaver PhD Candidate Page 8 11 Revision 0 12 Feb 14 Mixed C and Assembly Atmel XMEGA void VADD asm nop asm VADD_ASM push R18 Push the number of values onto the stack for later use asm movw R28 R26 mow Y X asm mov R19 R18 Copy count of values in vector to counter register Work with first vector asm LOOP asm lpm R21 Z Load values of first vector pointed to by Z asm st Y R21 Store currently loaded value into data memory pointed by Y asm dec R19 Decrement counter asm brne LOOP Loop until counter asm pop R19 Restore the count of values in vector to counter register Figure 7 Simple Inline Assembly example INLINE ASSEMBLY COMMANDS USING OPERANDS In circumstances where defined PORT names Control Registers or Memory locations are used the more advanced asm template with operands must be used In some of these situations it is also important to insure that the assembly command used is not moved during the compiler process requiring the volatile modifier to be used asm LDI R18 xFF asm volatile STS r18 m PORTK_DIRSET asm LDI R18 0x01 asm volatile STS r18 m EBI_CTRL 3 asm volatile LDI R31 hi8 i amp EBI_CS BASEADDR asm volatile LDI R30 108 0 i amp EBI_CS BASEADDR asm LDI R
12. nds that are given in various avr gcc compiling guides Notice the constraint descriptions are slightly different Table 2 Commands in AVR GCC Mnemonic Constraint Meaning adc T r Add without Carry add T T Add with Carry adiw w I Add Immediate to Word and T T Logical AND andi d M Logical AND with Immediate asr T Arithmetic Shift Right belr I Flag Clear bld r I Bit load from T to Register brbc I label Branch if Status Flag Cleared brbs I label Branch if Status Flag Set bset I Flag Set bst r I Bit Store from Register to T cbi LI Clear Bit s in I O Register cbr d I Clear Bit s in Register com T One s Complement cp T T Compare cpc T T Compare with Carry cpi d M Compare with Immediate cpse T T Compare Skip if Equal dec T Decrement elpm t z Extended Load Program Memory eor r T Exclusive OR in r I In From I O Location inc T Increment ld r e Load Indirect ld T e Load Indirect post increment ld T Load Indirect pre decrement ldd r b I Load Indirect with Displacement ldi d M Load Immediate lds r label Load Direct from data space lpm t Z Load Program Memory Isl T Logical Shift Left lsr T Logical Shift Right mov T T Copy Register movw eG Copy Register Pair mul T T Multiply Unsigned muls d d Multiply Signed neg r Two s Compliment or eG Logical OR ori d M Logical OR with Imme
13. ne a space in data memory as well as desired initial values The initial value is initially stored in program memory however the special command global __do_copy_data handles copying the data from program memory to the desired position for that information in data memory CSEG AND PROGRAM MEMORY Like assembly program memory is used to hold both program variables constants and the assembly code The following example shows how the section text structure may be used University of Florida EEL 4744 Dr Eric M Schwartz Department of Electrical and Computer Engineering Josh Weaver PhD Candidate Page 6 11 Revision 0 12 Feb 14 Mixed C and Assembly Atmel XMEGA Program Flash Data Section in Code Memory Space section text ariable1 byte 1 2 3 4 5 6 ariable2 byte xA xB xC xD OxEO OxFa RR HR 2K 2 EK 2 OK KK EK KE KDR TMARY CODE 8 8 7 2 2 2 EO eK HO KK HK KK global MAIN_ASM The assembly function must be declared as global MAIN_ASM ldi R18 N Load the number of values ldi ZL 108 VA Load the address of program memory for Variable1 ldi ZH hi8 VA call VADD Call the ASM function for vector addition ret Return to call from C code Figure 4 CSEG and Program Memory example The VariableI and Variable2 labels define a place in program memory that holds two sets of respective values Following this declaration the main body of the assembly program various functions may now be defined
14. ust use C C style comments semicolons will cause errors e Table 3 shows a comparison of the structures for assembly and C Table 4 Structure Comparisons include xxx inc include lt avr io gt dseg section data cseg section text db 1 2 3 4 byte 1 2 3 4 db message ascii message db message 0x00 asciz message dw word HIGHQ hi8Q LOW 1080 EXAMPLES DSEG AND DATA MEMORY When working with data memory the section data structure is used The below example shows various methods of using Data Memory SRAM Data Segment section data This will start at address 0x2000 as default Text asciz hello world ds b 4 Define storage of bytes 4 long ds w 2 Define storage of words 2 long byte 5 Define byte value of 5 global __do_copy_data Copy a defined variable in program memory to data memory Figure 3 DSEG and Data Memory example The Text label uses asciz to define a null terminated string Labels Variable and Variable2 are used to define storage spaces of either byte size or words size respectively Label Variable3 is used to define a specific value of 5 in a space of byte size Data memory is typically used for creating storage of variables such as defined by Variable and Variable2 In some cases it is desired to not only create a space in memory but also to define a value to be stored at that position on startup Text and Variable3 defi
15. utput operand list and list input operand sections Below is a table of possible placeholders Table 5 ASM Template Placeholders Placeholder Replaced by n By argument in operands where n 0 to 9 for argument A n The first register of the argument n bits 0 to 7 Bn The second register of the argument n bits 8 to 15 Cn The third register of the argument n bits 16 to 23 Dn The fourth register of the argument n bits 24 to 31 An The Address register X Y or Z The symbol when needed The symbol when needed N A newline to separate multiple asm commands T A tab used in generated asm The output operand list and list input operand follows the definitions given in Table 3 Command Operand Constraints The various operands may be used with various modifiers as defined in the following Table 6 Operand Modifiers Modifier Meaning Output operand amp Not used as input but only an output Input and Output Operand Operands are used by placing an operand and modifier in quotes while placing the true variable register or memory position in parenthesis Any C expression may be used within the parenthesis EXAMPLES SIMPLE INLINE ASSEMBLY COMMANDS When using simple inline assembly commands there is little difference from the look in a C Assembler s file other than the required asm function call surrounding the assembly command University of F
16. y embedded into the C code may be desirable In these cases instead of creating a separate Assembler file the asm command may be used to wrap around and insert assembler commands into C University of Florida EEL 4744 Dr Eric M Schwartz Department of Electrical and Computer Engineering Josh Weaver PhD Candidate Page 2 11 Revision 0 12 Feb 14 Mixed C and Assembly Atmel XMEGA Program Flash Data Section in Code Memory Space asm section text asm VA byte 1 2 3 4 5 6 asm VB byte xA xB xC xD OxE OxFe void VADD C Prototype for VADD Main function required in primary C file of project int main void asm ldi R18 N Load the number of values asm ldi R30 108 VA Load the address of program memory for VA asm ldi R31 hi8 VA asm ldi R26 lo8 VC Load the address of data memory storage for C result asm ldi R27 hi8 VC VADD Use C function call DONE while loop once main program is complete while 1 Figure 2 Inline Assembly example C ASSEMBLER S USAGE REGISTERS When using the GCC compiler in a C program registers are used slightly differently from a standard assembly project First r0 is defined as a temporary register which may be used by compiler generated code Any assembly code that uses r0 and calls a C function should save and restore the register r1 is assumed to always be zero by the compiler so any assembly code th

Download Pdf Manuals

image

Related Search

Related Contents

VizRoom User's Manual - University of Alberta  Notes SOMMAIRE 1. DOMAINE D`UTILISATION  PM-700、ST-770をお使いの方  NEC LCD8205 LCD User Manual  GUÍA RÁPIDA FRENIC MEGA Variador  Denon DVP-602CI User's Manual    JVC CA-MXS5RMDB User's Manual  E&J TRAVELER 3F02-INS-LAB-REVB10 User's Manual  取扱説明書 (12.17 MB/PDF)  

Copyright © All rights reserved.
Failed to retrieve file