03 - CPU Memory Program Execution Assembly - Exercise Sheet (Solutions) - 1504219614
03 - CPU Memory Program Execution Assembly - Exercise Sheet (Solutions) - 1504219614
Memory locations/addresses:
num1 = > 4040
num2 = > 5050
Total => 6060
ADD 6060, 4040, 5050
a. int num1= 5;
int num2 = 14;
int total = num1 + num2;
Q#2: Consider the below state of memory, where some values are already stored in different locations:
Value 50 70 14 9 7 44 20
Memory Address 2024 2028 2032 2036 2040 2044 2048 2052
What would change if these instructions were to be executed in the following sequence?
(a) XOR 2052, 2040, 2044 [14, Stored in 2052]
(b) ADD 2036, 2024, 2036 [64, Stored in 2036 and overwrites the 14]
(c) ADD 2028, 2024, 2048 [94, Stored in 2028]
(d) SUB 2040, 2048, 2044 [37, Stored in 2040 and overwrites the 9]
(e) MUL 2028, 2044, 2052 [98, Stored in 2028 and overwrites the 94]
When the instructions are executed in a different order, the resultant memory state will be different:
Value 50 98 70 64 37 7 44 14
Memory Address 2024 2028 2032 2036 2040 2044 2048 2052
Q#3: Using the figure below, describe the entire fetch / execute cycle detailing the result of each step in
the cycle for the following machine instruction: ADD 343, 546, 772
PC: 600
In practice, there are factors that mean that this theoretical maximum is never reached.
Q#5: What is the value stored in ‘z’ after the execution of the following MIPS assembly code, if x=5?
li $t0, 1
li $t1, 2
lw $t2, &x
L1: bgt $t1, $t2, L2
mul $t0, $t0, $t1
addi $t1, $t1, 1
j L1
L2: sw $t0, &z
The bgt (branch on greater than) instruction compares two registers and jumps to L2 if the content of
register $t1 is greater than the content of register $t2. The equivalent Java code is a for loop:
z = 1;
for (i = 2; i <= x; i++){
z = z * i;
}
Knowing that x = 5 and the index i starts from 2, the result will be:
z = 1 * 2 * 3 * 4 * 5 = 120.
Q#7: Given the MIPS Instruction set, convert the following MIPS assembly code into
Java/Python-like code. Consider the register assignment proposed at the start of the code.
You may like to refer to the MIPS Instructions cheatsheet available at:
https://jarrettbillingsley.github.io/teaching/classes/cs0447/guides/
instructions.html
sum = 0;
for (int i = 0; i < 15; i++){
sum = sum + i * 2;
}