topic03a introduction to the assembly language on the freescale mc9s12x

39
Assembly Language: Introduction Topic Video 03A 3A 1

Upload: brett-wildermoth

Post on 14-Apr-2015

35 views

Category:

Documents


1 download

DESCRIPTION

This topic covers the use of the assembly language within the context of the Freescale MC9S12X Microcontroller. It explain the layout of an assembly file trough to the use assembler directives.

TRANSCRIPT

Page 1: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Assembly Language: Introduction

Topic Video 03A3A

1

Page 2: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Introduction to Programming

• A program is a group of tasks that instruct the microprocessor to perform a specific function.

• Each instruction enables the processor to function in a particular manner.

• The program enables the microprocessor to perform many different tasks.

• A microprocessor could be used in a child’s toy the same microprocessor could be used to control complex manufacturing equipment, the only difference between the two applications is the program.

• Almost all microprocessor commands consist of two components, an opcode and an operand.

• The opcode tells the processor the function it should perform and the operand contains the data for that instruction.

2

Page 3: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Machine Code Level Programming

• Machine code level programming refers to the lowest possible level language, this language consists of raw binary data that is directly interpreted by the instruction decoder of the microprocessor.

• It is referred to as Machine Code as this is all that the microprocessor (machine) understands.

• When viewing machine code it is generally represented using 8 bits in the form of hexadecimal numbers.

• The opcodes for each microprocessor are different as each instruction decoder is different. If each instruction decoder was the same, then what would be the benefit of using one processor type over another.

• All higher level programs are compiled or assembled into machine code prior to being executed by the microprocessor.

• The next language level above machine code is the assembly language, it can be directly translated into machine code.

3

Page 4: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Assembly Language Programming

• Assembly language programs are made of a series of instructions and parameters commonly referred to as opcodes and operands respectively.

• Opcodes usually consists of mnemonics or assembler directives.• Mnemonics are easy to remember and can be manually translated

into machine level code. • A program that performs this translation is referred to as an

assembler.• There is a variety of different assemblers for every processor on the

market, some better than others.• The purpose of the assembler is to take your assembly code (source

code) and produce machine code (object code) that the processor can directly execute (run).

4

Page 5: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

The Code Warrior (Freescale) Assembler

• The Freescale assembler program is a key part of the Code Warrior IDE.

• Code Warrior has the following key advantages:– Advanced project manager.– Optimizing C and C++ compiler as

well as an assembler.– Support for many Freescale

processors, HCXX, HS12 and XGate processors.

– Built in macro compiler– Library Maker– Syntax highlighting editor.– Full chip simulator and debugger.– Data visualization tools.

5

Page 6: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler Program

;load $41 into A#$41LDAA Main:

• An assembler program consists of four columns, each separated by a tab or a space.

• The columns from left to right are:• Labels• Opcode• Operand• Comment

For example:

6

Page 7: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler Program Label Column

• A label consists of no more than 16 characters.• A label must be placed in the first column.• A label must begin with a letter or an underscore. • A label may contain any of the following characters: A – Z, a-z,

0-9, $, _ after the initial letter or underscore. • Labels longer than 16 characters are truncated to 16

characters.

Label ::= {letter|underscore}1 {letter | digit | underscore}0+

(Same rule as an identifier in “C”.)‏

7

Page 8: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler ProgramOpCode Column

• The opcode column must contain a mnemonic, an assembler directive, or a macro name.

• Mnemonics are the name given to the processors instructions.

• In the Freescale assembler there exist many useful assembler directives than can be used to define constants, reserve memory, and specify specific memory locations for code and data.

8

Page 9: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

• [label] DC[.size] value [,value]label – specifies an optional labelsize – specifies an optional size qualifier .B - define byte data (default)‏ .W - define word data (16 bits)‏ .L - define double word data (32 bits)‏This data definition directive is used to specify one or more

expressions or character strings separated by commas. Eg. Byte DC $2D String DC.B 'Hello World' Values DC.W $1234, $7A54

A Detailed Look at an Assembler ProgramOpCode Column

9

Page 10: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler ProgramOpCode Column

• [label] DCB[.size] length [,value]

label – specifies an optional labelsize – specifies an optional size qualifier .B - allocate byte elements (default)‏ .W - allocate word elements (16 bits)‏ .L - allocate double word elements (32 bits)‏length – Specifies the number of elements to be allocated.value – specifies an optional fill value.The DCB directive is used to allocate chunks of memory whose size

is defined by both the length and size parameters.Eg. Data DCB 10

10

Page 11: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler ProgramOpCode Column

• [label] DS[.size] lengthlabel – specifies an optional labelsize – specifies an optional size qualifier .B - allocate byte elements (default)‏ .W - allocate word elements (16 bits)‏ .L - allocate double word elements (32 bits)‏length – Specifies the number of elements to be reserved.This directive is used to reserve storage space in memory.Eg. DS.B 10

11

Page 12: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler ProgramOpCode Column

• label EQU expressionlabel – specifies the label that the expression is assigned to.expression – specifies an expression assigned to the label.This pseudo-operation is used to defined symbols, much like the

#define statement in C.Eg. PORTA EQU $0000

12

Page 13: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler ProgramOpCode Column

• label SET expression

label – specifies the label that the expression is assigned to.Expression – specifies an expression assigned to the labelThis directive works in the same way as EQU except that SET can

be redefined.

Eg.

Value SET 10 ...Value SET 12

13

Page 14: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler ProgramOpCode Column

• ORG expression

expression – specifies the absolute start address of the section.The ORG directive is used to begin a new absolute section and set the

location counter to expression.

Eg.

ORG ROMSTART

14

Page 15: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler ProgramOperand Column

• The operand that follows the opcode, must be described in a way suitable for that opcode.

• The operand may consist of a symbol (label), a constant or an expression, that is interpreted by the assembler.

• The way you express the operand defines the addressing mode used.

Operand Format Addressing Mode / Instruction TypeNo operand InherentExpression Direct, extended, or relative

#Expression ImmediateExpression, R Indexed offset where R can be X, Y, SP, or PC.Expression, -R Indexed automatic predecrement.Expression, +R Indexed automatic preincrement.Expression, R- Indexed automatic postdecrement.Expression, R+ Indexed automatic postincrement.Accumulator, R Indexed Accumulator[Expression, R] Indexed Indirect

[D,R] Indexed indirect D AccumulatorExpression, Expression Bit set or clear

Expression, R, Expression Bit set or clearExpression, Expression, Expression Bit test and branch

Expression, R, Expression, Expression Bit test and branch

15

Page 16: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler ProgramOperand Column

• A constant can exist in many forms• The assembler is informed of the constant type

by the use of a prefix or defining structure.  Number - Decimal $Number - Hexadecimal @Number - Octal %Number - Binary 'Char' - Character

16

Page 17: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler ProgramOperand Column

• Decimal Constants • Values depend on the corresponding

instruction.• No prefix means decimal.

  Example:

LDAA #28 ; (A) 28

17

Page 18: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler ProgramOperand Column

• Hexadecimal constants• Preceded by the ‘$’ prefix, e.g.

  Example:

LDAA #$1C ; (A) 28 ($1C) ‏ 

18

Page 19: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler ProgramOperand Column

• Octal constant• Preceded by the ‘@’ prefix• Octal is a base 8 numbering system only

digits from 0 to 7 are valid.  Example:

LDAA #@34 ; (A) 28 (@34)‏ 

19

Page 20: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler ProgramOperand Column

• Binary constants• Preceded by the ‘%’ prefix • Consist purely of ‘1’s and ‘0’s. • Generally an eight-bit or 16 bit word should be

defined. • Binary constants are commonly used for defining

masks  Example:

BSET DDRA, %00001111 ; Bits 0:3 of DDRA are set to ‘1’ 

20

Page 21: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler ProgramOperand Column

• ASCII constants• Single character enclosed in single quotes.

Example:

CMPA #'B' ; the contents of Accumulator A are

compared to the character ‘B’ 

21

Page 22: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler ProgramOperand Column

• Expressions• Expressions are a combination of symbols separated by operators. • Operators recognized by the assembler are +, -, * (multiply), / (divide), <

(shift left), > (shift right), & (bitwise AND), | (bitwise OR), and ^ (bitwise XOR).

• Expressions are evaluated using the normal algebraic operation precedence, if needed brackets can be used.

• Expressions are evaluated at assemble time not runtime.

Example:

Value EQU ((1+2)*4)>2; Value is then evaluated to 3 at assembly time.

22

Page 23: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

A Detailed Look at an Assembler Program Comment Column

• A semi-colon should precede the comments included in column four.

• A line of assembler code could look like this (note that “Example:” is in fact the label): Example: <tab> LDAA <tab> #$41 <tab> ; (A) $41

23

Page 24: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing Modes• The 9S12 contains 6 major addressing modes:

• Inherent• Immediate• Direct• Extended• Relative• Indexed

• The addressing mode describes what the processor must do in order to get the operand for the current instruction.

• Each instruction only supports a limited number of these addressing modes.

24

Page 25: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing ModesINHERENT

•No Operands

For example

INX

PSHY

25

Page 26: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing ModesIMMEDIATE

• In “immediate addressing” the operand or data to be used is part of the instructions.For example:

LDAA #$41

...A28641719A...

...$40F1$40F2$40F3$40F4$40F5

...

Accumulator ALDAA#$41$ 41

26

Page 27: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing ModesIMMEDIATE

• In “immediate addressing” the operand or data to be used is part of the instructions.For example:

LDAA #$41

...A28641719A...

...$40F1$40F2$40F3$40F4$40F5

...

Accumulator ALDAA#$41$ 41

26

Page 28: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing ModesIMMEDIATE

• Immediate addressing is always defined in assembler by the “#” symbol preceding the operand.

• Eight bit immediate values are in the range of 0 to 255 or –128 to 127 (unsigned or signed).

• Sixteen bit immediate values are in the range of 0 to 65,535 or -32,768 to 32,767 (unsigned or signed).

27

Page 29: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing ModesDIRECT

• Immediate addressing is useful if the value of the operand is known at the point of coding.

• If the value is not known then a variable (stored in RAM) must be used.

• A variable is accessed (read from or written to) using direct or extended addressing modes depending on where the variable is stored in memory.

• “Direct addressing” requires the use of only a single byte for its operand. And therefore can only be used to address memory locations in the range of $0000 and $00FF.

28

Page 30: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing ModesDIRECT

Example:LDAA $55

...A2D134719A...

...$0051$0052$0053$0054$0055

...

Memory

Accumulator A$0055

$55

9A$

29

Page 31: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing ModesDIRECT

Example:LDAA $55

...A2D134719A...

...$0051$0052$0053$0054$0055

...

Memory

Accumulator A$0055

$55

9A$

29

Page 32: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing ModesEXTENDED

• “Extended addressing” uses a total of two bytes for the operand and therefore any memory location in the range $0000 to $FFFF can be specified.

• Direct uses only a single byte and can only address up to $00FF, however it requires one less instruction cycle to run and therefore is a little bit faster than if extended addressing was used.

LDAA $1234

...2376B2F103...

...$1232$1233$1234$1235$1235

...

Accumulator A

Memory$1234

$1234 B2$

30

Page 33: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing ModesEXTENDED

• “Extended addressing” uses a total of two bytes for the operand and therefore any memory location in the range $0000 to $FFFF can be specified.

• Direct uses only a single byte and can only address up to $00FF, however it requires one less instruction cycle to run and therefore is a little bit faster than if extended addressing was used.

LDAA $1234

...2376B2F103...

...$1232$1233$1234$1235$1235

...

Accumulator A

Memory$1234

$1234B2$

30

Page 34: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing ModesRELATIVE

• The most common use of “relative addressing” is when changing the program flow.

• All the branch instructions use relative addressing.• In relative addressing the operand consists of an 8 bit

relative offset from the current value contained in the program counter (PC).

• The relative offset are a signed 9 bit number (–127 to 127).

31

Page 35: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing ModesRELATIVE

For Example:BRA $FE

The relative operand is “-2”, so there the new value of the program counter would be PC = PC + -2

Memory

...A220FE719A...

...$40F1$40F2$40F3$40F4$40F5

...

$40F4

Program Counter BRAFE

...A220FE719A...

...$40F1$40F2$40F3$40F4$40F5

...

$40F2

Program Counter

BRAFE

Memory

32

Page 36: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing ModesINDEXED

• “Indexed addressing” requires us to use an indexed register in order to calculate the address of the instructions operand.

• It is easy to see when index addressing is being used it generally follows the form of

Mnemonic Offset, Register• Where the offset is a relative value that needs to be added to value

contained in the register in order to find the location in memory of the data for the Mnemonic.

33

Page 37: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing ModesINDEXED

For example:LDAA ,

...2376B2F103...

...$1232$1233$1234

$1236...

Accumulator A+ =

Memory

X

$1232 3

3

$1235

$1235 F1

$1232

$

Index Register X

34

Page 38: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Addressing ModesINDEXED

For example:LDAA ,

...2376B2F103...

...$1232$1233$1234

$1236...

Accumulator A+ =

Memory

X

$1232 3

3

$1235

$1235F1

$1232

$

Index Register X

34

Page 39: Topic03A Introduction to the Assembly Language on the Freescale MC9S12X

Need Further Assistance?

• Ask your Demonstrator,

• Post a question on the Forum,

• Email the Convener, or

• Make an appointment.

35