Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
For example:
Main: LDAA #$41 ;load $41 into A
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.)
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.
10
11
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
13
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
15
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
Decimal Constants
Example:
LDAA 28 ; (A) 28 #
17
Hexadecimal constants
Preceded by the $ prex, e.g.
Example:
18
Octal constant
Example:
Preceded by the @ prex Octal is a base 8 numbering system only digits from 0 to 7 are valid.
19
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
ASCII constants
Single character enclosed in single quotes.
Example:
CMPA
#'B'
; the contents of Accumulator A are compared to the character B
21
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 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
... 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
... 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 ...
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
Memory ... ... 23 $1232 76 $1233 B2 $1234 F1 $1235 03 $1236 ... ...
34
Addressing Modes
INDEXED
Memory ... ... 23 $1232 76 $1233 B2 $1234 F1 $1235 03 $1236 ... ...
34