Class Notes (902,779)
CA (537,654)
UTSG (45,694)
CSC (765)
CSC258H1 (46)
Lecture

mar20ce.docx

2 Pages
113 Views

Department
Computer Science
Course Code
CSC258H1
Professor
Steve Engels

This preview shows half of the first page. Sign up to view the full 2 pages of the document.
MIPS Instructions cont.
o Shift instructions
1st character: "s" = shift
2nd character: "l" or "r" = shift direction
3rd character: "a" or "l" = arthematic or logical
shift left logical == shift left arthematic (not in use)
4th (optional) character: "v" = a variable number of bits,
specified by $s
Instruction
Opcode/
Function
Synatx
Operation
sll
000000
$d, $s, a
$d = $t << a
sllv
000100
$d, $t, $s
$d = $t << $s
sra
000011
$d, $t, a
$d = $t >> a
srav
000111
$d, $t, $s
$d = $t >> $s
srl
000010
$d, $t, a
$d = $t >>> a
srlv
000110
$d, $t, $s
$d = $t >>> $s
Data movement instructions
o These instructions for operating on the HI and LO registers
Instruction
Opcode/
Function
Operation
mfhi
010000
$d
$d = hi
mflo
010010
$d
$t = lo
mthi
010001
$s
hi = $s
mtlo
010011
$s
lo = $s
ALU instructions
o Note that for ALU instructions, most are R-type instructions
The six-digit codes in the table are therefore the function codes
(opcodes are 000000)
Exceptions are the I-type instructions (addi, andi, orri, etc.)
o Note all R-type instructions have an I-type equivalent
RISC architecures dictate that an operation doesn’t need an
instruction if it can be performed through multiple existing
operations
Ex. addi + div divi
Store the immediate value into a register first than divide
Example program
o A fibonacci sequence C code into assembly code
1. int fib(void) {
2. int n = 10;
3. int f1 = 1, f2 = -1;
4. while (n != 0) {
5. f1 = f1 + f2;
6. f2 = f1 f2;
7. n = n 1;
8. }
9. return f1;
10. }
o In assembly
Note: beq, j, and sb instructions will be touched upon later
1. # fib.asm
2. # register usage: $t3=n, $t4=f1, $t5=f2
3. # RES refer to memory address of result
4. FIB: addi $t3, $zero, 10 # initialize n=10
5. addi $t4, $zero, 1 # initialize f1=1
6. addi $t5, $zero, -1 # initialize f2=-1
7. LOOP: beq $t3, $zero, END # done loop if n==0
8. add $t4, $t4, $t5 # f1 = f1 + f2
9. sub $t5, $t4, $t5 # f2 = f1 - f2
10. addi $t3, $t3, -1 # n = n 1
11. j LOOP # repeat until done
12. END: sb $t4, RES # store result
o Making an asssmbly program
Assembly Lanauge programs typically have structure similar to
simple Python or C programs
Set aside registers to store data
Have sections of instructions that manipulate this data
Its always good to decide at the beginning which registers will
be used for what purpose
Control flow in assembly
o Not all programs follow a linear set of instructions
Some operations require the code to branch to one section of
the code or another (if/else)
Some require the code to jump back and repeat a section of
code again (for/while)
o For this, we have labels on the left-hand side that indicate the points
that the program flow might need to jump to
References to these points in the assembly code are resolved at
compile time to offset values for the program counter
o Branch instructions
Branch operations are key when implementing if statements
and while loops
The labels are memory locations, assigned to each lable at
compile time
Note: i is calculated as
(label (current PC + 4)) >> 2
In branch i-type instruction last 16 bits is the difference btwn
the branch-call location and the branch-to location
beq = branch if equals
bgtz = branch if greater than
blez = branch if lesser than or equals to
bne = branch if not equals
Note: branch if lesser than DNE, instead: beq, bgtz & blez
Instruction
Opcode/Fun
ction
Synatx
Operation
beq
000100
$s, $t, label
if ($s == $t)
pc += i << 2
bgtz
000111
$s, label
if ($s > 0)
pc += i << 2
blez)
000110
$s, label
if ($s <= 0)
pc += i << 2
bne
000101
$s, $t, label
if ($s != $t)
pc += i << 2
o Jump instructions
j = “jump”, for calling subroutines
similar to GOTO statements
if an assembly program doesn’t have jump statements,
the program will run from beginning to end sequentially
jal = “jump and link”, for calling functions
register $ra ($31) stores the address that’s used when
returning from a subroutine
jalr = “jump and link to register”
jr = “jump to register”, jump to address contain in register $s
usually for ending a subroutine by jumping to the return
address stored in $ra
note: jr and jalr are not j-type instructions
Instruction
Opcode/
Function
Synatx
Operation
j
000010
label
pc += i << 2
jal
000011
label
$ra = pc; pc += i << 2
jalr
001001
$s
$31 = pc; pc = $s
jr
001000
$s
pc = $s
o Comparison instructions
Comparison operation stores a:
1 in the dst register if the less-than comparison is true
0 in the dst register otherwise
Instruction
Opcode/
Function
Synatx
Operation
slt
101010
$d, $s, $t
$d = ($s < $t)
sltu
101001
$d, $s, $t
$d = ($s < $t)
slti
001010
$t, $s, i
$t = ($s < SE(i))
sltiu
001001
$t, $s, i
$t = ($s < SE(i))

Loved by over 2.2 million students

Over 90% improved by at least one letter grade.

Leah — University of Toronto

OneClass has been such a huge help in my studies at UofT especially since I am a transfer student. OneClass is the study buddy I never had before and definitely gives me the extra push to get from a B to an A!

Leah — University of Toronto
Saarim — University of Michigan

Balancing social life With academics can be difficult, that is why I'm so glad that OneClass is out there where I can find the top notes for all of my classes. Now I can be the all-star student I want to be.

Saarim — University of Michigan
Jenna — University of Wisconsin

As a college student living on a college budget, I love how easy it is to earn gift cards just by submitting my notes.

Jenna — University of Wisconsin
Anne — University of California

OneClass has allowed me to catch up with my most difficult course! #lifesaver

Anne — University of California
Description
 MIPS Instructions cont.  Control flow in assembly o Shift instructions o Not all programs follow a linear set of instructions st  1 ndaracter: "s" = shift  Some operations require the code to branch to one section of  2 character: "l" or "r" = shift direction the code or another (if/else)  3 character: "a" or "l" = arthematic or logical  Some require the code to jump back and repeat a section of  shift left logical == shift left arthematic (not in use) code again (for/while) th  4 (optional) character: "v" = a variable number of bits, o For this, we have labels on the left-hand side that indicate the points specified by $s that the program flow might need to jump to Instruction Opcode/ Synatx Operation  References to these points in the assembly code are resolved at compile time to offset values for the program counter Function sll 000000 $d, $s, a $d = $t << a o Branch instructions sllv 000100 $d, $t, $s $d = $t << $s  Branch operations are key when implementing if statements and while loops sra 000011 $d, $t, a $d = $t >> a  The labels are memory locations, assigned to each lable at srav 000111 $d, $t, $s $d = $t >> $s compile time srl 000010 $d, $t, a $d = $t >>> a  Note: i is calculated as srlv 000110 $d, $t, $s $d = $t >>> $s (label – (current PC + 4)) >> 2  In branch i-type instruction last 16 bits is the difference btwn  Data movement instructions the branch-call location and the branch-to location o These instructions for operating on the HI and LO registers  beq = branch if equals Instruction Opcode/ Synatx Operation  bgtz = branch if greater than Function  blez = branch if lesser than or equals to mfhi 010000 $d $d = hi  bne = branch if not equals mflo 010010 $d $t = lo  Note: branch if lesser than DNE, instead: beq, bgtz & blez mthi 010001 $s hi = $s Instruction Opcode/Fun Synatx Operation mtlo 010011 $s lo = $s ction beq 000100 $s, $t, label if ($s == $t)  ALU instructions pc += i << 2 o Note that for ALU instructions, most are R-type instructions  The six-digit codes in the table are therefore the function codes 000111 $s, label if ($s > 0) pc += i << 2 (opcodes are 000000)  Exceptions are the I-type instructions (addi, andi, orri, etc.)z) 000110 $s, label if ($s <= 0) o Note all R-type instructions have an I-type equivalent pc += i << 2  RISC architecures dictate that an operation doesn’t need an bne 000101 $s, $t, label if ($s != $t) instruction if it can be performed through multiple existing pc += i << 2 operations  Ex. addi + div  divi o Jump instructions  j = “jump”, for calling subroutines  Store the immediate value into a register first than divide  similar to GOTO statements  Example program o A fibonacci sequence C code into assembly code  if an assembly program doesn’t have jump statements, 1. int fib(void) { the program will run from beginning to end sequentially 2. int n = 10;  jal = “jump and link”, for calling functions 3. int f1 = 1, f2 = -1; 4. while (n != 0) {  register $ra ($31) stores the address that’s used when 5. f1 = f1 + f2; returning from a subroutine 6. f2 = f1 – f2;  jalr = “jump and link to register” 7. n = n – 1;  jr = “jump to register”, jump to address contain in register $s 8. } 9. return f1;  usually for ending a subroutine by jumping to the return 10. } address stored in $ra
More Less
Unlock Document


Only half of the first page are available for preview. Some parts have been intentionally blurred.

Unlock Document
You're Reading a Preview

Unlock to view full version

Unlock Document

Log In


OR

Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit