Upload
helena
View
44
Download
0
Embed Size (px)
DESCRIPTION
Intro to Computer Org. MIPS Architecture & Assembly: More Instructions. MIPS Assembly Instructions. All MIPS instructions are one of these three types. R-type All the data is in registers I-type Only way to load from/store to memory Uses registers + hardcoded data J-type - PowerPoint PPT Presentation
Citation preview
Intro to Computer Org.
MIPS Architecture & Assembly: More Instructions
MIPS Assembly Instructions
All MIPS instructions are one of these three types.R-type
All the data is in registers I-type
Only way to load from/store to memoryUses registers + hardcoded data
J-typeUses only hardcoded data
MIPS Assembly – R-types
R-type instructions include:add – Additionsub – Subtractionand, or, xor, nor – Logical and, or,
exclusive or, norsll, srl – Shift Left/Right Logicalslt – Set Less Than jr – Jump Register
MIPS Assembly – R-types
slt – Set if less than If source 1 < source 2, dest = 1Else, dest = 0Very useful in combination with beq &
bne.
MIPS Assembly – R-types
jr – Jump Register Jumps to the address contained within
the specified register.Usual format: jr $ra
MIPS Assembly – I-types
I-type instructions include:addi – Additionandi, ori, xori – Logical and, or,
exclusive orslti – Set Less Thanbeq, bne – Branch If Equal/Not Equal lw, sw – Load Word/Store Word
MIPS Assembly – J-types
J-type instructions include: j – Jump jal – Jump and Link
MIPS Assembly – J-types
jal – Jump and LinkPerforms a regular jump to the
specified labelSaves the address of the instruction
after the jal in $ra
MIPS Assembly - Shortcuts
In addition to regular instructions, there are also pseudoinstructions that represent very common operations.
We’ll worry about using the real instructions for these basic operations later.
MIPS Assembly - Shortcuts
move $t1, $t0Copies contents of the source register
($t0) to the destination register ($t1)li $t1, <integer>
Loads a constant value directly into a register.
la $t1, labelLoads an address into a register
Basic MIPS Programming
To write a comment in MIPS, precede it with the sharp sign. (#)#This is a comment.
To make a label for branches and jumps…label: <instruction>
Basic MIPS Programming
Now that we have some of the basic MIPS instructions, how can we use them to write meaningful programs?
If – Then – Else
Let’s consider the following code:
if($t0 == $t1)$t2 = 4;
else $t2 = 5;
How can we make this in MIPS?
If – Then – Else
Let’s work this out on the blackboard.
If – Then – Else
if($t0 == $t1)$t2 = 4;
else $t2 = 5;
if: bne $t0, $t1, else
li $t2, 4j end
else: li $t2, 5end: #Whatever’s next.
If – Then – Else
With an if, we wish to execute the next line of code if the condition is true.
But with a beq or a bne, we execute the next line of code if the condition is false.Thus, we test the opposite of what the
original if tested.
For - Next
Let’s consider the following code:
int sum = 0;for(int i = 1; i <= 10; i++){
sum += i;}
For - Next
for(int i = 1; i <= 10; i++)int i = 1 //Initializationi <= 10 //Testi++ //Increment
For – Next
int sum = 0;for(int i = 1; i <= 10; i++)
{sum += i;
}
li $t1, 0 init: li $t0, 0li $t3, 10
test: slti $t2, $t3, $t0bne $t2, $0, end
body: add $t1, $t1, $t0
incr: addi $t0, $t0, 1j test
end: #Whatever’s next.
For – Next
While in that example we could have moved the test to the end, it is not always possible to do so.
for(int i=0; i < a.length; i++)
{//Body of loop
}
Using References/Pointers
array[5] += 4
Using References/Pointers
la $t0, arraylw $t1, 20($t0) # 20 = 5 *
4addi $t1, $t1, 4sw $t1, 20($t0)
Using References/Pointers
//Assume that value has offset 0//and next has offset 4.
struct Node {int value;Node* next;
}
Using References/Pointers
//C++ versionstruct Node {int value;Node* next;
}
//Java versionclass Node {public int value;public Node next;
}
Using References/Pointers
//Assume that value has byte offset 0//and next has byte offset 4.
struct Node {int value;Node* next;
}
Using References/Pointers
struct Node {int value;Node* next;
}
... //Assume a preexisting Node ‘root’.
Node* curNode = root;curNode = curNode->next;curNode->value = 5;
Using References/Pointers
la $t0, root #t0 = curNodelw $t0, 4($t0)li $t1, 5sw $t1, 0($t0)
Other Useful Instructions
mult $t0, $t1Performs integer multiplicationPlaces the result in special registers
called Hi and Lo.Multiplying two 32-bit integers can
potentially give a 64-bit result.
Other Useful Instructions
div $t0, $t1Performs integer divisionPlaces the result Lo and the remainder
in Hi. Integer division cannot give fractional
numbers.
Other Useful Instructions
mfhi $t0Moves contents of Hi into $t0
mflo $t0Moves contents of Lo into $t0
Other Useful Instructions
sllv, srlv $t0, $t1, $t2Shift left/right logical variableReplaces the hardcoded shift value
with one in a registerUses value of that register modulus 32.
Other Useful Instructions
Before continuing with other instructions, we need to examine the nature of the data we’re working with.Coming next: Integer and
floating-point representations!