Solutions Ch 2

advertisement
CPSC 330
Fall 1999
HW #1
Assigned September 1, 1999
Due September 8, 1999
Submit in class
Use a word processor (although you
may hand-draw answers to Problems
24 and 25)
Chapter 2, Heuring and Jordan,
Prob: 2, 3, 4, 5, 6, 9, 14, 23, 24, 25
2.3
T is a memory location used as a temporary
3-address
2-address
1-address
0-address
ADD A,B,C
LOAD A,B
LDA
D
PUSH
D
ADD T,D,E
ADD
A,C
ADD
E
PUSH
E
MPY A,A,T
LOAD T,D
STA
T
ADD
ADD
T,E
LDA
B
PUSH
C
MPY
A,T
ADD
C
PUSH
B
MPY
T
ADD
STA
A
MPY
POP
A
2.4 Assume that operands and results are stored in memory
addresses can be accessed with direct addressing. Also assume
that SRC has a multiply instruction, mpy, that uses format 6.
ld
r0, B
ld
r1, C
add r0, r0, r1
ld
r1, D
ld
r1, D
ld
r2, E
add r1, r1, r2
mpy r0, r0, r1
st
r0, A
2.5
Assume each instruction fetch requires one memory access
Machine
Instructions
3-address
3
2-address
5
1-address
7
0-address
8
SRC (gen reg)
8
*ADD A, C
Opnds/Results
9
13*
7
5
5
Total
12
18
14
13
13
takes 3 data accesses:
2 to fetch A and C and one to store A
2.6.
3-address: Each instruction has one 8-bit opcode field and
three 24-bit memory address fields, so one instruction
needs 10 bytes. Three instructions = 30 bytes.
2-address: Each instruction has one 8-bit opcode field and
two 24-bit memory address fields. One instruction needs 7
bytes, so five require 35 bytes.
1-address: Each instruction has one 8-bit opcode field and
one 24-bit memory address field. One instruction needs 4
bytes, so eight require 32 bytes.
0-address: One arithmetic instruction only requires one
8-bit opcode field. One PUSH/POP instruction requires one
8-bit and one 24-bit memory address field, so three
arithmetic instructions require 3 bytes and five PUSH/POP
instructions require 20 bytes. Total 23 bytes.
SRC: One load/store instruction requires 8-bit opcode
field, one 5-bit register number field, and one 24-bit
memory address field, so one load/store instruction
requires 5 bytes. One arithmetic instruction requires
one 8-bit opcode field and three 5-bit register number
fields, so one arithmetic instruction requires 3 bytes.
Therefore. five load/store instructions and three
arithmetic instructions require 5 * 5 + 3 * 3 = 34 bytes.
2.9
In a stack machine, an arithmetic instruction only needs an
opcode field, while a PUSH/POP instruction needs both
opcode and a much longer address field. If all
instructions were forced to be the same length, a
considerable amount of memory space would be wasted in
arithmetic instructions.
2.14
a)
ld
la
lar
brzr
neg
plus: add
st
r0,
r1,
r2,
r2,
r1,
r0,
r0,
a
1
plus
r0
r1
r0, r1
a
;
;
;
;
;
;
;
Get value of a
Gt constant +1
Set branch target
Skip next if a=0
Convert constant to -1
Form a+1 or a-1
Update a
b) la
la
lar
addi
loop: st
addi
addi
addi
brmi
r4,
r3,
r2,
r1,
r4,
r3,
r1,
r0,
r2,
0
ndigit
loop
r4, 0
0(r3)
r3, 4
r1, 1
r1,-10
r0
;
;
;
;
;
;
;
;
;
Constant 0
R[3] points to ndigit[i]
Branch target
Make R[1], =i, 0
ndigit[i] = 0
Advance array pointer
i++
R[0]<0 iff i<10
Repeat if i<10
2.23
Register Transfer
Control signals
------------------
-------------------
a) W <- R[6]+1
R[6]out, Win
Y <- W
Wout, Yin
Z <- R[4] + Y
R[4]out, Zin
R[3] <- Z
Zout, R[3]in
b) Y <- R[5]
R[5]out, Yin
Z <- R[4] + Y
R[4], Zin
Y <- Z
Zout, Yin
Z <- R[3] + Y
R[3]out, Zin
R[2] <- Z
Zout, R[2]in
2.24
On whiteboard
2.25
On whiteboard
Download