ECE 2510 Introduction toECE 2510 Introduction to...

27
ECE 2510 Introduction to ECE 2510 Introduction to Microprocessors Chapter 2 ICCv7 for CPU12 (ICC12) Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering College of Engineering and Applied Sciences College of Engineering and Applied Sciences

Transcript of ECE 2510 Introduction toECE 2510 Introduction to...

Page 1: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

ECE 2510 Introduction toECE 2510 Introduction to Microprocessors

Chapter 2pICCv7 for CPU12 (ICC12)

Dr. Bradley J. BazuinAssociate Professor

Department of Electrical and Computer EngineeringCollege of Engineering and Applied SciencesCollege of Engineering and Applied Sciences

Page 2: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

Chapter 2 Overviewp

• Assembly Programming Using ICC12y g g g– Assembly Language Code Syntax– Assembler Directives

Assembly Language for Simple Tasks– Assembly Language for Simple Tasks

• The Imagecraft ICC12 Assembler Information may be found on-line at

– http://www.dragonsgate.net/pub/help/icc12/wwhelp/wwhimpl/js/html/wwhelp.htm

– Select Tool References and then select:• Assembler SyntaxAssembler Syntax• Assembly Directives• Assembly Instructions

– They are also on the solution set web site as * pdf files

ECE 2510 2

– They are also on the solution set web site as .pdf files.

Page 3: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

Assembly Language Program Structurey g g g

• Assembly program consists of a sequence of statements y p g qthat tells the controller to perform the desired operation– Lab1 task1 and task2 code!

• HCS12 assembly programs consists of three sections– Assembler Directives – Assembly Language Instructions– Comments

ECE 2510 3

Page 4: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

Assembly Program Elementsy g

• Assembler directives– Defines data and symbol– Reserves and initializes memory locations

Sets assembler and linking condition– Sets assembler and linking condition– Specifies output format– Specifies the end of a program

• Assembly language instructions– HCS12/MC9S12 instructions

• Comments• Comments– Explains the function of a single or a group of instructions

ECE 2510

Page 5: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

Fields of a HCS12 Instruction

label:[:] operation operands ;comments• Label field

– OptionalStarts with a letter and followed by

• Operand field– Follows the operation field and is – Starts with a letter and followed by

letters, digits, or special symbols – Can start from any column if ended

with “:”

pseparated from the operation field by at least one space

– Contains operands for instructions or f bl di i

– Must start from column 1 if not ended with “:”

• Operation field (opcode)

arguments for assembler directives• Comment field

– Any line starts with an * or ; is a p ( p )– Contains the mnemonic of a machine

instruction or an assembler directive– Separated from the label by at least

comment– Separated from the operand and

operation field for at least one spaceOptional

ECE 2510

one space – Optional

Page 6: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

Identify the Four Fields of an I t ti

Example

Instruction

loop: ADDA #$40 ; add 40 to accumulator A

(1) “loop” is a label(2) “ADDA” is an instruction mnemonic(2) ADDA is an instruction mnemonic(3) “#$40” is the operand(4) “add #$40 to accumulator A” is a comment

movb 0 X 0 Y ; memory to memory copymovb 0,X,0,Y ; memory to memory copy

(1) no label field(b) “movb” is an instruction mnemonic(c) “0 X 0 Y” is the operand field(c) 0,X,0,Y is the operand field(d) “; memory to memory copy” is a comment

ECE 2510

Page 7: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

Names for Labels and Symbolsy

• All names in the assembler must conform to the following gspecification: (‘_’ | [a-Z]) [ [a-Z] | [0-9] | ‘_’ ] *

• That is, a name must start with either an underscore ( _) or l h b ti h t f ll d b fan alphabetic character, followed by a sequence of

alphabetic characters, digits, or underscores. • In this document, names and symbols are synonyms for t s docu e t, a es a d sy bo s a e sy o y s o

each other. A name is either the name of a symbol, which is a constant value, or the name of a label, which is the value of the Program Counter (PC) at that moment Avalue of the Program Counter (PC) at that moment. A name can be up to 30 characters in length. Names are case-sensitive except for instruction mnemonics and assembler

ECE 2510 7

directives.Imagecraft, “Assembler Syntax”, see

http://www.dragonsgate.net/pub/help/icc12/wwhelp/wwhimpl/js/html/wwhelp.htm

Page 8: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

Name Visibilityy

• A symbol may either be used only within a program y y y p gmodule or it can be made visible to other modules. – In the former case, the symbol is said to be a local symbol.

In the latter case it is called a global symbol– In the latter case, it is called a global symbol.

• If a name is not defined within the file in which it is referenced, then it is assumed to be defined in another module and its value will be resolved by the linker. – The linker is sometimes referred to as a relocatable linker precisely

because one of its purposes is to relocate the values of global p p gsymbols to their final addresses.

ECE 2510 8Imagecraft, “Assembler Syntax”, see http://www.dragonsgate.net/pub/help/icc12/wwhelp/wwhimpl/js/html/wwhelp.htm

Page 9: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

Label Field

• Labels are symbols defined by the user to identify memory y y y ylocations in the program and data areas of the assembly module

• For most instructions and assembler directives, the label isFor most instructions and assembler directives, the label is optional

• Naming Rules for forming a LabelL b l fi ld b i i l 1 i l– Label field can begin in column1 or in any column

– Should be terminated by a colon (:)– Can begin with A-Z or a – z or _

ICC12 bl d i l b l i i h @– ICC12 assembler does not recognize labels starting with @• Most assemblers restrict the number of characters in a label

name

ECE 2510 9

begin: ldaa #10print: jsr hexout

Page 10: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

The Operation Fieldp

• This field specifies an assembler instruction, a directive, or p , ,a macro cell

• Assembler instructions or directives are case insensitive• Operation field must start in column one (only limited to

some assemblers)• If a label is present the instruction or directive must be• If a label is present, the instruction or directive must be

separated from the label field by at least one space• Examples:

adda #$02True = $01

ECE 2510 10

Page 11: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

The Operand Fieldp

• The operand field follows the operation field and is p pseparated from the operation field by at least one space

• The operand field is composed of one or more operands t d b f ll d b ti l t bseparated by commas, followed by optional space or tab

characters• Operand field is used to supply arguments to the assembler Ope a d e d s used to supp y a gu e ts to t e asse b e

instruction, directive or macro that has been used in the operation field

E lExampleTCNT = $0084 ;$0084 is the operand fieldTC0 = $0090 ; $0090 is the operand field

ECE 2510 11

p

Page 12: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

The Comment Field

• Comment field is optional and is mainly used for p ydocumentation purpose

• The comment field is ignored by the assembler• Usually, any line beginning with * or // or ; is a comment• ICC12 software used in the ECE2510 lab recognizes

semicolon ( ; ) as a commentsemicolon ( ; ) as a comment• Ex:

Dec lp_cnt ; Decrement the loop count

ECE 2510 12

Page 13: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

Assembler On-Line Documentation

• Imagecraft, “Assembler Syntax”, see g , y ,– http://www.dragonsgate.net/pub/help/icc12/wwhelp/wwhimpl/js/html/wwhelp.htm

Th i th C t t A “TOOL REFERENCES”• Then in the Contents Area open “TOOL REFERENCES”– You will then have access to– Assembler Syntax– Assembler Directives– Assembler Instructions

ECE 2510 13

Page 14: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

Assembler Directives

• Assembler Directives look just like the instructions in an jassembly language program

• Assembler directives tell the assembler to perform an ti th th ti th hi d foperation other than creating the machine code for an

instruction• Assembler directives are commands to the assemblersse b e d ect ves a e co a ds to t e asse b e• Assembler directives are case insensitive• Assembler directives vary with the assembler• Note: Please do not follow the assembler directives

mentioned in the course textbook. Those directives are based on a assembler different from the one used in the lab

ECE 2510 14

based on a assembler different from the one used in the lab

Page 15: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

Assembler Directives

• “Universal” Assembler Directives (UAD) are generally ( ) g yavailable in all assembly language development environments. The text describes one example set.Th I ft ICC12 A bl Di ti b f d• The Imagecraft ICC12 Assembler Directives may be found on-line at

– http://www.dragonsgate.net/pub/help/icc12/wwhelp/wwhimpl/js/html/wwhelp.htm– Select Tool References and then select Assembler Directives

– It is also on the password web site as a *.pdf file.

ECE 2510 15

Page 16: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

ICC12 Memory Address Settingy g

• .org <value>g– The assembler uses a location counter to keep track of the memory

location where the next machine code byte should be placed.– Sets the Program Counter (PC) to value This directive is onlySets the Program Counter (PC) to value. This directive is only

valid for areas with the abs attribute. Note that value is a byte address.

• Example: – .area interrupt_vectors(abs)– .org 0xFFD0– .dc.w

ECE 2510 Imagecraft, “Assembler Directives”, see http://www.dragonsgate.net/pub/help/icc12/wwhelp/wwhimpl/js/html/wwhelp.htm

Page 17: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

ICC12 Memory Areasy

.area <name> [(attributes)]Defines a memory region to load the following code or data The linker– Defines a memory region to load the following code or data. The linker gathers all areas with the same name together and either concatenates or overlays them depending on the area's attributes.

– The attributes are:• abs, or <- absolute area• rel <- relocatable area

– followed by• con or <- concatenated• con, or <- concatenated• ovr <- overlay

– Starting address of an absolute area is specified within the assembly file– Starting address of a relocatable area is specified as a command option to

the linker– For an area with the con attribute, the linker concatenates areas of that

name one after another. For an area with the ovr attribute, for each file, the linker starts an area at the same address.

ECE 2510 Imagecraft, “Assembler Directives”, see http://www.dragonsgate.net/pub/help/icc12/wwhelp/wwhimpl/js/html/wwhelp.htm

Page 18: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

ICC12 Program Text and Maing

.text– Sets to starting address of the program area. – Program area for the MC9S12DP256 should always be set to

0x12000x1200– .text should be used before start of the main program

_main:: – should follow .text indicating the start of the main program

ECE 2510 18

Page 19: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

ICC12 Data Array Assignmentsy g

.byte <expr> [,<expr>]*

.word <expr> [,<expr>]*

.long <expr> [,<expr>]*– These directives define constants. The three directives denote byteThese directives define constants. The three directives denote byte

constant, word constant (2 bytes), and long word constant (4 bytes), respectively. Word and long word constants are output in little endian format, the format used by the AVR microcontrollers. Note that .long can

l h l d Th h ionly have constant values as operands. The other two may contain relocatable expressions.

Example: T 1 d l b l fTemp1: .word label,foo Temp2: .byte $12, $AA, $34, $56, $74Temp3: .word $1234, $ABCD, $1111, $1212

ECE 2510 19Imagecraft, “Assembler Directives”, see http://www.dragonsgate.net/pub/help/icc12/wwhelp/wwhimpl/js/html/wwhelp.htm

Page 20: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

ICC12 Data Reserved Spacep

.blkb <value>

.blkw <value>

.blkl <value>– These directives reserve space without giving them values. The

number of items reserved is given by the operand.

Examples:Examples:Table1: .blkb $64 ;100 byte storage areaTable2: .blkw 100 ;100 word storage areaT bl 3 blkl $0A 10 l d (4 b t ) tTable3: .blkl $0A ;10 long word (4 byte) storage area

ECE 2510 20Imagecraft, “Assembler Directives”, see http://www.dragonsgate.net/pub/help/icc12/wwhelp/wwhimpl/js/html/wwhelp.htm

Page 21: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

ICC12 Text String Assignmentg g

.ascii "strings"

.asciz "strings"– These directives are used to define strings, which must be enclosed in a

delimiter pair. The delimiter can be any character as long as the beginning delimiter matches the closing delimiter. Within the delimiters, any

i bl ASCII h lid l h f ll i C lprintable ASCII characters are valid, plus the following C-style escape characters, all of which start with a backslash (\):

\e escape\b backspace\f f f d\f form feed\n line feed\r carriage return\t tab\< t 3 t l di it > h t ith l l t th t l di it\<up to 3 octal digits> character with value equal to the octal digits

• .asciz adds a NUL character (\0) at the end. It is acceptable to embed \0 within the string.

Examples:

ECE 2510 21

p.asciz "Hello World\n".asciz "123\0456" Imagecraft, “Assembler Directives”, see

http://www.dragonsgate.net/pub/help/icc12/wwhelp/wwhimpl/js/html/wwhelp.htm

Page 22: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

ASCII (American Standard Code for I f I h ) blInformation Interchange ) table

ECE 2510 22

Page 23: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

ICC12 Symbol Valuey

<symbol> = <value>y– Defines a numeric constant value for a symbol.

Example: foo = 5

ECE 2510 23Imagecraft, “Assembler Directives”, see http://www.dragonsgate.net/pub/help/icc12/wwhelp/wwhimpl/js/html/wwhelp.htm

Page 24: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

ICC12 Global Symboly

.globl <symbol> [, <symbol>]g y [, y ]– Makes the symbols defined in the current module visible to other

modules– Otherwise symbols are local to the current moduleOtherwise, symbols are local to the current module

ECE 2510 24

Page 25: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

ICC12 Macro Definition

.macro <macroname>Defines a macro The body of the macro consists of all the statements up– Defines a macro. The body of the macro consists of all the statements up to the .endmacro statement. Any assembly statement is allowed in a macro body except for another macro statement. Within a macro body, the expression @digit, where digit is between 0 and 9, is replaced by the corresponding macro arg ment hen the macro is in oked Yo cannotcorresponding macro argument when the macro is invoked. You cannot define a macro name that conflicts with an instruction mnemonic or an assembly directive. See .endmacro and Macro Invocation.

For example, the following defines a macro named foo:p g.macro foo

lds @0,amov @1,@0

.endmacro– Invoking foo with two arguments:

foo R10,R11is equivalent to writing:

ECE 2510 25

– is equivalent to writing:lds R10,amov R11,R10

Imagecraft, “Assembler Directives”, see http://www.dragonsgate.net/pub/help/icc12/wwhelp/wwhimpl/js/html/wwhelp.htm

Page 26: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

ICC12 Macro Invokingg

<macro> [<arg0> [,<args>]*]k b i i h bl d– Invokes a macro by writing the macro name as an assembly command

followed by a number of arguments. The assembler replaces the statement with the body of the macro, expanding expressions of the form @digit with the corresponding macro argument. You may specify more w e co espo d g c o gu e . ou y spec y o earguments than are needed by the macro body, but it is an error if you specify fewer arguments than needed.

Example: foo bar,x

Invokes the macro named foo with two arguments, bar and x

ECE 2510 26Imagecraft, “Assembler Directives”, see

http://www.dragonsgate.net/pub/help/icc12/wwhelp/wwhimpl/js/html/wwhelp.htm

Page 27: ECE 2510 Introduction toECE 2510 Introduction to ...homepages.wmich.edu/~bazuinb/ECE4510/Ch02_ICC12_asm.pdf · ECE 2510 Introduction toECE 2510 Introduction to Microprocessors Chapter

Example Assembly Program: Task1p y g.area program(abs) ; command to indicate the absolute start of the program.org $1000 ; command forces the controller to start the program from memory location 1000Array1: .byte $01,$02,$03,$04,$05,$06,$07,$08,$09,$0A

; This command declares an array of name Array1 and initializes the array with ; the values provided starting from memory location 1000

.org $1020Array2: .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00

;This command declares an array of name Array2 and initializes the ; array to zeros starting from memory location 1020

.text_main:: ;Label indicating the start of main program (address $122a)

Ldx #Array1 ;loads index register X with start address of Array1Ldy #Array2 ;loads index register Y with start address of Array2Ldab #$0A ;Loads B accumulator with value 10 for counter

L l b lLoop: ;labelLdaa 1,x+ ;Loads Array1 into accumulator A and increments value in X tStaa 1,y+ ;Stores accumulator A to Array2 and increments value of YDbne b,Loop ;Decrement Value of B accumulator and if its not equal to zero then loop backswi

ECE 2510 27