32
Intro to Computer Org. MIPS Architecture & Assembly: More Instructions

Intro to Computer Org

  • 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

Page 1: Intro to Computer Org

Intro to Computer Org.

MIPS Architecture & Assembly: More Instructions

Page 2: Intro to Computer Org

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

Page 3: Intro to Computer Org

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

Page 4: Intro to Computer Org

MIPS Assembly – R-types

slt – Set if less than If source 1 < source 2, dest = 1Else, dest = 0Very useful in combination with beq &

bne.

Page 5: Intro to Computer Org

MIPS Assembly – R-types

jr – Jump Register Jumps to the address contained within

the specified register.Usual format: jr $ra

Page 6: Intro to Computer Org

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

Page 7: Intro to Computer Org

MIPS Assembly – J-types

J-type instructions include: j – Jump jal – Jump and Link

Page 8: Intro to Computer Org

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

Page 9: Intro to Computer Org

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.

Page 10: Intro to Computer Org

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

Page 11: Intro to Computer Org

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>

Page 12: Intro to Computer Org

Basic MIPS Programming

Now that we have some of the basic MIPS instructions, how can we use them to write meaningful programs?

Page 13: Intro to Computer Org

If – Then – Else

Let’s consider the following code:

if($t0 == $t1)$t2 = 4;

else $t2 = 5;

How can we make this in MIPS?

Page 14: Intro to Computer Org

If – Then – Else

Let’s work this out on the blackboard.

Page 15: Intro to Computer Org

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.

Page 16: Intro to Computer Org

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.

Page 17: Intro to Computer Org

For - Next

Let’s consider the following code:

int sum = 0;for(int i = 1; i <= 10; i++){

sum += i;}

Page 18: Intro to Computer Org

For - Next

for(int i = 1; i <= 10; i++)int i = 1 //Initializationi <= 10 //Testi++ //Increment

Page 19: Intro to Computer Org

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.

Page 20: Intro to Computer Org

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

}

Page 21: Intro to Computer Org

Using References/Pointers

array[5] += 4

Page 22: Intro to Computer Org

Using References/Pointers

la $t0, arraylw $t1, 20($t0) # 20 = 5 *

4addi $t1, $t1, 4sw $t1, 20($t0)

Page 23: Intro to Computer Org

Using References/Pointers

//Assume that value has offset 0//and next has offset 4.

struct Node {int value;Node* next;

}

Page 24: Intro to Computer Org

Using References/Pointers

//C++ versionstruct Node {int value;Node* next;

}

//Java versionclass Node {public int value;public Node next;

}

Page 25: Intro to Computer Org

Using References/Pointers

//Assume that value has byte offset 0//and next has byte offset 4.

struct Node {int value;Node* next;

}

Page 26: Intro to Computer Org

Using References/Pointers

struct Node {int value;Node* next;

}

... //Assume a preexisting Node ‘root’.

Node* curNode = root;curNode = curNode->next;curNode->value = 5;

Page 27: Intro to Computer Org

Using References/Pointers

la $t0, root #t0 = curNodelw $t0, 4($t0)li $t1, 5sw $t1, 0($t0)

Page 28: Intro to Computer Org

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.

Page 29: Intro to Computer Org

Other Useful Instructions

div $t0, $t1Performs integer divisionPlaces the result Lo and the remainder

in Hi. Integer division cannot give fractional

numbers.

Page 30: Intro to Computer Org

Other Useful Instructions

mfhi $t0Moves contents of Hi into $t0

mflo $t0Moves contents of Lo into $t0

Page 31: Intro to Computer Org

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.

Page 32: Intro to Computer Org

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!