Skip to content

Instantly share code, notes, and snippets.

@davepagurek
Created October 29, 2015 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davepagurek/61edf63beea1068db30b to your computer and use it in GitHub Desktop.
Save davepagurek/61edf63beea1068db30b to your computer and use it in GitHub Desktop.
CS tutorial
1. initially all registers are set to 0x0 except
$1 = 3
$2 = 1
$31 = something
what registers have changed (from beginning to end)
DIV(2,1) ; sets lo,hi = (1/3),1%3 = 0,1
ADD(0,1,1) ; $0 = $1 + $1, but this doesn't do anything ($0 stays 0)
SUB(3,1,2) ; $3 = $1 - $2, so $3 = 2
MFHI(3) ; $3 = 1
ADD(1,1,1) ; $1 = 3+3 = 6
SUB(1,1,1) ; $1 = 6-6 = 0
SLT(2,1,3) ; $2 = (0 < 1) = 1
at end,
$1 = 0
$2 = 1
$3 = 0
changed: HI, $1, $3, PC
2. Implement the function 'bitshift' using MIPS (including labels).
If $1 contains n, then at the end, $3 should contain 2^n.
If 2^n doesn't fit into 32 bits, but fits into 64, put the 32 most significant in $2.
ex. if $1 = 35 --- $2 = 0001 0000, $3 = 0
LIS(4)
Word(32)
LIS(10)
Word(2)
SLT(5,4,1) ; $5 = (32 < $1). if 5 = 1 then just play with $3, else go to $2
BNE(0,5,start) ; don't branch if i <= 32
SUB(1,1,4)
JR(31)
Define(start) ; if i > 32
3. Suppose we want register allocation to evaluate the following expression.
How many registers do we need?
(((a+b) * (c-d)) + (a * ((c-d) + (a+b))))
t1 = a+b
t1
t2 = c-d
t1,t2
t3 = t1*t2
t1,t2,t3
t4 = t1+t2
t3,t4
t5 = a*t4
t3,t5
e = t3+t5
4. We want to implement switch-case into our language.
Switch_Case(x: Int) =>
case(1) => do something
case(2) => do something else
case(4) => another thing
Switch_Case(val: Int, cases: Seq[Int], thens: Seq[Code], else: Code, labels: Seq[Label]) =
block(
cases.zip(thens).zip(labels).map { case (case,then,label) => block(
LIS(3),
Word(encodeUnsigned(val)),
LIS(4),
Word(encodeUnsigned(case)),
BNE(10,11,next),
then,
Define(label)
)},
else
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment