Machine Language Prog Slides

advertisement
Chapter 6
• Programming in Machine Language
• The LC-3 Simulator
• The LC-3 Editor
The LC-3 Computer
a von Neumann machine
The Instruction Cycle:
Fetch:
Next Instruction from Memory
(PC)  (points to) next instruction
PC (PC) + 1
Decode:
Fetched Instruction
Evaluate: Instr & Address (es)
(find where the data is)
Load:
PSW
Operand (s)
(get data as specified)
Execute: Operation
Store:
Result
(if specified)
PSW (Program Status Word):
Bits: 15
| S|
10 9 8
|Priority|
Memory
2 1 0
| N| Z| P|
Important Registers in the LC-3
CPU Registers:
•
8 General Purpose Registers (R0 – R7) – Holds Data or Addresses
•
Program Counter (PC) - Points to the next instruction
•
Instruction Register (IR) – holds the instruction being executed
•
Program Status Word (PSW) – holds the status of the program being executed, including N Z P: Negative, Zero,
Positive result of an operate instruction
Memory Access Registers:
•
Memory Address Register (MAR) – Holds the address of a memory location being accessed
•
Memory Data Register (MDR) – Hold the data to be written into memory or the date read from memory
Note: These are all 16 bit registers
LC-3 Memory Map
(64K of 16 bit words)
256 words
256 words
(We will get to these later)
23.5 K words
39.5 K words
512 words
LC-3 Instructions
Addressing Modes
• Register
(Operand is in one of the 8 registers)
•
PC-relative
(Operand is “offset” from where the PC points
- offsets are sign extended to 16 bits)
•
Base + Offset (Base relative)
(Operand is “offset” from the contents of a register)
•
Immediate
(Operand is in the instruction)
•
Indirect
(The “Operand” points to the real address of Operand
– rather than being the operand)
Note: The LC-3 has No Direct Addressing Mode
Operate Instructions
•
There are only three operate Instructions:
- ADD Register mode
Register/Immediate mode
- AND
- NOT
•
Register mode
Register/Immediate mode
Register mode
[0001 DR SR1 0 00 SR2]
[0001 DR SR1 1 imm5]
[0101 DR SR1 0 00 SR2]
[0101 DR SR1 1 imm5]
[1001 DR SR 111111]
The Source and Destination operands are:
CPU Registers
or
Immediate Values
Data Movement Instructions
• Load - read data from memory to a
– LD:
PC-relative mode
– LDI:
Indirect mode
– LDR:
Base+offset mode
register
[0010 DR PCoffset9]
[1010 DR PCoffset9]
[0110 DR BaseR offset6]
• Store - write data from a register to memory
– ST:
PC-relative mode
[0011 DR PCoffset9]
– STI:
Indirect mode
[1011 DR PCoffset9]
– STR:
Base+offset mode
[0111 DR BaseR offset6]
• Load effective address – address saved in register
– LEA:
PC-relative mode
[1110 DR PCoffset9]
All have 2 or 3 operands
Control Instructions
• Go to New Location in Program – “GO TO”
– BR:
– JMP:
PC-relative mode
Indirect mode
• Trap Service Routine Call
– TRAP:
Indirect
• Jump to Subroutine (will be covered later)
– JSR:
– JSRR:
PC-relative mode
Indirect mode
• Return from Trap/Subroutine
– RET:
No operand
[0000 NZP PCoffset9]
[1100 000 BaseR 000000]
[1111 0000 TrapVec8]
[0100 1 PCoffset11]
[0100 000 BaseR 000000]
[1100 000 111 000000]
• Return from Interrupt (will be covered later)
– RTI:
No operand
[1000 000000000000]
TRAP Instruction
•
Calls a service routine, identified by 8-bit “trap vector.”
vector
•
•
•
Service routine (Partial List)
x23
input a character from the keyboard
x21
output a character to the monitor
x25
halt the program
Register R7 is loaded with the incremented contents of the PC.
The PC is loaded with the address in the Trapvector Table at position “trapvector8”
R0 is typically used for passing values between the Program and the Trap Routine
RET [1100 000 111 000000]
•
When service routine is done, an RET will load R7 (the incremented value of the PC
before jumping to the TRAP routine) into the PC, and the program will continue with
the next instruction after the TRAP, i.e. the program will “return” from the TRAP
Routine.
Note: an RET is a JMP Base-relative with Base = R7
TRAPS
See page 543.
Example:
Program to multiply [R4] x [R5]
and place the result in R2
clear R2
add R4 to R2
decrement R5
No
R5 = 0?
R4 – Multiplicand
Yes
HALT
R5 – Multiplier
R2 – Accumulator
LC-3 Instructions
Addressing Modes
• Register
(Operand is in one of the 8 registers)
•
PC-relative
(Operand is “offset” from where the PC points
- offsets are sign extended to 16 bits)
•
Base + Offset (Base relative)
(Operand is “offset” from the contents of a register)
•
Immediate
(Operand is in the instruction)
•
Indirect
(The “Operand” points to the real address of Operand
– rather than being the operand)
Note: The LC-3 has No Direct Addressing Mode
Example:
Program to multiply [R4] x [R5]
and place the result in R2
clear R2
add R4 to R2
decrement R5
No
R5 = 0?
Yes
HALT
R2 <- 0
R2 <- R2 + R4
R5 <- R5 – 1
BRz x3201
HALT
x3200
?
Example:
Program to multiply [R4] x [R5]
and place the result in R2
clear R2
add R4 to R2
decrement R5
No
R5 = 0?
Yes
HALT
R2 <- 0
R2 <- R2 + R4
R5 <- R5 – 1
BRz x3201
HALT
x3200
x3201
x3202
x3203
x3204
0101010010100000
0001010010000100
0001101101111111
0000010111111101
1111000000100101
Example:
Program to multiply [R4] x [R5]
and place the result in R2
clear R2
add R4 to R2
decrement R5
No
R5 = 0?
Yes
HALT
R2 <- 0
R2 <- R2 + R4
R5 <- R5 – 1
BRzp x3201
HALT
x3200
x3201
x3202
x3203
x3204
0101010010100000
0001010010000100
0001101101111111
0000010111111101
1111000000100101
54A0
1484
1B7F
02FD
FO25
LC3 Editor / Simulator
Go to Author’s Web Site:
http://www.mhhe.com/patt2
Get:
• LC3 Edit
• LC3 Simulator
LC3 Edit Screen
LC3 Edit
Enter (or Load) the program into LC3 Edit
- Store it as prog.bin for a binary file, or
Store it as prog.hex for a hex file
- Create a prog.obj file with the Editor
LC-3 Simulator Screen
LC-3 Simulator
Open LC-3 Simulator
- Load prog.obj
- Load data.obj
- Set breakpoint(s)
or
- Step through program
LC-3 Simulator
Open LC-3 Simulator
- Load prog.obj
- Load data.obj
- Initialize values (PC, memory, registers)
- Set breakpoint(s)
- Step through program checking registers
and “memory map”
- Debug program
Example:
Program to multiply [R4] x [R5]
and place the result in R2
clear R2
add R4 to R2
decrement R5
No
R5 = 0?
Yes
HALT
R2 <- 0
R2 <- R2 + R4
R5 <- R5 – 1
BRzp x3201
HALT
x3200
x3201
x3202
x3203
x3204
0101010010100000
0001010010000100
0001101101111111
0000011111111101
1111000000100101
54A0
1484
1B7F
03FD
FO25
Example:
Program to multiply [R4] x [R5]
and place the result in R2
Enter Program in Simulator and test it:
• Enter Program
• Run
• Single Step
• Add Breakpoints
The Sum program in “binary”
x3000
x3001
x3002
x3003
x3004
x3005
x3006
x3007
x3008
x3009
x300A
0011000000000000
1110001011111111
0101011011100000
0101010010100000
0001010010101100
0000010000000101
0110100001000000
0001011011000100
0001001001100001
0001010010111111
0000111111111010
1111000000100101
;start x3000
;R1=x3100
;R3=0
;R2=0
;R2=R2+12
;If z goto x300A
;Load next value into R4
;R3=R3+R4
;R1=R1+1
;R2=R2-1
;goto x3004
;halt
The Sum program in “hex”
x3000
x3001
x3002
x3003
x3004
x3005
x3006
x3007
x3008
x3009
x300A
3000
E2FF
56E0
54A0
14AC
0405
6840
16C4
1261
14BF
0FFA
F025
;start x3000
;R1=x3100
;R3=0
;R2=0
;R2=R2+12
;If z goto x300A
;Load next value into R4
;R3=R3+R4
;R1=R1+1
;R2=R2-1
;goto x3004
;halt
The Sum program Data in “hex”
x3100
x3101
x3102
x3103
x3104
x3105
x3106
x3107
x3108
x3109
x310A
x310B
3100
0001
0002
0004
0008
FFFF
1C10
11B1
0019
0F07
0004
0A00
400F
; Begin data at x3100
; Loc x3100
; Loc x310B
Example
Compute the Sum of 12 Integers Program
•
•
Program begins at location x3000.
Integers begin at location x3100.
R1  x3100
R3  0 (Sum)
R2  12(count)
R2=0?
NO
R4
R3
R1
R2




M[R1]
R3+R4
R1+1
R2-1
YES
R1: “Array” index pointer (Begin with location 3100)
R3: Accumulator for the sum of integers
R2: Loop counter (Count down from 12)
R4: Temporary register to store next integer
Example:
Compute the Sum of 12 Integers Program
Enter Program in Simulator and test it:
• Enter Program
• Enter Data
• Run
• Single Step
• Add Breakpoints
Example:
# of Occurrences of an Inputted Char from a string
Count = 0
(R2 = 0)
Done?
YES
(R1 ?= EOT)
Ptr = 1st file character
Convert count to
ASCII character
(R0 = x30, R0 = R2 + R0)
NO
(R3 = M[x3012])
Print count
YES
Match?
NO
(TRAP x21)
(R1 ?= R0)
Input char
from keybd
(TRAP x23)
HALT
Incr Count
Load char from file
(TRAP x25)
(R2 = R2 + 1)
(R1 = M[R3])
Load next char from file
(R3 = R3 + 1, R1 = M[R3])
R3 – ptr to char string, R1 – input char buffer, R2 – char count, R0 – output buffer
Example:
Counting Occurrences of a Character
Address
Instruction
Comments
x3000
0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0
R2  0 (counter)
x3001
0 0 1 0 0 1 1 0 0 0 0 1 0 0 0 0
R3  M[x3102] (ptr)
x3002
1 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1
Input to R0(TRAP x23)
x3003
0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0
R1  M[R3]
x3004
0 0 0 1 1 0 0 0 0 1 1 1 1 1 0 0
R4  R1 – 4 (EOT)
x3005
0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0
If Z, goto x300E
x3006
1 0 0 1 0 0 1 0 0 1 1 1 1 1 1 1
R1  NOT R1
x3007
0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1
R1  R1 + 1
X3008
0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0
R1  R1 + R0
x3009
0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1
If N or P, goto x300B
x300A
0 0 0 1 0 1 0 0 1 0 1 0 0 0 0 1
R2  R2 + 1
x300B
0 0 0 1 0 1 1 0 1 1 1 0 0 0 0 1
R3  R3 + 1
x300C
0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0
R1  M[R3]
x300D
0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0
Goto x3004
x300E
0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1
R0  M[x3013]
x300F
0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0
R0  R0 + R2
x3010
1 1 1 1 0 0 0 0 0 0 1 0 0 0 0 1
Print R0 (TRAP x21)
x3011
1 1 1 1 0 0 0 0 0 0 1 0 0 1 0 1
HALT (TRAP x25)
X3012
Starting Address of File
x3013
0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0
ASCII x30 (‘0’)
R3 – ptr to char string, R1 – input char buffer,
R2 – char count,
R0 – Keyboard char /Output display char
HW 6:
After reading the Simulator manual:
1) Write a program to place the absolute value of the [R2] in R2.
The program should begin in memory location 3000
Test it on the LC-3 Simulator.
Provide Simulator Snapshots with your homework.
2) Write a program to read a value of N from memory and
calculate N factorial. The result should be in R4.
The program should begin in memory location 3050
N should be in memory location 3000
Test the program on the LC-3 Simulator
Provide Simulator Snapshots with your homework.
Download