Chapter 2 Exercise Problems
Chapter 2 Exercise Problems
9(HW))
Machine Instructions and Programs
(b) To subtract the second number, form its 2’s-complement and add it to
the first number.
(a) 00101 (b) 00111 (c) 10010
+ 10110 + 10011 + 10101
——— ——— ———
11011 11010 00111
no overflow no overflow overflow
2.6 Byte contents in hex, starting at location 1000, will be 4A, 6F, 68, 6E, 73, 6F,
6E. The two words at 1000 and 1004 will be 6E686F4A and XX6E6F73.Byte
1007 (shown as XX) is unchanged. (See section 2.6.3 for hex notation.)
2.7 Clear the high-order 4 bits of each byte to 0000.
Load A
Multiply B
Store RESULT
Load C
Multiply D
Add RESULT
Store RESULT
2.9 Memory word location J contains the number of tests, j, and memory word
location N contains the number of students, n. The list of student marks
begins at memory word location LIST in the format shown in Figure 2.14. The
parameter Stride = 4(j + 1) is the distance in bytes between scores on a
particular test for adjacent students in the list.
The Base with index addressing mode (R1,R2) is used to access the scores
on a particular test. Register R1 points to the test score for student 1, and
R2 is incremented by Stride in the inner loop to access scores on the same
test by successive students in the list.
Memory
accesses
————
Move #AVEC,R1 1
Move #BVEC,R2 1
Load N,R3 2
Clear R0 1
LOOP Load (R1)+,R4 2
Load (R2)+,R5 2
Multiply R4,R5 1
Add R5,R0 1
Decrement R3 1
Branch>0 LOOP 1
Store R0,DOTPROD 2
(b) k1 = 1 + 1 + 2 + 1 + 2 = 7; and k2 = 2 + 2 + 1 + 1 + 1 + 1 = 8
2.11. (a) The original program in Figure 2.33 is efficient on this task.
(b) k1 = 7; and k2 = 7
This is only better than the program in Problem 2.10(a) by a small
amount.
2.12. The dot product program in Figure 2.33 uses five registers. Instead of using
R0 to accumulate the sum, the sum can be accumulated directly into
DOTPROD. This means that the last Move instruction in the program can be
removed, but DOTPROD is read and written on each pass through the loop,
significantly increasing memory accesses. The four registers R1, R2, R3, and
R4, are still needed to make this program efficient, and they areall used
in the loop. Suppose that R1 and R2 are retained as pointers tothe A and B
vectors. Counter register R3 and temporary storage register R4 could be
replaced by memory locations in a 2-register machine; but the number of
memory accesses would increase significantly.
Move #1000,R0
Clear R1
Clear R2
Clear R3
LOOP Add 8(R0),R1
Add 12(R0),R2
Add 16(R0),R3
Move 4(R0),R0
Branch>0 LOOP
Move R1,SUM1
Move R2,SUM2
Move R3,SUM3
2.15. Assume that the subroutine can change the contents of any register used
to pass parameters.
Subroutine
2.17. (a)
Move (R5)+,R0
Add (R5)+,R0
Move R0,−(R5)
(b)
Move 16(R5),R3