Homework 2
Prof. Milos Prvulovic
CS 4290/6290 - Spring 2009
Handed Out: 3 Feb 2009 (Tue)
Due: 10 Feb 2009 (Tue)
This homework counts as 5% of the overall class grade. Submit your answers in T-Square as a
single PDF document. You can either hand-write your answers and scan them into a PDF
document, or write your answers in a text processor (e.g. LaTeX) and generate a PDF document
from that (e.g. pdflatex). No printed submissions will be accepted. In case you hand-write your
answers, keep in mind that illegible answers will be considered to be wrong answers.
1.
Consider the following MIPS code. Note that R0 (register 0) in the MIPS ISA always has
a value of zero. Also note that MIPS instructions always start at word-aligned addresses,
so the two least significant bits of the instruction’s address are always zero. As a result,
the two least significant bits are never used to index branch predictor tables. Instead,
these bits are ignored and the index for the predictor consists of the least significant
remaining bits when indexing into a branch predictor table.
0x100
li
R2,0
; v=0
0x104
li
R3,100
; Loop bound for LoopI
0x108
li
R4,0
; i=0
LoopI:
0x10C
beq
R4,R3,EndLoopI ; Exit LoopI if i==100
0x110
li
R5,0
; j=0
LoopJ:
0x114
beq
R5,R3,EndLoopJ ; Exit LoopJ if j==100
0x118
add
R6,R5,R4
; j+i
0x11C
andi R6,R6,1
; (j+i)%2
0x120
bne
R6,R0,EndIf
; Skip if (j+1)%2!=0
0x124
add R2,R2,R5
; v+=j
EndIf:
0x128
addi R5,R5,1
; j++
0x12C
beq
R0,R0,LoopJ
; Go back to LoopJ
EndLoopJ:
0x130
addi R4,R4,1
; i++
0x134
beq
R0,R0,LoopI
; Go back to LoopI
EndLoopI:
For this code, determine the
exact
number of branch mispredictions that occur during its
execution (show your work), if we use:
a)
An always-taken predictor.
This code can be written in C as follows: