In: Computer Science
Consider the following fragment of C code:
for (i=0; i<100; i++) {
A[i]=B[i]+C;
}
Assume that A and B are arrays of 64-bit integers, and C and i are 64-bit integers.
Assume that all data values and their addresses are kept in memory (at
addresses 1000, 3000, 5000, and 7000 for A, B, C, and i, respectively) except when
they are operated on. Assume that values in registers are lost between iterations of
the loop. Assume all addresses and words are 64 bits. (Write the code in MIPS ) c. [20]<A.2>Write the code for a stack machine. Assume all operations occur
on top of the stack. Push and pop are the only instructions that access memory;
all others remove their operands from the stack and replace them with the result.
The implementation uses a hardwired stack for only the top two stack entries,
which keeps the processor circuit very small and low in cost. Additional stack
positions are kept in memory locations, and accesses to these stack positions
require memory references. How many instructions are required dynamically?
How many memory-data references will be executed?
DADD R1, R0, R0 ; R0 =0; initialize i = 0
SW R1, 7000(R0) ; store i
Loop: LD R1, 7000(R0) ; get value of i
DSLL R2, R1, #3 ; R2 = word offset of B[i]
DADDI R3, R2, #3000 ; add base address of B to R2
LD R4, 0(R3) ; load B[i]
LD R5, 5000(R0) ; load C
DADD R6, R4, R5 ; B[i] + C
LD R1, 7000(R0) ; get value of i
DSLL R2, R1, #3 ; R2 = word offset of A[i]
DADDI R7, R2, #1000 ; add base address of A to R2
SD R6, 1000(R7) ; A[i] = B[i] + C
LD R1, 7000(R0) ; get value of i
DADDI R1, R1, #1 ; increment i
SD R1, 7000(R0) ; store i
LD R1, 7000(R0) ; get value of i
DADDI R8, R1, #-101 ; is counter at 101?
BNEZ R8, loop ; if not 101 then repeat
-------------------------------------
b)Dynamic instructions are executed = initialization instructions plus loop instructions times the number of iterations
= 2 + (16 X 101) = 1618.
------------------------------------
c) Memory data reference is a count of load-store instructions = 0 + (8 X 101) = 808.