08-Assembly_Language

Report
© 2010 Kettering University, All rights reserved.
Microcomputers I – CE 320
Jaerock Kwon, Ph.D.
Electrical and Computer Engineering
Kettering University
[email protected]
http://www.kettering.edu/~jkwon
Announcements
• Quiz 2 on Friday.
 An instruction reference booklet will be given for the quiz.
 Exercise 2 will be greatly helpful.
• You are expected to know how to convert assembly code lines to
machine codes.
• Also you need to know how to get post bytes from index addressing
mode representations.
 Taken or not-taken problems of conditional branches (BHI, BHS,
BLS, BLO, BGT, BGE, BLE, BNE, BEQ)
• Lab reports will be uploaded a week earlier.
Lecture 8:
Assembly Language
Today’s Topics
• Review the concept of memories and registers (accumulators)
• Generating machine code manually.
 You are expected to convert assembly code lines to machine
codes.
• Files and processes associated with converting assembly
source code to machine code
• To learn assembler directives
A Short Story
•
World is converging again.
 No pure EE, CE, and CS.
• Fundamentals are always important.
• A short story about hiring interviews.
 Very simple but fundamental questions are asked even to Senior
engineers.
• Grade or GPA is important but…
• Last but not least, you are expected to study outside
classroom. Lectures cannot cover all topics.
Microcontrollers (or Microcomputers)
Basic ideas
• Microcontroller
 CPU core + I/O ports + Memories (RAM and ROM) + …
• Memories
 We only use RAM area to learn assembly language and test
programs.
 No need to worry about burning your program into ROM.
• Registers (accumulators) vs. memories
 Registers are small read/write memory cells inside CPU core.
 Memories are located outside CPU.
 To get a value from a location in a memory, the value should
travel through data bus. (Remember memory modules are
separated from CPU core)
• This takes time (much longer than getting from/setting to Registers)
Microcontrollers (or Microcomputers)
Load and Store / Move / Transfer
CPU Core
Memory
Load
Register A
Register B
Store
Transfer
Move
Before arithmetic operations including
comparisons, the microcontroller
requires a value on a register to do the
operations.
Microcontrollers (or Microcomputers)
Load and Store / Move / Transfer
CPU Core
Memory
Load
Register A
Register B
Store as a master of a
As a programmer, you may think yourself
small world. You can do whatever you want as long as the
Transfer
environment supports for you.
When it comes to the environment, you have two main tools
for now (bunch of other things will come soon); a storage to
save data; and a process unit to manipulate the data/to
conduct arithmetic operations and logical decisions.
Before arithmetic
operations including
To control the processor and manipulate the memory, we
comparisons,
microcontroller
writethe
programs.
requires a value on a register to do the
operations.
Move
Code Line and Program Counter
• When we say Code Line in Lab assignments, quizzes, and
exams, the instruction line is completed (executed). So
registers are supposed to be affected by the execution.
• Program Counter always points the NEXT instruction!!
 Caution on Branches. PC depends on whether the branch is taken
or not.
Trace
Line
PC
A
B
X
N
Z
V
• Example:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
1500
1503
1508
150A
150C
150E
1511
1513
1516
1517
1518
151A
CE 2000
180B FF 1000
C6 02
27 0E
A6 00
B1 1000
24 03
7A 1000
08
53
20 F0
3F
LDX #$2000
MOVB #$FF,$1000
LDAB #2
BEQ 14
LDAA 0,X
CMPA $1000
BHS 3
STAA $1000
INX
DECB
BRA -16
SWI
C
1
1
1503
-
-
2000
0
0
0
-
2
2
1508
-
-
2000
0
0
0
-
3
3
150A
-
02
2000
0
0
0
-
4
4
150C
-
02
2000
0
0
0
-
5
5
150E
40
02
2000
0
0
0
-
6
6
1511
40
02
2000
0
0
0
1
7
7
1513
40
02
2000
0
0
0
1
8
8
1516
40
02
2000
0
0
0
1
9
9
1517
40
02
2001
0
0
0
1
10
10
1518
40
01
2001
0
0
0
1
Machine Code
Manually generate machine code*
Address
Machine Code
Source Code
1500
CE 2000
LDX #$2000
1503
180B FF 1000
MOVB #$FF, $1000
1508
C6 02
LDAB #2
150A
27 0E
BEQ 14
150C
A6 00
LDAA 0,X
150E
B1 1000
CMPA $1000
1511
24 03
BHS 3
1513
7A 1000
STAA $1000
1516
08
INX
1517
53
DECB
1518
20 F0
BRA -16
151A
3F
SWI
Assembly Process
General case
Assembly source code:
prog.asm
Assembler
ASCII text file
.asm extension
Cross-assembler:
Translate assembly code into object code.
Object code:
Mix of machine code and additional information
Object file:
prog.obj
Listing file:
prog.lst
Linker:
Combine multiple object files into a single piece
of machine code
Linker
Machine
code file
Listing file:
Human readable log file containing the original
assembly and the machine code.
Loader
Microcontroller
Assembly Process
In the lab
Assembly source code:
prog.asm
Assembler
S19 file:
prog.s19
Loader
Microcontroller
Listing file:
prog.lst
ASCII text file
.asm extension
Cross-assembler:
Translate assembly code into object code.
Listing file:
Human readable log file containing the original
assembly and the machine code.
S19 file: Motorola S-record format
An ASCII encoding for binary data.
http://en.wikipedia.org/wiki/S19_(file_format)
Loader:
D-Bug12 is used to load .s19 file to the memory
of the microcontroller. Load instruction is used.
Proper Assembly Code
•
1. Separate the source code into constant section, data and
variable sections, and code section.
•
2. Do not use numbers within the code
 Except for possibly 0 or 1 in obvious situation
•
Always begin with a comment block stating





Purpose of the program
Inputs
Outputs
Programmer
Anything else useful
•
Comment within the code
•
Assume that a reader understands the processor’s assembly
code, so do not use comments to simply rephrase the assembly
code.
Assembly Language Syntax
• The assembly language consists of lines of text in the form:
 [label:] [command] [operand(s)] [;comment]
or
 ; comment
 where ‘:’ indicates the end of label and ‘;’ defines the start of a
comment. The command field may be an instruction, a directive
or a macro call.
Assembler Directives
Only small fractions…
•
Memory allocation

org: puts an address into the assembler location counter

ds.[size] (Define Storage)
; allocate specified number of storage spaces
ds.b (Define Storage Bytes)
ds.w (Define Storage Words)
• org $1000
• buffer
ds.b
100
• dbuffer ds.w
100
•
Data formation

dc[.size] (Define Constant)
; allocate and initialize storage for variables
dc.b (Define Constant Byte)
dc.w (Define Constant Word)
• array
dc.b
$11, $12,$22
• Initialize a 3-byte constant with the data
•
Note: need to know a difference between

buffer ds.b $10

buffer dc.b $10
• allocate 16 byte memory space for ‘buffer’
• allocate 1 byte for ‘buffer’ and the value of ‘buffer’ will be initialized to $10
Assembler Directives
• Symbol Definition
 equ
• TRUE equ $FF
• Numbers
 $xx or xxh: hexadecimal
 %xxxx.. : binary
 Otherwise: decimal
Some More Instructions
Load Effective Address Instructions
• Load effective address instructions
 LEAX: Load effective address into X
• LEAX 10,X
 LEAY: Load effective address into Y
• LEAY B, Y
 LEAS: Load effective address into SP
• LEAS 0,PC
• Can you tell a difference between “LEAX 10,X” and “LDX 10,X”?
 Assuming (X) = 1200, the content at 120A is 34h, and at 120B is
56h
• “LEAX 10,X” makes X be 120A
• “LDX 10,X” makes X be the content at memory (120A+120B) which is
3456.
Some More Instructions
Addition and Subtraction
•
•
8 bit addition


ABA: (A) + (B)  A; Note that there is no AAB instruction!
ADDA: (A) + (M)  A

ADDB: (B) + (M)  B


ADCA: (A) + (M) + C  A
ADCB: (B) + (M) + C  B
• ADDB #10
8 bit subtraction





•
• ADDA $1000
SBA: (A) – (B)  A; Subtract B from A (Note: not SAB instruction!)
SUBA: (A) – (M)  A; Subtract M from A
SUBB: (B) – (M)  B
SBCA: (A) – (M) – C  A
SBCB: (B) – (M) – C  B
There is a pattern that make you
16 bit addition and subtraction




ADDD: (A:B) + (M:M+1)  A:B
SUBD: (A:B) – (M:M+1)  A:B
ABX: (B) + (X)  X
ABY: (B) + (Y)  Y
be easy to remember the
instructions!!!
1. The last letter in these
instructions is the destination!
2. Also it comes to the first in the
operation
Some More Instructions
Increments, Decrements, and Negate
•
Increments






•
Decrements






•
INC: (M) + 1  M
INCA: (A) + 1  A
INCB
INS
INX
INY
DEC
DECA
DECB
DES
DEX
DEY
Negate



NEG: negate a memory byte
NEGA
NEGB
Note that we don’t
have IND and DED!
Assembly Code Example
Requirements
• Write a program to copy a table of one-byte values.
• Our table will be defined by a starting address, supplied at
$1000, and by a one-byte number of elements in the table,
supplied at $1002.
• The table will be copied a given distance from the original
table, and this two-byte offset will be supplied at address
$1003.
Just one example
…
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
Symbols:
done
length
loop
offset
table
=00001000
1000 +0002
1002 +0001
1003 +0002
=00001800
1800 FE 1000
1803 B7 54
1805 F3 1003
1808 B7 46
180A F6 1002
180D 27 09
180F 180A 00 40
1813 08
1814 02
1815 53
1816 20 F5
1818 3F
table
length
offset
loop
done
*00001818
*00001002
*0000180d
*00001003
*00001000
ORG
ds.w
ds.b
ds.w
$1000
1
1
1
ORG
LDX
TFR
ADDD
TFR
LDAB
BEQ
MOVB
INX
INY
DECB
BRA
SWI
$1800
table
X,D
offset
D,Y
length
done
0,X,0,Y
loop
1000
20
1001
00
1002
40
1003
05
1004
00
…
2000
12
2001
34
2002
56
2003
78
2004
55
…
2500
2501
2502
2503
2504
…
Modification of the Example
• What changes are required to handle a table of two-byte
numbers?
 Need to copy two bytes instead of one byte.
• What changes are required to handle a two-byte length?
 The length should represent two-byte numbers!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
Symbols:
done
length
loop
offset
table
=00001000
1000 +0002
1002 +0001
1003 +0002
=00001800
1800 FE 1000
1803 B7 54
1805 F3 1003
1808 B7 46
180A F6 1002
180D 27 09
180F 180A 00 40
1813 08
1814 02
1815 53
1816 20 F5
1818 3F
table
length
offset
loop
done
ORG
ds.w
ds.b
ds.w
$1000
1
1
1
ORG
LDX
TFR
ADDD
TFR
LDAB
BEQ
MOVB
INX
INY
DECB
BRA
SWI
$1800
table
X,D
offset
D,Y
length
LDD
done
0,X,0,Y MOVW 0,X,0,Y
length
ds.w
1
length
Add additional INX
instruction
loop
Add additional INY
instruction
*00001818
*00001002
*0000180d
*00001003
*00001000
SUBD #1
Questions?
Wrap-up
What we’ve learned
• Registers and memories
• Generating machine code manually.
• Concept of assembly language
• Assembler directives
What to Come
• Flowcharts
• Some assembly programming examples

similar documents