The Datasheet Archive - 100 Million Datasheets from 7500 Manufacturers.    


Datasheet Search Engine   
 
Part # or Description: • 5V RS232 Driver • 2SC5066* • "Real Time Clock" • "USB connector" • "blue led" 5mm • 10 watt zener diode • 2N3055* motorola
 
Search Tip: Try entering the part number only. Include a wildcard (eg. lm317* or 1n4148*)

 

 

AN2093 Creating Efficient Code MC68HC08 Freescale Semiconduc


Datasheet Thumbnail

  

Download PDF



Top Searches for this datasheet



Order this document AN2093/D
AN2093
Creating Efficient Code MC68HC08
Freescale Semiconductor, Inc.
Stuart Robb East Kilbride, Scotland
Introduction
programming language powerful, flexible potentially portable high-level programming language. These other features, such support low-level operations, make this useful language programming embedded applications. Many embedded applications low-cost microcontrollers with 8-bit data bus. Such microcontrollers often have limited on-chip resources, such registers limited amounts ROM. Compared other 8-bit microcontrollers, HC08 architecture well suited programming language. effective instruction with addressing modes which enable efficient implementation instructions. instruction includes instructions manipulating stack pointer. addressing modes include indexed addressing modes with index contained index register stack pointer register. These features allow efficient access local variables. language used successfully create program HC08 microcontroller, produce most efficient machine code, programmer must carefully construct language program. this context, "efficient code" means compact code size fast execution time. programmer must only create efficient high level design, also attention detailed implementation. Principally, efficiency improvements obtained appropriate design data structures data types. Programmers accustomed coding assembly language will familiar with these issues programmers should remember that their code converted into assembly language compiler. actual fact, compiler will recognise certain constructs replace them with functions in-line code which have often been hand coded assembly. Thus
Motorola, Inc., 2000
More Information This Product, www.freescale.com
AN2093
Freescale Semiconductor, Inc. Application Note
compiler more efficient that good assembly programmer. however, much easier write good code which converted into efficient assembly code than write efficient assembly code hand. potential drawback that very easy create code which compiler, matter good, convert into efficient assembly code. Some hints tips presented this paper programmer write their code which compiler convert into efficient machine code. Some these tips will also improve portability code. Examples given based real compiler output generated Hiware HC08 compiler from Metrowerks Europe (formerly Hiware AG), Motorola company.
Freescale Semiconductor, Inc.
CPU08 Register Model
Stack pointer (SP) Program counter (PC) Condition code register (CCR) Accumulator Index register (H:X)
Carry/borrow flag Zero flag Negative flag Interrupt mask Half-carry flag Two's complement overflow flag
Figure CPU08 Register Model overview CPU08 register model given here completeness. CPU08 five registers which part memory map. These registers briefly described. Accumulator accumulator general purpose 8-bit register. uses accumulator hold operands results operations. 16-bit index register called used indexed addressing modes determine effective address operand. index
AN2093
Index Register
More Information This Product, www.freescale.com
MOTOROLA
Addressing Modes
register access byte address space this mode. lower byte used hold operand instructions. also serve temporary data storage location. Stack Pointer 16-bit stack pointer used hold address next available location stack. uses contents stack pointer register index access operands stack stack pointer offset addressing modes. stack located anywhere where there byte address space. 16-bit program counter contains address next instruction operand fetched. program counter access byte address space. 8-bit condition code register contains global interrupt mask five flags that indicate results instruction just executed. Bits permanently logic
Freescale Semiconductor, Inc.
Program Counter
Condition Code Register
Addressing Modes
CPU08 different addressing modes. brief overview given here. Inherent Inherent instructions have operand fetch require operand address. Most byte long. operand immediate instructions contained bytes immediately following opcode. Immediate instructions therefore have constant operands. Direct instructions used access operands direct page, i.e. address range $0000 $00FF. high-order byte address included instruction, thus saving byte execution cycle compared extended addressing. Extended instructions access operands address byte memory map. extended instructions three bytes long. Indexed instructions contents 16-bit index register access operands with variable addresses, such variables accessed
Immediate
Direct
Extended
Indexed
AN2093 MOTOROLA
More Information This Product, www.freescale.com
Freescale Semiconductor, Inc. Application Note
through pointer. There five modes indexed addressing: indexed, offset, with without post-increment index register; indexed, 8-bit offset, with without post-increment index register; indexed with 16-bit offset. Stack Pointer Stack pointer instructions similar indexed instructions only they contents stack pointer address operand instead index register. There modes stack pointer addressing: stack pointer with 8-bit offset with 16-bit offset. Stack pointer instructions require extra byte extra execution cycle compared equivalent indexed instruction. conditional branch instructions relative addressing evaluate effective address. branch condition true, evaluates branch destination adding signed byte following opcode program counter. branch range -128 +127 bytes from address after branch instruction. Memory memory instructions copy data from location another. locations always direct page. There four modes memory memory instructions: move immediate data direct location; move direct location direct location; move indexed location direct location with post-increment index register; move direct location indexed location with post-increment index register.
Freescale Semiconductor, Inc.
Relative
Memory Memory
Basic Data Types
Table Basic Data Types
Data Type char short long Size bits bits bits bits Range (unsigned) 65535 65535 4294967295 Range (signed) -128 -32768 32767 -32768 32767 -2147483648 2147483647
Easily greatest savings code size execution time made choosing most appropriate data type variables. This particularly true 8-bit microcontrollers where natural internal data size 8-bits (one byte) whereas preferred data type `int'. ANSI standard does precisely define size native types,
AN2093
More Information This Product, www.freescale.com
MOTOROLA
Basic Data Types
compilers 8-bit microcontroller's usually implement `int' signed 16-bit value. 8-bit microcontrollers process 8-bit data types more efficiently than 16-bit types, `int' larger data types should only used where required size data represented. Double precision floating point operations particularly inefficient should avoided wherever efficiency important. This seem obvious, often overlooked huge impact code size execution time. well magnitude required data type, signedness must also specified. ANSI standard specifies `int' signed default, `char' defined vary between compilers. Thus create portable code, data type `char' should used all. Instead signedness should defined explicitly: `unsigned char' `signed char'. good practice create type definitions these data types header file which then included every other file. also worthwhile create type definitions other data types which used well, consistency, allow portability between compilers. Something like following used:
typedef unsigned char UINT8; typedef signed char SINT8; typedef unsigned UINT16; typedef SINT16; typedef unsigned long UINT32; typedef long SINT32;
Freescale Semiconductor, Inc.
variable typically used more than expression, some those expressions require full data size signedness variable. this case, savings made casting variable, part expression containing variable, most appropriate data size. Summary: Create type definitions data types used. smallest data type appropriate each variable. signed data types only when required. casts within expressions reduce data types minimum required.
AN2093 MOTOROLA
More Information This Product, www.freescale.com
Freescale Semiconductor, Inc. Application Note Local versus Global Variables
Variables classified their scope. Global variables accessible part program allocated permanent storage RAM. Local variables accessible only function within which they declared allocated storage stack. Local variables therefore only occupy while function which they belong running. Their absolute address cannot determined when code compiled linked they allocated memory relative stack pointer. access local variables compiler stack pointer addressing mode. This addressing mode requires extra byte extra cycle access variable compared same instruction indexed addressing mode. code requires several consecutive accesses local variables, compiler will usually transfer stack pointer 16-bit index register indexed addressing instead. 'static' access modifier used with local variables. This causes local variable permanently allocated storage memory, like global variable, variable's value preserved between function calls. However static local still only accessible function within which declared. Global variables allocated permanent storage memory absolute address determined when code linked. memory occupied global variable cannot reused other variable. Global variables protected way, part program access global variable time. This gives rise issue data consistency global variables more than single byte size. This means that variable data could corrupted part variable derived from value rest variable derived from another value. Inconsistent data arises when global variable accessed (read written) part program before every byte variable been accessed program interrupted. This hardware interrupt example, operating system, used. global variable then accessed interrupting routine then inconsistent data result. This must avoided reliable program execution desired this often achieved disabling interrupts while accessing global variables. 'static' access modifier also used with global variables. This gives some degree protection variable restricts access variable those functions file which variable declared. compiler will generally extended addressing mode access global variables indexed addressing mode they accessed though pointer. global variables does generally result significantly more efficient code than local variables. There some
AN2093
Freescale Semiconductor, Inc.
More Information This Product, www.freescale.com
MOTOROLA
Direct Page Variables
limited exceptions this generalisation, being when global variable located direct page. However, global variables prevents function from being recursive reentrant, often does make most efficient RAM, which limited resource most microcontrollers. programmer must therefore make careful choice deciding which variables, any, make global scope. Worthwhile gains efficiency sometimes obtained making just most intensively used variables global scope, particularly these variables located direct page.
Freescale Semiconductor, Inc.
Summary: Careful analysis required when deciding which variables make global scope.
Direct Page Variables
address range $0000 $00FF called direct page, base page zero page. M68HC08 microcontrollers, lower part direct page always contains control registers upper part direct page always contains RAM. After reset, stack pointer always contains address $00FF. direct page important because most CPU08 instructions have direct addressing mode whereby they access operands direct page clock cycle less than extended addressing mode. Furthermore direct addressing mode instruction requires less byte code. highly efficient instructions will only work with direct page operands. These are: BSET, BCLR, BRSET BRCLR. instruction requires operands direct page. compiler cannot take advantage efficient direct addressing mode unless variables explicitly declared direct page. There ANSI standard doing this compilers generally offer different solutions. Hiware compiler uses #pragma statement:
#pragma UINT16 #pragma DATA_SEG SHORT myDirectPageVars myDirectPageVar1; unsigned direct page DATA_SEG DEFAULT
This declares direct page segment myDirectPageVars which contains variable myDirectPageVar1 which accessed using direct addressing mode. programmer must remember make linker place myDirectPageVars segment address
AN2093 MOTOROLA
More Information This Product, www.freescale.com
Freescale Semiconductor, Inc. Application Note
direct page. amount direct page always limited, only most intensively used variables should located direct page. release more direct page global variables, stack relocated outwith direct page, available. This will affect stack pointer addressing modes. Many control registers located direct page they should declared such compiler direct addressing mode where possible. possible ways this are: Define register name address together:
Freescale Semiconductor, Inc.
#define PortA (*((volatile UINT8 *)(0x0000))) #define PortB (*((volatile UINT8 *)(0x0001)))
define register names direct page segment define segment address link time:
#pragma DATA_SEG SHORT myDirectPagePortRegisters volatile UINT8 PortA; volatile UINT8 PortB; #pragma DATA_SEG DEFAULT
Summary: Declare direct page registers compiler. only most frequently used variables direct page. Release more direct page variables relocating stack.
Loops
loop executed less than times, 'unsigned char' loop counter type. loop executed more than times, 'unsigned int' loop counter. This because 8-bit arithmetic more efficient than 16-bit unsigned arithmetic more efficient than signed. value loop counter immaterial, more efficient decrement counter compare with zero than increment compare with non-zero value. This optimisation effective loop must executed with loop counter equal zero, such when loop counter used index array element first element must accessed.
AN2093
More Information This Product, www.freescale.com
MOTOROLA
Data Structures
loop counter used expressions within loop, remember cast most appropriate data type each time used. When loop performed fixed number times that number small, such three four, often more efficient have loop all. Instead, write code explicitly many times required. This will result more lines code will often generate less assembly code execute much faster than loop. actual savings will vary, depending code executed. summary
Freescale Semiconductor, Inc.
smallest appropriate unsigned type loop counter. Decrement loop counter compare with zero where possible. loop counter used within loop, cast most appropriate data type. loop code executed small, fixed number times.
Data Structures
When programming easy create complex data structures, example array structures with each structure containing number different data types. This will produce complex slow code 8-bit microcontroller which limited number registers indexing. Each level de-referencing will result multiplication element number element size, with result probably pushed onto stack order next calculation. Structures should avoided where possible data structures kept simple. This done organising data into simple one-dimensional arrays simple data type. This will result greater number arrays, program will able access data much more quickly. structures unavoidable, they should passed function argument function return value, they should passed reference instead. Summary complex data structures.
AN2093 MOTOROLA
More Information This Product, www.freescale.com
Freescale Semiconductor, Inc. Application Note Examples
Examples assembly code generated Hiware HC08 compiler described this section, based following type definitions:
typedef unsigned char UINT8; typedef signed char SINT8; typedef unsigned UINT16; typedef SINT16;
Freescale Semiconductor, Inc.
Register1
This example illustrates manipulation register direct page (PORTA) direct page (CMCR0). Setting clearing more bits register direct page requires bytes cycles. Setting clearing multiple bits register direct page requires bytes cycles. Setting clearing single register direct page requires bytes cycles.
Code Assembly Code Bytes Cycles
#define PORTA #define CMCR0
(*((volatile UINT8 *)(0x0000))) (*((volatile UINT8 *)(0x0500)))
void register1(void) CMCR0 ~0x01; PORTA 0x03; PORTA ~0x02;
bit1 b1,2 bit2
LDHX BSET
#0x0500 #0xFE 0x00 #0x03 0x00 0,0x00
AN2093
More Information This Product, www.freescale.com
MOTOROLA
Examples Datacopy1
Datacopy1
This example inappropriate `int' variable which used both loop counter array index. compiler forced signed 16-bit arithmetic calculate address each element dataPtr[]. This routine requires bytes with iterations loop, executes cycles.
Code UINT8 buffer[4]; void datacopy1(UINT8 dataPtr) i++) buffer[i] dataPtr[i];
Assembly Code
Bytes
Cycles
PSHA PSHX PSHA PSHA PULH PULX PSHX 3,SP PULH buffer,X PSHA PULH CPHX #0x0004 *-39
Freescale Semiconductor, Inc.
AN2093 MOTOROLA
More Information This Product, www.freescale.com
Freescale Semiconductor, Inc. Application Note Datacopy2
this example, loop counter array index variable optimised `unsigned char'. This routine requires bytes ROM, bytes less than Datacopy1. With four iterations, Datacopy2 executes cycles, cycles less than Datacopy1. this example value loop counter important; loop must execute with significant improvement obtained this case decrementing loop counter instead incrementing. Also, significant improvement obtained this case variable `buffer' place direct page: instruction `STA buffer,X' uses direct addressing instead extended, saving byte code cycle iteration.
Freescale Semiconductor, Inc.
Code UINT8 buffer[4]; void datacopy2(UINT8 dataPtr) UINT8 i++) buffer[i] dataPtr[i];
Assembly Code
Bytes
Cycles
PSHA PSHX PSHH PSHA CLRA PSHA PULH PULX CLRH buffer,X #0x04 *-25
AN2093
More Information This Product, www.freescale.com
MOTOROLA
Examples Datacopy3
Datacopy3
this example, data copied without using loop. This routine requires bytes executes cycles. This bytes less fewer cycles than Datacopy2. bytes were copied, this method would require bytes more than Datacopy2, would execute fewer cycles. Although there potential savings variable `buffer' located direct page, compiler does take full advantage them this case.
Freescale Semiconductor, Inc.
Code UINT8 buffer[4]; void datacopy3(UINT8 buffer[0] buffer[1] buffer[2] buffer[3]
Assembly Code
Bytes
Cycles
dataPtr) dataPtr[0]; dataPtr[1]; dataPtr[2]; dataPtr[3];
PSHX PULH
buffer buffer:0x1 buffer:0x2 buffer:0x3
AN2093 MOTOROLA
More Information This Product, www.freescale.com
Freescale Semiconductor, Inc. Application Note Loop1
only number iterations matter value loop counter, more efficient decrement loop counter compare with zero. this example `for' statement requires bytes ROM. increment test loop counter take cycles each iteration. This saves bytes cycles iteration compared `for' statement Datacopy2. However this optimisation cannot applied Datacopy2 code within this loop executed with
Code
Assembly Code
Bytes
Cycles
Freescale Semiconductor, Inc.
void loop1(void) UINT8 for(i=4; i!=0; code
PSHH #0x04 DBNZ ,X,*-offset PULH
AN2093
More Information This Product, www.freescale.com
MOTOROLA
Examples Loop1
Freescale Semiconductor, Inc.
AN2093 MOTOROLA
More Information This Product, www.freescale.com
Freescale Semiconductor, Inc.
Motorola reserves right make changes without further notice products herein. Motorola makes warranty, representation guarantee regarding suitability products particular purpose, does Motorola assume liability arising application product circuit, specifically disclaims liability, including without limitation consequential incidental damages. "Typical" parameters which provided Motorola data sheets and/or specifications vary different applications actual performance vary over time. operating parameters, including "Typicals" must validated each customer application customer's technical experts. Motorola does convey license under patent rights rights others. Motorola products designed, intended, authorized components systems intended surgical implant into body, other applications intended support sustain life, other application which failure Motorola product could create situation where personal injury death occur. Should Buyer purchase Motorola products such unintended unauthorized application, Buyer shall indemnify hold Motorola officers, employees, subsidiaries, affiliates, distributors harmless against claims, costs, damages, expenses, reasonable attorney fees arising directly indirectly, claim personal injury death associated with such unintended unauthorized use, even such claim alleges that Motorola negligent regarding design manufacture part. Motorola registered trademarks Motorola, Inc. Motorola, Inc. Equal Opportunity/Affirmative Action Employer.
reach USA/EUROPE: Motorola Literature Distribution; P.O. 5405, Denver, Colorado 80217. 1-303-675-2140 HOME PAGE: http://motorola.com/sps/ JAPAN: Motorola Japan Ltd.; SPS, Technial Information Center, 3-20-1, Minami-Azabu, Minato-ku, Tokyo 106-8573 Japan. 81-3-3440-3569 ASIA/PACIFIC: Motorola Semiconductors H.K. Ltd.; Silicon Harbour Centre, King Street, Industrial Estate, N.T., Hong Kong. 852-266668334 CUSTOMER FOCUS CENTER: 1-800-521-6274
Motorola, Inc., 2000
More Information This Product, www.freescale.com
AN2093/D

Other recent searches


TC0185A - TC0185A   TC0185A Datasheet
TB6562ANG - TB6562ANG   TB6562ANG Datasheet
SCAS067A - SCAS067A   SCAS067A Datasheet
PLL130-08 - PLL130-08   PLL130-08 Datasheet
LB1258 - LB1258   LB1258 Datasheet
ELS-450 - ELS-450   ELS-450 Datasheet
AL4CE211 - AL4CE211   AL4CE211 Datasheet
AL4CE221 - AL4CE221   AL4CE221 Datasheet
AL4CE231 - AL4CE231   AL4CE231 Datasheet
AL4CE241 - AL4CE241   AL4CE241 Datasheet
AL4CE251 - AL4CE251   AL4CE251 Datasheet

 

Privacy Policy | Disclaimer
© 2012 Datasheet Archive