Sei sulla pagina 1di 39

Topic Video 03A

Assembly Language: Introduction

3A

Introduction to Programming
A program is a group of tasks that instruct the microprocessor to perform a specic 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 childs 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.

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

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

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.

A Detailed Look at an Assembler Program


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:
Main: LDAA #$41 ;load $41 into A

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 rst 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 identier in C.)

A Detailed Look at an Assembler Program


OpCode 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 dene constants, reserve memory, and specify specic memory locations for code and data.

A Detailed Look at an Assembler Program


OpCode Column

[label] DC[.size] value [,value]


label specifies an optional label size 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 String DC.B Values DC.W $2D 'Hello World' $1234, $7A54

A Detailed Look at an Assembler Program


OpCode Column

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


label specifies an optional label size 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

A Detailed Look at an Assembler Program


OpCode Column

[label] DS[.size] length


label specifies an optional label size 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

A Detailed Look at an Assembler Program


OpCode Column

label EQU expression


label 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

A Detailed Look at an Assembler Program


OpCode Column

label SET expression


label specifies the label that the expression is assigned to. Expression specifies an expression assigned to the label This directive works in the same way as EQU except that SET can be redefined. Eg. Value ... Value SET 10 SET 12

13

A Detailed Look at an Assembler Program


OpCode 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

A Detailed Look at an Assembler Program


Operand 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 denes the addressing mode used.
Operand Format No operand Expression #Expression Expression, R Expression, -R Expression, +R Expression, RExpression, R+ Accumulator, R [Expression, R] [D,R] Expression, Expression Expression, R, Expression Expression, Expression, Expression Expression, R, Expression, Expression Addressing Mode / Instruction Type Inherent Direct, extended, or relative Immediate Indexed offset where R can be X, Y, SP, or PC. Indexed automatic predecrement. Indexed automatic preincrement. Indexed automatic postdecrement. Indexed automatic postincrement. Indexed Accumulator Indexed Indirect Indexed indirect D Accumulator Bit set or clear Bit set or clear Bit test and branch Bit test and branch

15

A Detailed Look at an Assembler Program


Operand Column

A constant can exist in many forms The assembler is informed of the constant type by the use of a prex or dening structure. Number - Decimal $Number - Hexadecimal @Number - Octal %Number - Binary 'Char' - Character

16

A Detailed Look at an Assembler Program


Operand Column

Decimal Constants
Example:

Values depend on the corresponding instruction. No prex means decimal.

LDAA 28 ; (A) 28 #

17

A Detailed Look at an Assembler Program


Operand Column

Hexadecimal constants
Preceded by the $ prex, e.g.

Example:

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

18

A Detailed Look at an Assembler Program


Operand Column

Octal constant
Example:

Preceded by the @ prex Octal is a base 8 numbering system only digits from 0 to 7 are valid.

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

19

A Detailed Look at an Assembler Program


Operand Column

Binary constants

Example:

Preceded by the % prex Consist purely of 1s and 0s. Generally an eight-bit or 16 bit word should be dened. Binary constants are commonly used for dening masks

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


20

A Detailed Look at an Assembler Program


Operand Column

ASCII constants
Single character enclosed in single quotes. Example: CMPA #'B' ; the contents of Accumulator A are compared to the character B
21

A Detailed Look at an Assembler Program


Operand Column

Expressions
Example: Value EQU ((1+2)*4)>2 ;Value is then evaluated to 3 at assembly time. 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.

22

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

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

Addressing Modes
INHERENT

No Operands
For example INX PSHY

25

Addressing Modes
IMMEDIATE In immediate addressing the operand or data to be used is part of the instructions. For example:
LDAA #$41

Accumulator A

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

... A2 86 41 71 9A ...

LDAA #$41

26

Addressing Modes
IMMEDIATE In immediate addressing the operand or data to be used is part of the instructions. For example:
LDAA #$41

Accumulator A

$ 41

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

... A2 86 41 71 9A ...

LDAA #$41

26

Addressing Modes
IMMEDIATE

Immediate addressing is always dened 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

Addressing Modes
DIRECT 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

Addressing Modes
DIRECT

Example: $55
LDAA

$55

Accumulator A $

Memory ... ... $0051 A2 $0052 D1 $0053 34 $0054 71 $0055 9A ... ...

29

Addressing Modes
DIRECT

Example: $55
LDAA

$55

Accumulator A $ 9A

Memory ... ... $0051 A2 $0052 D1 $0053 34 $0054 71 $0055 9A ... ...

29

EXTENDED Extended addressing uses a total of two bytes for the operand and therefore any memory location in the range $0000 to $FFFF can be specied. 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

Addressing Modes

Accumulator A $

Memory ... ... $1232 23 $1233 76 B2 $1234 $1235 F1 $1235 03 ... ...
30

EXTENDED Extended addressing uses a total of two bytes for the operand and therefore any memory location in the range $0000 to $FFFF can be specied. 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

Addressing Modes

Accumulator A $ B2

Memory ... ... $1232 23 $1233 76 B2 $1234 $1235 F1 $1235 03 ... ...
30

Addressing Modes
RELATIVE

The most common use of relative addressing is when changing the program ow. 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

Addressing Modes
RELATIVE For Example:
BRA $FE The relative operand is -2, so there the new value of the program counter would be PC = PC + -2 Memory ... $40F1 $40F2 $40F3 $40F4 $40F5 ... ... A2 20 FE 71 9A ... Program Counter BRA FE $40F2 ... $40F1 $40F2 $40F3 $40F4 $40F5 ... Memory ... A2 20 FE 71 9A ...

Program Counter $40F4

BRA FE

32

Addressing Modes
INDEXED

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 nd the location in memory of the data for the Mnemonic.

33

Addressing Modes
INDEXED

For example: LDAA 3, X

Index Register X $1232 $1232 + 3 = $1235 Accumulator A $

Memory ... ... 23 $1232 76 $1233 B2 $1234 F1 $1235 03 $1236 ... ...
34

Addressing Modes
INDEXED

For example: LDAA 3, X

Index Register X $1232 $1232 + 3 = $1235 Accumulator A $ F1

Memory ... ... 23 $1232 76 $1233 B2 $1234 F1 $1235 03 $1236 ... ...
34

Need Further Assistance?


Ask your Demonstrator, Post a question on the Forum, Email the Convener, or Make an appointment.
35

Potrebbero piacerti anche