330 likes | 529 Views
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
E N D
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 • Uses only hardcoded data
MIPS Assembly – R-types • R-type instructions include: • add – Addition • sub – Subtraction • and, or, xor, nor – Logical and, or, exclusive or, nor • sll, srl – Shift Left/Right Logical • slt – Set Less Than • jr – Jump Register
MIPS Assembly – R-types • slt – Set if less than • If source 1 < source 2, dest = 1 • Else, dest = 0 • Very 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 – Addition • andi, ori, xori – Logical and, or, exclusive or • slti – Set Less Than • beq, 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 Link • Performs a regular jump to the specified label • Saves 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, $t0 • Copies contents of the source register ($t0) to the destination register ($t1) • li $t1, <integer> • Loads a constant value directly into a register. • la $t1, label • Loads 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($t0 == $t1) $t2 = 4; else $t2 = 5; if: bne $t0, $t1, else li $t2, 4 j end else: li $t2, 5 end: #Whatever’s next. If – Then – Else
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 //Initialization • i <= 10 //Test • i++ //Increment
int sum = 0; for(int i = 1; i <= 10; i++) { sum += i; } li $t1, 0 init: li $t0, 0 li $t3, 10 test: slti $t2, $t3, $t0 bne $t2, $0, end body: add $t1, $t1, $t0 incr: addi $t0, $t0, 1 j test end: #Whatever’s next. For – 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, array lw $t1, 20($t0) # 20 = 5 * 4 addi $t1, $t1, 4 sw $t1, 20($t0)
Using References/Pointers //Assume that value has offset 0 //and next has offset 4. struct Node { int value; Node* next; }
//C++ version struct Node { int value; Node* next; } //Java version class Node { public int value; public Node next; } Using References/Pointers
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 = curNode lw $t0, 4($t0) li $t1, 5 sw $t1, 0($t0)
Other Useful Instructions • mult $t0, $t1 • Performs integer multiplication • Places 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, $t1 • Performs integer division • Places the result Lo and the remainder in Hi. • Integer division cannot give fractional numbers.
Other Useful Instructions • mfhi $t0 • Moves contents of Hi into $t0 • mflo $t0 • Moves contents of Lo into $t0
Other Useful Instructions • sllv, srlv $t0, $t1, $t2 • Shift left/right logical variable • Replaces the hardcoded shift value with one in a register • Uses 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!