Fall 2001 Comp 222 Peter Smith
Handout #11
Program #5
Due date: Thursday, December 6, 2001
(1) Add to your program, implementations of 9 new instructions
Opcode (decimal) (symbolic) Effect
14 br unconditional branch to operand
17 breq branch to operand if Z=1
16 brlt branch to operand if N=1 and Z=0
29 deci input number from keyboard to operand
30 deco output number from operand to screen
28 charo output character from operand to screen
13 addsp add operand to stack pointer (sp)
24 jsr sp = sp-2; mem[sp] = pc; pc = operand
25 rts pc = mem[sp]; sp = sp+2
(2) Make your program interactive and menu driven. The menu should include the following:
CommandEffect
l Prompt user for file name, call load with that file.
r Call run, prompt user for start address?
d Call display; prompt user for delimiters.
t Toggle trace flag (initially zero/false)
q Quit simulator.
(3) Tracing. If trace flag is on, output information about the current instruction followed by register contents and status flags after the instruction has executed.
Testing
The load file testforp5 on web page is a program that will test all the new instructions and most of those we implemented in program 4. On the next page is a listing of assembly code version of the test program. Following that is the output from a test run of my version of program 5 modified only to show the keyboard inputs in bold.
No later than the due date, turn in a listing of your program and the results of testing it on testforp5 (and any others you wish to devise).
GradingCorrectness50
Testing25
Readability25
; sum below is a Pep/6 translation of
;
; int sum(int N)
; { int temp;
; if N==0) return 0;
; else { temp = N+sum(N-1);
; return temp;
; }
; }
;
;
;
br main
;
sum:addsp d#-2,i; for local variable
loada d#4,s; parameter N
breq zero ; branch if N==0
;
addsp d#-2,i; space for result of recursive call
addsp d#-2,i; space for parameter of recursive call
loada d#8,s;; N
suba d#1,i ; N-1
storea d#0,s ; parameter of
jsr sum ; recursive call
addsp d#2,i ; get rid of parameter of recursion
loada d#0,s ; top of stack is sum(N-1)
addsp d#2,i ; finished with that
adda d#4,s ; add N
storea d#0,s ; put in temp
storea d#6,s; and returned value slot
addsp d#2,i; local variable goes away and
rts ; we exit
;
zero: loada d#0,i
storea d#6,s ; zero is value returned
addsp d#2,i ; local variable goes
rts ; exit
;
;
main:deci N,d
addsp d#-2,i
addsp d#-2,i
loada N,d
storea d#0,s
jsr sum
addsp d#2,i
deco d#0,s
stop
N: .block d#2
.end
$ a.out
command : l
Name of load file: testforp5
load OK 0 0 59
command : d
start and finish (hex): 0 59
Address 0 1 2 3 4 5 6 7 8 9 A B C D E F
000 70 00 3e 68 ff fe 0a 00 04 88 00 34 68 ff fe 68
010 ff fe 0a 00 08 20 00 01 12 00 00 c0 00 03 68 00
020 02 0a 00 00 68 00 02 1a 00 04 12 00 00 12 00 06
030 68 00 02 c8 08 00 00 12 00 06 68 00 02 c8 e9 00
040 57 68 ff fe 68 ff fe 09 00 57 12 00 00 c0 00 03
050 68 00 02 f2 00 00 00 00 00 00 00 00 00 00 00 00
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0000 NZVC: 0000
command : r
start_address (hex): 0
? 6
21
Halt
command : d
start and finish (hex): b80 bb8
Address 0 1 2 3 4 5 6 7 8 9 A B C D E F
b80 00 00 00 1e 00 00 00 00 00 01 00 1e 00 01 00 01
b90 00 03 00 1e 00 02 00 03 00 06 00 1e 00 03 00 06
ba0 00 0a 00 1e 00 04 00 0a 00 0f 00 1e 00 05 00 0f
bb0 00 15 00 50 00 06 00 15 00 00 00 00 00 00 00 00
RegA: 0015 RegX: 0000 RegB: 0000 SP: 0bb6 NZVC: 0000
command : r
start_address (hex): 0
? 100
5050
Halt
command : t
trace is now 1
command : r
start_address (hex): 0
Address: 0 opcode: e register: 0 mode: 0 operand: 003e
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0bb8 NZVC: 0000
Address: 3e opcode: 1d register: 0 mode: 1 operand: 0057
? 3
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0bb8 NZVC: 0000
Address: 41 opcode: d register: 0 mode: 0 operand: fffe
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0bb6 NZVC: 0000
Address: 44 opcode: d register: 0 mode: 0 operand: fffe
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0bb4 NZVC: 0000
Address: 47 opcode: 1 register: 0 mode: 1 operand: 0057
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0bb4 NZVC: 0000
Address: 4a opcode: 2 register: 0 mode: 2 operand: 0000
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0bb4 NZVC: 0000
Address: 4d opcode: 18 register: 0 mode: 0 operand: 0003
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0bb2 NZVC: 0000
Address: 3 opcode: d register: 0 mode: 0 operand: fffe
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0bb0 NZVC: 0000
Address: 6 opcode: 1 register: 0 mode: 2 operand: 0004
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0bb0 NZVC: 0000
Address: 9 opcode: 11 register: 0 mode: 0 operand: 0034
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0bb0 NZVC: 0000
Address: c opcode: d register: 0 mode: 0 operand: fffe
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0bae NZVC: 0000
Address: f opcode: d register: 0 mode: 0 operand: fffe
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0bac NZVC: 0000
Address: 12 opcode: 1 register: 0 mode: 2 operand: 0008
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0bac NZVC: 0000
Address: 15 opcode: 4 register: 0 mode: 0 operand: 0001
RegA: 0002 RegX: 0000 RegB: 0000 SP: 0bac NZVC: 0000
Address: 18 opcode: 2 register: 0 mode: 2 operand: 0000
RegA: 0002 RegX: 0000 RegB: 0000 SP: 0bac NZVC: 0000
Address: 1b opcode: 18 register: 0 mode: 0 operand: 0003
RegA: 0002 RegX: 0000 RegB: 0000 SP: 0baa NZVC: 0000
Address: 3 opcode: d register: 0 mode: 0 operand: fffe
RegA: 0002 RegX: 0000 RegB: 0000 SP: 0ba8 NZVC: 0000
Address: 6 opcode: 1 register: 0 mode: 2 operand: 0004
RegA: 0002 RegX: 0000 RegB: 0000 SP: 0ba8 NZVC: 0000
Address: 9 opcode: 11 register: 0 mode: 0 operand: 0034
RegA: 0002 RegX: 0000 RegB: 0000 SP: 0ba8 NZVC: 0000
Address: c opcode: d register: 0 mode: 0 operand: fffe
RegA: 0002 RegX: 0000 RegB: 0000 SP: 0ba6 NZVC: 0000
Address: f opcode: d register: 0 mode: 0 operand: fffe
RegA: 0002 RegX: 0000 RegB: 0000 SP: 0ba4 NZVC: 0000
Address: 12 opcode: 1 register: 0 mode: 2 operand: 0008
RegA: 0002 RegX: 0000 RegB: 0000 SP: 0ba4 NZVC: 0000
Address: 15 opcode: 4 register: 0 mode: 0 operand: 0001
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0ba4 NZVC: 0000
Address: 18 opcode: 2 register: 0 mode: 2 operand: 0000
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0ba4 NZVC: 0000
Address: 1b opcode: 18 register: 0 mode: 0 operand: 0003
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0ba2 NZVC: 0000
Address: 3 opcode: d register: 0 mode: 0 operand: fffe
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0ba0 NZVC: 0000
Address: 6 opcode: 1 register: 0 mode: 2 operand: 0004
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0ba0 NZVC: 0000
Address: 9 opcode: 11 register: 0 mode: 0 operand: 0034
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0ba0 NZVC: 0000
Address: c opcode: d register: 0 mode: 0 operand: fffe
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0b9e NZVC: 0000
Address: f opcode: d register: 0 mode: 0 operand: fffe
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0b9c NZVC: 0000
Address: 12 opcode: 1 register: 0 mode: 2 operand: 0008
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0b9c NZVC: 0000
Address: 15 opcode: 4 register: 0 mode: 0 operand: 0001
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0b9c NZVC: 0100
Address: 18 opcode: 2 register: 0 mode: 2 operand: 0000
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0b9c NZVC: 0100
Address: 1b opcode: 18 register: 0 mode: 0 operand: 0003
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0b9a NZVC: 0100
Address: 3 opcode: d register: 0 mode: 0 operand: fffe
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0b98 NZVC: 0000
Address: 6 opcode: 1 register: 0 mode: 2 operand: 0004
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0b98 NZVC: 0100
Address: 9 opcode: 11 register: 0 mode: 0 operand: 0034
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0b98 NZVC: 0100
Address: 34 opcode: 1 register: 0 mode: 0 operand: 0000
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0b98 NZVC: 0100
Address: 37 opcode: 2 register: 0 mode: 2 operand: 0006
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0b98 NZVC: 0100
Address: 3a opcode: d register: 0 mode: 0 operand: 0002
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0b9a NZVC: 0000
Address: 3d opcode: 19 register: 0 mode: 0
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0b9c NZVC: 0000
Address: 1e opcode: d register: 0 mode: 0 operand: 0002
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0b9e NZVC: 0000
Address: 21 opcode: 1 register: 0 mode: 2 operand: 0000
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0b9e NZVC: 0100
Address: 24 opcode: d register: 0 mode: 0 operand: 0002
RegA: 0000 RegX: 0000 RegB: 0000 SP: 0ba0 NZVC: 0000
Address: 27 opcode: 3 register: 0 mode: 2 operand: 0004
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0ba0 NZVC: 0000
Address: 2a opcode: 2 register: 0 mode: 2 operand: 0000
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0ba0 NZVC: 0000
Address: 2d opcode: 2 register: 0 mode: 2 operand: 0006
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0ba0 NZVC: 0000
Address: 30 opcode: d register: 0 mode: 0 operand: 0002
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0ba2 NZVC: 0000
Address: 33 opcode: 19 register: 0 mode: 0
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0ba4 NZVC: 0000
Address: 1e opcode: d register: 0 mode: 0 operand: 0002
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0ba6 NZVC: 0000
Address: 21 opcode: 1 register: 0 mode: 2 operand: 0000
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0ba6 NZVC: 0000
Address: 24 opcode: d register: 0 mode: 0 operand: 0002
RegA: 0001 RegX: 0000 RegB: 0000 SP: 0ba8 NZVC: 0000
Address: 27 opcode: 3 register: 0 mode: 2 operand: 0004
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0ba8 NZVC: 0000
Address: 2a opcode: 2 register: 0 mode: 2 operand: 0000
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0ba8 NZVC: 0000
Address: 2d opcode: 2 register: 0 mode: 2 operand: 0006
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0ba8 NZVC: 0000
Address: 30 opcode: d register: 0 mode: 0 operand: 0002
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0baa NZVC: 0000
Address: 33 opcode: 19 register: 0 mode: 0
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0bac NZVC: 0000
Address: 1e opcode: d register: 0 mode: 0 operand: 0002
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0bae NZVC: 0000
Address: 21 opcode: 1 register: 0 mode: 2 operand: 0000
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0bae NZVC: 0000
Address: 24 opcode: d register: 0 mode: 0 operand: 0002
RegA: 0003 RegX: 0000 RegB: 0000 SP: 0bb0 NZVC: 0000
Address: 27 opcode: 3 register: 0 mode: 2 operand: 0004
RegA: 0006 RegX: 0000 RegB: 0000 SP: 0bb0 NZVC: 0000
Address: 2a opcode: 2 register: 0 mode: 2 operand: 0000
RegA: 0006 RegX: 0000 RegB: 0000 SP: 0bb0 NZVC: 0000
Address: 2d opcode: 2 register: 0 mode: 2 operand: 0006
RegA: 0006 RegX: 0000 RegB: 0000 SP: 0bb0 NZVC: 0000
Address: 30 opcode: d register: 0 mode: 0 operand: 0002
RegA: 0006 RegX: 0000 RegB: 0000 SP: 0bb2 NZVC: 0000
Address: 33 opcode: 19 register: 0 mode: 0
RegA: 0006 RegX: 0000 RegB: 0000 SP: 0bb4 NZVC: 0000
Address: 50 opcode: d register: 0 mode: 0 operand: 0002
RegA: 0006 RegX: 0000 RegB: 0000 SP: 0bb6 NZVC: 0000
Address: 53 opcode: 1e register: 0 mode: 2 operand: 0000
6
RegA: 0006 RegX: 0000 RegB: 0000 SP: 0bb6 NZVC: 0000
Address: 56 opcode: 0 register: 0 mode: 0
Halt
RegA: 0006 RegX: 0000 RegB: 0000 SP: 0bb6 NZVC: 0000
command : t
trace is now 0
command : r
start_address (hex): 0
? 10
55
Halt
command : q
$