Tutorial 2
Tutorial 2
Tutorial 2
23.01.2024
24.01.2024
29.01.2024
Contents
• RISC Programming
• Pipelining
• Booth’s Algorithm
• Restoring Division
• Self study: Representation of Negative Numbers,
Sign Extension, Computer Arithmetic
1/28/2023 2
Problem 1
Consider the load/store architecture, where only load and store instructions move data between the
registers and memory. The following table gives some sample instructions for a load/store machine. Using
the instructions, write a code that will emulate the following expression. Note you have only two size-
independent registers, R1 and R2, available in the machine.
Expression: A = B + C * D – E + F + A
Solution
Problem 2
Now consider each load and store instruction takes two memory accesses: one to fetch the instruction
and the other to access the data value. The arithmetic instructions need only one memory access to fetch
the instruction as the operands are in registers. How many memory access will be required to compute the
code in Problem 1?
Solution
Store 1 2 2
Add 3 1 3
Sub 1 1 1
Mult 1 1 1
Grand Total 19
Problem 3
For R-type instructions, pipelining will require only 4 stages: IF, ID, EX, and WB (MEM is not
required). What will happen if we try to pipeline loads of the following program? Note that each
functional unit can only be used once per instruction. In case of a clash, how to solve it?
Solution 3
All arithmetic or logical instructions are R-type instructions since it will operate only on registers.
Given the pipelining data, we can fill the clock cycles using the pipelining stages as:
Clock cycle
1 2 3 4 5 6 7 8 9
add $sp, $sp, IF ID EX WB
-4
sub $v0, IF ID EX WB
$a0, $a1
lw $t0, IF ID EX ME WB
4($sp) M
or $s0, $s1, IF ID EX WB
$s2
lw $t1, IF ID EX MEM WB
8($sp)
R-type IF ID EX NOP WB
Clock cycle
1 2 3 4 5 6 7 8 9
add $sp, $sp, IF ID EX NOP WB
-4
sub $v0, IF ID EX NOP WB
$a0, $a1
lw $t0, IF ID EX ME WB
4($sp) M
or $s0, $s1, IF ID EX NOP WB
$s2
lw $t1, IF ID EX MEM WB
8($sp)
Note: Store and branch instructions also have NOP stages too…
Machine Implementation
Pen and Pencil Method
12
Practice Problems
• -5 7
• -5 -8
• 39 6