lecture21

advertisement
Machine Language, CrossAssembler, Loops
Today:
• First Hour: Machine Language, Cross-assembler
– Section 2.1-2.4 of Huang’s Textbook
– In-class Activity #1
• Second Hour: Branches, Loops
• Section 2.8 of Huang’s Textbook
– In-class Activity #2
1
Machine Language Programming
Example program
Add two 8-bit numbers N1 & N2
to form an 8-bit sum SUM
That is,
N1 + N2  SUM
2
N1 + N2  SUM
Program fragment
LDAA
ADDA
STAA
HALT
N1
N2
SUM
...
$C120
$C121
$C122
$C123
$C124
$C125
$C126
$C127
$C128
$C129
...
$C210
$C211
$C212
...
...
B6
C2
10
BB
C2
11
B7
C2
12
3F
...
12
34
FF
...
C1
20
PC
A
B
Let's go through it
instruction by instruction
3
Load Accumulator A
N1  A
LDAA
ADDA
STAA
HALT
N1
N2
SUM
...
$C120
$C121
$C122
$C123
$C124
$C125
$C126
$C127
$C128
$C129
...
$C210
$C211
$C212
...
...
B6
C2
10
BB
C2
11
B7
C2
12
3F
...
12
34
FF
...
C1
23
12
PC
A
B
The result of the first
instruction
4
Add to Accumulator A
A + N2  A
LDAA
ADDA
STAA
HALT
N1
N2
SUM
...
$C120
$C121
$C122
$C123
$C124
$C125
$C126
$C127
$C128
$C129
...
$C210
$C211
$C212
...
...
B6
C2
10
BB
C2
11
B7
C2
12
3F
...
12
34
FF
...
C1
26
46
PC
A
B
The result of the second
instruction
5
Store Accumulator A
A  SUM
LDAA
ADDA
STAA
HALT
N1
N2
SUM
...
$C120
$C121
$C122
$C123
$C124
$C125
$C126
$C127
$C128
$C129
...
$C210
$C211
$C212
...
...
B6
C2
10
BB
C2
11
B7
C2
12
3F
...
12
34
46
...
C1
29
46
PC
A
B
The result of the third
instruction
6
Halt the Program
Return to the Buffalo Monitor
LDAA
ADDA
STAA
HALT
N1
N2
SUM
...
$C120
$C121
$C122
$C123
$C124
$C125
$C126
$C127
$C128
$C129
...
$C210
$C211
$C212
...
...
B6
C2
10
BB
C2
11
B7
C2
12
3F
...
12
34
46
...
C1
2A
46
PC
A
B
The result of the last
instruction
7
M6811 Cross-Assembler
Program assembles on one machine (host) and runs on another (target)
DOS instruction
Source Code
<prog.asm>
"asm prog"
Assembler Outputs
ASSEMBLER
Listing File
<prog.lst>
Load File
<prog.s19>
Script File
<prog>
"text"
"text"
"batch"
8
Assembly Language
Source Statement Content
Numbers
Decimal (default)
10, -15
Hexadecimal
$2C
Binary
Symbols
%00100000
Good for humans
Good for addresses
Good for bit patterns
1..6 Alphanumeric characters
1 character symbols not allowed: A, B, D, X, or Y
Avoid symbols that are identical to opcode mnemonics
Expressions
Symbols and numbers separated by arithmetic operators
Arithmetic operators are +, -, *, and /
No embedded spaces are allowed
The assembler
does the math
-- in any base!!
9
Source Statement Format
Label
Operation
Operand
Comment
Statement fields are separated by blanks†
Label
a symbol whose first
character is in column 1
Operation instruction mnemonic for
Opcode
Operand
can be empty, a symbol, a
number, or an expression
† Actually,tabs
are
allowed too.
What about a blank
in column 1?
Comment the remainder of the line,
may be blank
Example:
START
LDAA $C200
This is the beginning of the program
10
HC11 Assembly Program
1. Assembler Directives
define data and symbol
reserve and initialize memory locations
set assembler and linking condition
specify output format
2. Assembly Language Instructions
3. END directive
last statement of a program
any statement after END will be ignored
4. Comments
explain the function of a single or a group of instructions
11
Example Assembly
Program
Consider the C program:
main ()
{
int i, j, k;
i = 75;
j = 10;
k = i + j - 6;
}
; i, j, k are integer variables
; assign 75 to i
; assign 10 to j
12
Assembly Program
(1) * Data storage declaration section (includes assembly directives)
(2)
ORG $00
(3) i
RMB 1
; variable i
(4) j
RMB 1
; variable j
(5) k
RMB 1
; variable k
(6) * program instruction section
(7) start ORG $C000
; starting address of program
(8)
LDAA #75
(9)
STAA i
; initialize i to 75
(10)
LDAA #10
(11)
STAA j
; initialize j to 10
(12)
ADDA i
; compute i + j
(13)
SUBA #6
; compute i + j -6
(14)
STAA k
; store i + j - 6 to k
(15)
END
13
Special Assembler Directives
Behave like operators but are only used by the assembler, not processor
Asterisk - two usages
1.
* Putting a * in column 1 makes the entire line a comment
2.
DATA RMB
$4A
END
*
EQU
* is a substitute for the current value
of the location counter
Pound sign
Usage:
LDAA #$0A denotes IMM addressing mode
Quotes (' and ")
Usage of ':
Usage of ":
'a
denotes one ASCII character a (= $61)
"A b." denotes ASCII string ($41,$20,$62,$2E)
14
Basic Assembler Directives
1. END
- ends a program to be processed by an assembler
- any statement following the END directive is ignored
- not supported by the Motorola freeware as11
2. ORG
- sets a new value for the location counter of the assembler
- tells the assembler where to put the next byte it generates after the
ORG directive
The sequence
ORG $C000
LDAB #$FF
will put the opcode byte for the instruction LDAB #$FF at location
$C000.
15
Basic Assembler Directives
3.
RMB -- reserve memory bytes
- reserve memory bytes without initialization
[<label>] RMB <expression> [<comment>]
buffer
RMB 100
allocates 100 bytes for data and can be referred to by the label
“buffer”.
4. BSZ -- block storage of zeros
- assembler allocates a block of bytes that are initialized to zeros
[<label>]
BSZ <expression> [<comment>]
buffer
BSZ 80
reserves a block of 80 bytes and their value are initialized to 0.
16
Basic Assembler Directives
5. FCB -- form constant byte
- reserves as many bytes as the number of arguments in the
directive
- each argument specifies the initial value of the corresponding
byte
[<label>]
FCB
[<expression>][,<expression>,...,<expression>][<comment>]
The statement
ABC
FCB $11,$22,$33
reserves three consecutive memory bytes and initializes their
values to $11, $22, and $33.FDB -- form double byte
- reserves 2 bytes for each argument of the directive
- each argument specifies the initial value of the
17
Basic Assembler Directives
6. FDB -- corresponding double bytes syntax is
[<label>]
FDB
<expression>][,<expression>,...,<expression>]
[comment>]
Eg:
ABC
FDB $11,$22,$33
initialize 6 bytes to $00 $11 $00 $22 $00 $33
7. FCC -- form constant character
generates ASCII code bytes for the letters in the
arguments
[label] FCC “<string>“ [<comment>]
ALPHA
FCC “DEF”
will generate the values $44 $45 $46 in memory
18
Basic Assembler Directives
8. DCB -- define constant block
- reserve an area of memory and initialize each byte to the same
constant value
- syntax is
[label]
DCB <length>,<value>
- not supported by the Motorola freeware as11
space
DCB 80,$20
will generate a line of 80 space characters.
9. FILL -- fill a block of constant values
- serve as the same purpose as does the DCB directive
[<label>] FILL <value>,<length>
ones
FILL 1,40
will force the freeware assembler to fill each of the 40 memory
locations with a 1.
19
Basic Assembler Directives
10. EQU -- equate
- allows the user to use a symbolic name in place of a number
- syntax is
<label>
EQU <expression> [<comment>]
The directive
ROM
EQU $E000
tells the assembler that wherever ROM appears in the program, the
value $E000 is to be substituted.
20
Directives Summary
Behave like operators but are only used by the assembler, not processor
Directives
ORG v
RMB n
Example Usage
Origin. Sets value of
location counter for the
following instruction/data
USTACK RMB 20
Reserve memory byte(s)
DATA
ORG $D000
ZMB 15
ZMB n (=BSZ n) Zero memory byte(s)
MYDATA BSZ $17
DCB v,n
Define Constant Block
BLOCK
FCB v, (FDB v)
Form Constant Byte (Form ONE
Double Byte) stores
TWO
constants
THREE
Form Constant Character
QUERY
associates a symbol with
the value of an expression MESS
START
denotes the end of the
source program
FCC c
s EQU v
END
n = number of bytes, v = value of byte(s), s = symbol
DCB $FF,12
FCB 10
FDB $C21A
FCB 1,$F,3
FCC '?
FCC "end"
EQU $6200
END
21
Do Activity #1 Now
22
Branch Instructions
This is
an example of a
conditional branch
instruction
Examples:
BEQ
Branch on Equal to Zero (i.e., Z bit = 1)
if(Z == 1) go to <a_specified_address>;
BRA
Branch Always
go to <a_specified_address>;
JMP
Jump Always
go to <a_specified_address>;
These are examples of
unconditional branch
instructions
Now, how are these
two different?
Look in the PRG!
23
What’s Different?
Source
Form
Addressing
Boolean
Mode for Machine Code
Expression Operand Opcode Operand Bytes
Operation
BRA
Branch Always
?1=1
REL
20
rr
2
JMP
Jump
See Special
EXT
7E
hh ll
3
Operations
IND,X
6E
ff
2
IND,Y
18 6E
ff
3
Relative to what?
This is a new
addressing mode
Relative addressing
24
Relative Addressing
In this mode, addresses are specified relative to the program counter
Also known as PC-relative addressing
Example:
$6142 BRA $28
Relative
Offset
Program branches to address (PC + $28)
To figure out this address, we need to know the value in the PC.
Where is the instruction located in memory?
BRA is a 2-byte instruction.
After the instruction is fetched,
Therefore,
PC = $6142 + $02 = $6144.
PC + $28 = $6142 + $28 = $616A.
25
Backward Jumps
The relative offset is treated as a signed number
in two’s complement notation
Its an infinite
loop!!
Example:
$6142 BRA $FE
What does this say
about this
instruction?
PC = $6144
Relative offset = $FE
Extend the offset to 16 bits  $FFFE
PC + relative offset = $6144 + $FFFE = ?
$6142
+
0110 0001 0100 0100
1111 1111 1111 1110
????
0110 ????
0001 ????
0100 ????
0010
26
Inherent Addressing Mode
Instructions with No Operands
Some of the instructions that have no operand:
ABA
A+BA
CLRB 0  B
INCA
A+1A
DECB B - 1  B
SEC
1  C (C = CCR<0>)
27
Condition Code Register
- - H - N Z V C
Condition Code Register
Carry/borrow (MSB)
oVerflow (2s C)
Zero
Negative
Half carry from (bit 3 to 4), used
for (BCD) corrections only
These CCR bits are set by
ALU operations
28
Simple Branches
Test only 1 or 0
Source
Form
Operation
Boolean
Expression
Opcode
BRA (opr)
Branch Always
?1 = 1
20
BRN (opr)
Branch Never
?1 = 0
21
29
Simple Conditional Branches
Test only one of N, Z, V, C
Boolean
Expression
Opcode
BMI (opr) Branch if Minus
?N = 1
20
BPL (opr) Branch if Plus
?N = 0
2A
BEQ (opr) Branch if Equal (zero)
?Z = 1
27
BNE (opr) Branch if Not Equal
?Z = 0
26
BVS (opr) Branch if oVerflow Set
?V = 1
29
BVC (opr) Branch if oVerflow Clear
?V = 0
28
BCS (opr) Branch if Carry Set
?C = 1
25
BCC (opr) Branch if Carry Clear
?C = 0
24
Source
Form
Operation
30
Signed Conditional Branches
Test combinations of N, Z and V
Boolean
Expression
Opcode
BGT (opr) Branch if > Zero
?Z + (N  V) = 0
2E
BLE (opr) Branch if <= Zero
?Z + (N  V) = 1
2F
BGE (opr) Branch if >= Zero
?N  V = 0
2C
BLT (opr) Branch if < Zero
?N  V = 1
2D
Source
Form
Operation
31
Unsigned Conditional Branches
Test combinations of Z and C
Boolean
Expression
Opcode
?C + Z = 0
22
BLS (opr) Branch if Lower or Same
?C + Z = 1
23
BHS (opr) Branch if Higher or Same
?C = 0
24*
BLO (opr) Branch if Lower
?C = 1
25**
Source
Form
BHI (opr)
Operation
Branch if Higher
* Same Opcode as BCC
** Same Opcode as BCS
32
Other Branch Instructions
Bit Manipulation Branches
Source Form
Operation
Boolean
Addr
Expression Mode Opcode
BRCLR (opr)(msk)(rel) Branch if bit(s) Clear ?M • mm = 0
BRSET (opr)(msk)(rel) Branch if bit(s) Set
DIR
13
IND,X 1F
IND,Y 18 1F
?M • mm = 0 DIR
14
IND,X 1C
IND,Y 18 1C
Branch to Subroutine
BSR (opr)
Branch to Subroutine
Special*
REL
8D
* See Special Operations page in the PRG
33
Comparison Instructions
8 Bit Comparisons
Source Form
Operation
Boolean
Expression
Addr
Mode Opcode
CMPA (opr) Compare A to Memory
A-M
IMM
81
DIR
91
EXT
B1
IND,X A1
IND,Y 18 A1
CMPB (opr) Compare B to Memory
B-M
IMM
C1
DIR
D1
EXT
F1
IND,X E1
IND,Y 18 E1
34
Comparison Instructions
16 Bit Comparisons
Source Form
Operation
Boolean
Addr
Expression Mode
Opcode
CPD (opr)
Compare D to Memory
D - M:M+1
IMM
DIR
EXT
IND,X
IND,Y
1A 83
1A 93
1A B3
1A A3
CD A3
CPX (opr)
Compare X to Memory
X - M:M+1
IMM
8C
DIR
9C
EXT
BC
IND,X AC
IND,Y CD AC
CPY (opr)
Compare Y to Memory
Y - M:M+1
35
Do Activity #2 Now
Due: End of Class Today.
RETAIN THE LAST PAGE(S) (#3 onwards)!!
For Next Class:
• Bring Huang Textbook, & HC11 PRG
• Required Reading:
– Sec 2.9– 2.10, 3.3, 3.6, 3.7 of Huang
• This reading is necessary for getting points in
the Studio Activity!
36
Download