Uploaded by Amos Maina

kipdf.com chapter-assembly-language 5ad717d27f8b9a63078b45f1

advertisement
Chapter 5
Assembly
Language
Two types of bit
patterns
• Instructions
‣ Mnemonics for opcodes
‣ Letters for addressing modes
• Data
‣ Pseudo-ops, also called dot commands
Figure 5.1
aaa
Addressing Mode
000
001
010
011
100
101
110
111
Immediate
Direct
Indirect
Stack-relative
Stack-relative deferred
Indexed
Stack-indexed
Stack-indexed deferred
Letters
i
d
n
s
sf
x
sx
sxf
Figure 5.2
Instruction
Specifier
Mnemonic
Instruction
Addressing
Modes
0000 0000
0000 0001
0000 0010
0000 0011
STOP
RETTR
MOVSPA
MOVFLGA
Stop execution
Return from trap
Move SP to A
Move NZVC flags to A
U
U
U
U
0000 010a
0000 011a
0000 100a
0000 101a
0000 110a
0000 111a
0001 000a
0001 001a
0001 010a
0001 011a
BR
BRLE
BRLT
BREQ
BRNE
BRGE
BRGT
BRV
BRC
CALL
Branch unconditional
Branch if less than or equal to
Branch if less than
Branch if equal to
Branch if not equal to
Branch if greater than or equal to
Branch if greater than
Branch if V
Branch if C
Call subroutine
i, x
i, x
i, x
i, x
i, x
i, x
i, x
i, x
i, x
i, x
0001 100r
0001 101r
0001 110r
0001 111r
0010 000r
0010 001r
NOTr
NEGr
ASLr
ASRr
ROLr
RORr
Bitwise invert r
Negate r
Arithmetic shift left r
Arithmetic shift right r
Rotate left r
Rotate right r
U
U
U
U
U
U
Status
Bits
NZ
NZV
NZVC
NZC
C
C
0000 100a
0000 101a
0000 110a
0000 111a
0001 000a
0001 001a
0001 010a
0001 011a
BRLT
BREQ
BRNE
BRGE
BRGT
BRV
BRC
CALL
Branch if less than
Branch if equal to
Branch if not equal to
Branch if greater than or equal to
Branch if greater than
Branch if V
Branch if C
Call subroutine
i, x
i, x
i, x
i, x
i, x
i, x
i, x
i, x
0001 100r
0001 101r
0001 110r
0001 111r
0010 000r
0010 001r
NOTr
NEGr
ASLr
ASRr
ROLr
RORr
Bitwise invert r
Negate r
Arithmetic shift left r
Arithmetic shift right r
Rotate left r
Rotate right r
U
U
U
U
U
U
0010 01nn
0010 1aaa
NOPn
NOP
Unary no operation trap
Nonunary no operation trap
U
i
0011 0aaa
0011 1aaa
0100 0aaa
0100 1aaa
0101 0aaa
DECI
DECO
STRO
CHARI
CHARO
Decimal input trap
Decimal output trap
String output trap
Character input
Character output
d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx, sxf
d, n, sf
d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx, sxf
0101 1nnn
RETn
Return from call with n local bytes
U
0110 0aaa
0110 1aaa
ADDSP
SUBSP
Add to stack pointer (SP)
Subtract from stack pointer (SP)
i, d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx, sxf
NZVC
NZVC
0111 raaa
1000 raaa
1001 raaa
1010 raaa
1011 raaa
ADDr
SUBr
ANDr
ORr
CPr
Add to r
Subtract from r
Bitwise AND to r
Bitwise OR to r
Compare r
i, d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx, sxf
NZVC
NZVC
NZ
NZ
NZVC
Figure 5.2
(Continued)
NZ
NZV
NZVC
NZC
C
C
NZV
0010 01nn
0010 1aaa
NOPn
NOP
Unary no operation trap
Nonunary no operation trap
U
i
0011 0aaa
0011 1aaa
0100 0aaa
0100 1aaa
0101 0aaa
DECI
DECO
STRO
CHARI
CHARO
Decimal input trap
Decimal output trap
String output trap
Character input
Character output
Figure 5.2NZV
d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx,(Continued)
sxf
d, n, sf
d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx, sxf
0101 1nnn
RETn
Return from call with n local bytes
U
0110 0aaa
0110 1aaa
ADDSP
SUBSP
Add to stack pointer (SP)
Subtract from stack pointer (SP)
i, d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx, sxf
NZVC
NZVC
0111 raaa
1000 raaa
1001 raaa
1010 raaa
1011 raaa
ADDr
SUBr
ANDr
ORr
CPr
Add to r
Subtract from r
Bitwise AND to r
Bitwise OR to r
Compare r
i, d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx, sxf
NZVC
NZVC
NZ
NZ
NZVC
1100 raaa
1101 raaa
1110 raaa
1111 raaa
LDr
LDBYTEr
STr
STBYTEr
Load r from memory
Load byte from memory
Store r to memory
Store byte r to memory
i, d, n, s, sf, x, sx, sxf
i, d, n, s, sf, x, sx, sxf
d, n, s, sf, x, sx, sxf
d, n, s, sf, x, sx, sxf
NZ
NZ
The unimplemented
opcode instructions
• NOPn
• NOP
• DECI
• DECO
• STRO
Unary no operation trap
Nonunary no operation trap
Decimal input trap
Decimal output trap
String output trap
Pseudo-operations
• .ADDRSS
• .ASCII
• .BLOCK
• .BURN
• .BYTE
• .END
• .EQUATE
• .WORD
The address of a symbol
A string of ASCII bytes
A block of bytes
Initiate ROM burn
A byte value
The sentinel for the assembler
Equate a symbol to a constant value
A word value (two bytes)
Figure 5.3
Assembler Input
;Stan Warford
;January 13, 2009
;A program to output "Hi"
;
CHARO
0x0007,d
;Output 'H'
CHARO
0x0008,d
;Output 'i'
STOP
.ASCII "Hi"
.END
Assembler Output
51 00 07 51 00 08 00 48 69 zz
Program Output
Hi
Figure 5.4
7
Application
level
6
High-order
language level
5
Assembly
level
4
Operating system
level
3
Instruction set
architecture level
2
Microcode
level
1
Logic gate
level
Figure 5.5
Input
CHARO
CHARO
STOP
.ASCII
.END
0x0007,d
0x0008,d
"Hi"
Processing
Output
Assembler
51 00 07 51 00
08 00 48 69 zz
Figure 5.6
Assembler Input
CHARI
CHARI
CHARO
CHARO
STOP
.BLOCK
.BLOCK
.END
0x000D,d
0x000E,d
0x000E,d
0x000D,d
;Input first character
;Input second character
;Output second character
;Output first character
1
1
;Storage for first char
;Storage for second char
Assembler Output
49 00 0D 49 00 0E 51 00 0E 51 00 0D 00 00 00 zz
Program Input
up
Program Output
pu
Figure 5.7
Assembler Input
LDA
ADDA
ORA
STBYTEA
CHARO
STOP
.BLOCK
.WORD
.WORD
.WORD
.END
0x0011,d
0x0013,d
0x0015,d
0x0010,d
0x0010,d
;A <- first number
;Add the two numbers
;Convert sum to character
;Store the character
;Output the character
1
5
3
0x0030
;Character to output
;Decimal 5
;Decimal 3
;Mask for ASCII char
Assembler Output
C1 00 11 71 00 13 A1 00 15 F1 00 10 51 00 10 00
00 00 05 00 03 00 30 zz
Program Output
8
Figure 5.8
Input
Processing
Output
Application
(assembly language)
Assembler
(machine language)
Application
(machine language)
up
Application
(machine language)
pu
Figure 5.9
Assembler Input
chari 0x000D,d
;Input first character
CHARI
0x000E,d
;Input second character
charo 0x000E,
d
;Output second character
CHarO
0x000D,D
;Output first character
stop
.bloCK 1 ;Storage for first char
.BLOCK 1
;Storage for second char
.END
Assembler Listing
Addr
0000
0003
0006
0009
000C
000D
000E
000F
Object
code
49000D
49000E
51000E
51000D
00
00
00
Mnemon
CHARI
CHARI
CHARO
CHARO
STOP
.BLOCK
.BLOCK
.END
Operand
0x000D,d
0x000E,d
0x000E,d
0x000D,d
Comment
;Input first character
;Input second character
;Output second character
;Output first character
1
1
;Storage for first char
;Storage for second char
Direct addressing
• Oprnd = Mem[OprndSpec]
• Asmb5 letter: d
• The operand specifier is the address in
memory of the operand.
Immediate addressing
• Oprnd = OprndSpec
• Asmb5 letter: i
• The operand specifier is the operand.
Figure 5.10
0000
0003
0006
0007
500048 CHARO
500069 CHARO
00
STOP
.END
Output
Hi
'H',i
'i',i
;Output 'H'
;Output 'i'
The decimal input
instruction
• Instruction specifier: 0011 0aaa
• Mnemonic: DECI
• Convert a string of ASCII characters from
the input device into a 16-bit signed integer
and store it into memory
Oprnd ← {decimal input}
The decimal output
instruction
• Instruction specifier: 0011 1aaa
• Mnemonic: DECO
• Convert a 16-bit signed integer from
memory into a string of ASCII characters
and send the string to the output device
{decimal output} ← Oprnd
The unconditional
branch instruction
• Instruction specifier: 0000 010a
• Mnemonic: BR
• Skips to a different memory location for the
next instruction to be executed.
PC ← {Oprnd}
Figure 5.11
0000
0003
0005
0008
000B
000E
0011
0014
0017
001A
001D
0020
0023
0026
0029
002C
002D
040005 BR
0000
.BLOCK
;
310003 DECI
390003 DECO
500020 CHARO
50002B CHARO
500020 CHARO
500031 CHARO
500020 CHARO
50003D CHARO
500020 CHARO
C10003 LDA
700001 ADDA
E10003 STA
390003 DECO
00
STOP
.END
Input
-479
Output
-479 + 1 = -478
0x0005
2
;Branch around data
;Storage for one integer
0x0003,d
0x0003,d
' ',i
'+',i
' ',i
'1',i
' ',i
'=',i
' ',i
0x0003,d
1,i
0x0003,d
0x0003,d
;Get the number
;and output it
;Output " + 1 = "
;A <- the number
;Add one to it
;Store the sum
;Output the sum
The string output
instruction
• Instruction specifier: 0100 0aaa
• Mnemonic: STRO
• Send a string of null-terminated ASCII
characters to the output device
{string output} ← Oprnd
Figure 5.12
0000
0003
0005
000D
0010
0013
0016
0019
001C
001F
0022
0023
04000D BR
0000
.BLOCK
202B20 .ASCII
31203D
2000
;
310003 DECI
390003 DECO
410005 STRO
C10003 LDA
700001 ADDA
E10003 STA
390003 DECO
00
STOP
.END
Input
-479
Output
-479 + 1 = -478
0x000D
;Branch around data
2
;Storage for one integer
" + 1 = \x00"
0x0003,d
0x0003,d
0x0005,d
0x0003,d
1,i
0x0003,d
0x0003,d
;Get the number
;and output it
;Output " + 1 = "
;A <- the number
;Add one to it
;Store the sum
;Output the sum
Interpreting bit patterns
• Dot commands set bit patterns at assembly
time
• Executable statements interpret bit patterns
at run time
Figure 5.13
0000
0003
0005
0006
0007
040009
FFFE
00
55
0470
0009
000C
000F
0012
0015
0018
001B
001C
390003
50000A
390005
50000A
510006
510008
00
Output
-2
85
Up
BR
.WORD
.BYTE
.BYTE
.WORD
;
DECO
CHARO
DECO
CHARO
CHARO
CHARO
STOP
.END
0x0009
0xFFFE
0x00
'U'
1136
;Branch around data
;First
;Second
;Third
;Fourth
0x0003,d
'\n',i
0x0005,d
'\n',i
0x0006,d
0x0008,d
;Interpret First as decimal
;Interpret Second and Third as decimal
;Interpret Third as character
;Interpret Fourth as character
Disassembler
• The inverse mapping of an assembler is not
unique
• Given a bit pattern at level ISA3, you cannot
determine the Asmb5 statement that
produced it
Figure 5.14
Assembly Language Program
0000
0003
0006
0009
000A
000D
51000A
51000B
51000C
00
50756E
CHARO
CHARO
CHARO
STOP
.ASCII
.END
0x000A,d
0x000B,d
0x000C,d
"Pun"
Assembly Language Program
0000
0003
0006
0009
000A
000D
51000A
51000B
51000C
00
50756E
CHARO
CHARO
CHARO
STOP
CHARO
.END
Program Output
Pun
0x000A,d
0x000B,d
0x000C,d
0x756E,i
Mappings
• The mapping from Asmb5 to ISA3 is one-toone
• The mapping from HOL6 to Asmb5 is oneto-many
Symbols
• Defined by an identifier followed by a colon
at the start of a statement
• The value of a symbol is the address of the
object code generated by the statement
Figure 5.15
Assembler Listing
Addr
0000
0003
0005
000D
0010
0013
0016
0019
001C
001F
0022
0023
Object
code
Symbol
04000D
0000
num:
202B20 msg:
31203D
2000
;
310003 main:
390003
410005
C10003
700001
E10003
390003
00
Symbol table:
Symbol
Value
main
000D
num
0003
Mnemon
Operand
BR
.BLOCK
.ASCII
main
;Branch around data
2
;Storage for one integer
" + 1 = \x00"
DECI
DECO
STRO
LDA
ADDA
STA
DECO
STOP
.END
num,d
num,d
msg,d
num,d
1,i
num,d
num,d
Symbol
msg
Value
0005
Comment
;Get the number
;and output it
;Output ' + 1 = '
;A := the number
;Add one to it
;Store the sum
;Output the sum
Figure 5.15
(Continued)
Input
-479
Output
-479 + 1 = -478
Figure 5.16
Assembler Input
this: DECO this,d
STOP
.END
Assembler Listing
0000
0003
0004
390000 this:
00
Output
14592
DECO
STOP
.END
this,d
Figure 5.17
7
Application
level
Application
level
6
High-order
language level
High-order
language level
5
Assembly
level
Assembly
level
4
Operating system
level
Operating system
level
3
Instruction set
architecture level
Instruction set
architecture level
2
Microcode
level
Microcode
level
1
Logic gate
level
Logic gate
level
(a) Translation directly to
machine language.
(b) Translation to assembly
language.
Translating cout
• Translate string output with STRO
• Translate character output with CHARO
• Translate integer output with DECO
Figure 5.18
High-Order Language
#include <iostream>
using namespace std;
int main () {
cout << "Love" << endl;
return 0;
}
Assembly Language
0000
0003
0006
0007
410007
50000A
00
4C6F76 msg:
6500
000C
Output
Love
STRO
CHARO
STOP
.ASCII
.END
msg,d
'\n',i
"Love\x00"
Figure 5.19
Input
#include <iostream>
using namespace std;
int main () {
cout << "Love" << endl;
return 0;
}
Processing
Compiler
Output
41 00 07 50 00 0A 00
4C 6F 76 65 00 zz
(a) A compiler that translates directly into machine language.
#include <iostream>
using namespace std;
int main () {
cout << "Love" << endl;
return 0;
}
Compiler
msg:
(b) A compiler that translates into assembly language.
STRO msg,d
CHARO '\n',i
STOP
.ASCII"Love\x00"
.END
ory
ss
o
Figure 5.20
C++ identifier
for variable name
Memory
address
(a) A compiler that translates to
machine language.
C++ identifier
for variable name
Pep/8 assembly
language symbol
C++ identifier
for variable nam
(b) A hypotheti
Memory
address
(b) A hypothetical compiler for illustrative purposes.
Global variables
• Allocated at a fixed location in memory
with .BLOCK
• Accessed with direct addressing (d)
Assignment statements
• Load the accumulator from the right hand
side of the assignment with LDA
• Compute the value of the right hand side of
the assignment if necessary
• Store the value to the variable on the left
hand side of the assignment with STA
Figure 5.21
High-Order Language
#include <iostream>
using namespace std;
char ch;
int j;
int main () {
cin >> ch >> j;
j += 5;
ch++;
cout << ch << endl << j << endl;
return 0;
}
Figure 5.21
(Continued)
Assembly Language
0000
0003
0004
0006
0009
000C
000F
0012
0015
0018
001B
001E
0021
0024
0027
002A
002B
040006
00
ch:
0000
j:
;
490003 main:
310004
C10004
700005
E10004
D10003
700001
F10003
510003
50000A
390004
50000A
00
BR
.BLOCK
.BLOCK
main
1
2
CHARI
DECI
LDA
ADDA
STA
LDBYTEA
ADDA
STBYTEA
CHARO
CHARO
DECO
CHARO
STOP
.END
ch,d
j,d
j,d
5,i
j,d
ch,d
1,i
ch,d
ch,d
'\n',i
j,d
'\n',i
;global variable #1c
;global variable #2d
;cin >> ch
;
>> j
;j += 5
;ch++
;cout << ch
;
<< endl
;
<< j
;
<< endl
Figure 5.21
(Continued)
Input
M 419
Output
N
424
Figure 5.22
#include <iostream>
using namespace std;
char ch;
int j;
int main () {
cin >> ch >> j;
j += 5;
ch++;
cout << ch << endl << j << endl;
return 0;
}
symbol value
kind
[0]
ch
0003
sChar
[1]
j
0004
sInt
[2]
Figure 5.23
#include <iostream>
using namespace std;
int j;
float y;
int main () {
...
j = j % 8;
...
y = y % 8; // Compile error
...
}
symbol value
kind
[0]
j
0003
sInt
[1]
y
0005
sFloat
[2]
Trace tags
• Format trace tags
‣ Required for global and local variables
• Symbol trace tags
‣ Not required for global variables
Format trace tags
• #1c
• #1d
• #2d
• #1h
• #2h
One-byte character
One-byte decimal
Two-byte decimal
One-byte hexadecimal
Two-byte hexadecimal
The arithmetic shift
right instruction
• Instruction specifier: 0001 111r
• Mnemonic: ASRr (ASRA, ASRX)
• Performs a one-bit arithmetic shift right on a
16-bit register
C ← r"15# , r"1..15# ← r"0..14#;
N←r<0, Z←r=0
Figure 5.24, 5.25
0
Instruction specifier
Opcode
r
0
0
0
1
1
1
1 1
E
CPU
CPU
NZC
A
(a) Before
0098
1E
Arithmetic shift right
accumulator
NZC
A
(b) After
000
004C
The arithmetic shift
left instruction
• Instruction specifier: 0001 110r
• Mnemonic: ASLr (ASLA, ASLX)
• Performs a one-bit arithmetic shift left on a
16-bit register
C ← r"0# , r"0..14# ← r"1..15# , r"15# ← 0 ;
N ← r < 0 , Z ← r = 0 , V ← {overflow}
The rotate left
instruction
• Instruction specifier: 0010 000r
• Mnemonic: ROLr (ROLA, ROLX)
• Performs a one-bit rotate left on a 16-bit
register
C ← r"0# , r"0..14# ← r"1..15# , r"15# ← C ;
The rotate right
instruction
• Instruction specifier: 0010 001r
• Mnemonic: RORr (RORA, RORX)
• Performs a one-bit rotate right on a 16-bit
register
C ← r"15# , r"1..15# ← r"0..14# , r"0# ← C ;
Constants
• Equate the constant to its value
with .EQUATE
• .EQUATE does not generate object code
• The value of the constant symbol is not an
address
Figure 5.26
High-Order Language
#include <iostream>
using namespace std;
const int bonus = 5;
int exam1;
int exam2;
int score;
int main () {
cin >> exam1 >> exam2;
score = (exam1 + exam2) / 2 + bonus;
cout << "score = " << score << endl;
return 0;
}
Figure 5.26
(Continued)
Assembly Language
0000
0003
0005
0007
0009
000C
000F
0012
0015
0016
0019
001C
001F
0022
0025
0026
002F
040009
bonus:
0000
exam1:
0000
exam2:
0000
score:
;
310003 main:
310005
C10003
710005
1E
700005
E10007
410026
390007
50000A
00
73636F msg:
726520
3D2000
BR
.EQUATE
.BLOCK
.BLOCK
.BLOCK
main
5
2
2
2
DECI
DECI
LDA
ADDA
ASRA
ADDA
STA
STRO
DECO
CHARO
STOP
.ASCII
exam1,d
exam2,d
exam1,d
exam2,d
.END
;constant
;global variable #2d
;global variable #2d
;global variable #2d
bonus,i
score,d
msg,d
score,d
'\n',i
;cin >> exam1
;
>> exam2
;score = (exam1
;
+ exam2)
;
/ 2
;
+ bonus
"score = \x00"
;cout << "score = "
;
<< score
;
<< endl
Figure 5.26
(Continued)
Symbol table:
Symbol
Value
bonus
0005
exam2
0005
msg
0026
Input
68 84
Output
score = 81
Symbol
exam1
main
score
Value
0003
0009
0007
Figure 5.27
Assembly Language
0000
0003
0006
0009
000C
000D
0010
0013
0016
0019
001C
001D
001F
0021
0023
002C
31001D main:
31001F
C1001D
71001F
1E
700005
E10021
410023
390021
50000A
00
;
bonus:
0000
exam1:
0000
exam2:
0000
score:
73636F msg:
726520
3D2000
DECI
DECI
LDA
ADDA
ASRA
ADDA
STA
STRO
DECO
CHARO
STOP
exam1,d
exam2,d
exam1,d
exam2,d
.EQUATE
.BLOCK
.BLOCK
.BLOCK
.ASCII
5
;constant
2
;global variable #2d
2
;global variable #2d
2
;global variable #2d
"score = \x00"
.END
bonus,i
score,d
msg,d
score,d
'\n',i
;cin >> exam1
;
>> exam2
;score = (exam1
;
+ exam2)
;
/ 2
;
+ bonus
;cout << "score = "
;
<< score
;
<< endl
Figure 5.28
Input
Run 1
Processing
Output
A
E
Run 2
F
Run 3
G
Run 4
H
Download