41
IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Embed Size (px)

Citation preview

Page 1: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

IT253: Computer Organization

Lecture 5:Assembly Language and an Introduction

to MIPS

Tonga Institute of Higher Education

Page 2: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Computer Organization

• CPU contains three components:• Registers — Hold data values for CPU• ALU — Arithmetic Logic Unit; performs arithmetic and logic

functions. Takes values from and returns values to the registers• Control — Determines what operation to perform, directs data

flow to/from memory, directs data flow between registers and ALU. Actions are determined by the current Instruction.

Page 3: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Where a program lives

Page 4: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

How are programs executed?

• Individual instructions (like add $r1,$r2) are taken from memory (ex. the RAM).

• These instructions are decoded and they tell the processor what to do.

• The operands (the variables) are then taken from memory and the operation is performed (executed).

• The result of the operation is stored • The next instruction starts

everything over again.

Page 5: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

What does the processor need to know?

• We have to create our instructions and operands (variables) so that the computer hardware can use them– We need to specify the

• Instruction format = the format of the binary machine language

• Where to find operands (variables) in memory

• How many operands (variables) will we use• What operations are supported• Successor instructions (how do we get to

another instruction or the next instruction)

Page 6: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Categories of MIPS Instructions

• Arithmetic – add, subtract, multiply, divide…

• Logical– Like boolean logic, AND, OR, NOR…

• Data transfer– Store and load things from memory

• Branching – if – else statements

• Jumping – When we program functions, we need to

“jump” out of the current instruction set

Page 7: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

MIPS Architecture

• MIPS uses a three address format for instructions

• This means, for most instructions, you need to specify three registers, or at least two registers and a constant number

• Examples– add $1, $2, $3 # $1 = $2 + $3– addi $1, $1, 4 # $1 = $1 + 4

Page 8: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

MIPS Registers• Registers are where the CPU saves the

data it is working on in the present• This memory is very fast because it is

built into the processor.• On MIPS machines, the registers are all

32 bits.– They can hold integers that use up to 32

bits (2^32) – They can also store addresses (pointers) to

places in memory– Since there are 32 bits, MIPS can access

2^32 places in memory

Page 9: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

MIPS Registers• The registers you will use are called

General Registers• There are 31 of them available in MIPS

(R0 always equals 0, so you can’t change that one). You can use R1-R31.

• There are also 32 frame pointers. These are used to keep track of where you have saved data in the memory.

• There are also a few floating point registers which allow you to store and manipulate floating point numbers

Page 10: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

MIPS Registers

• There are also special registers – The PC register – The program

counter. This holds the address of the next instruction that will be executed.

– The GP register – global pointer. Contains the address of where the program starts

Page 11: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Registers

Page 12: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

MIPS Instructions• MIPS math functions are the simplest to

use and understand– They use three operands (registers)– Math is done in the registers. – Registers must be loaded with the right

values– The result of a math function is saved in the

first operand• Example

– C++/Java code int x = y + z;

– MIPS code add $s0,$s1,$s2 # s0 = x, s1 = y, s2 = z

# s0 = s1 + s2

Page 13: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

MIPS Adding

• If we only have three operands, how can we do the following– int a = w + x + y + z;

• We have to store in temporary valuesadd $t0,$s0,$s1 # t0 = w + x;add $t1,$s2,$s3 # t1 = y + z;add $s4,$t0,$t1 # a = t0 + t1

• These commands work the same for "sub" as well.

Page 14: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Different MIPS Adding

• The "add" command in MIPS can only take 3 registers at a time:– Example: add $s0,$t0,$t1

• To add a constant number with a register there is a different command called "addi" or add immediate

• In MIPS, "immediate" means a constant number• Example:

– JAVA: int x = 7 + y;– MIPS: addi $s0,$s1,7 # s0 = x, s1 = y

• If you must add a constant number, you must use "addi"

Page 15: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Add-Immediate

• Sometimes, you might want to use only one register and two immediates, like– int x = 5 + 7;

• There is no command in MIPS that allows this in one step.

• Instead you must break this line into two and add the first number to the zero register– addi $t0,$zero,5– addi $s0,$t0,7

Page 16: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Available Registers

• What registers are you allowed to use?– You can use any registers you want, even

special ones to save your data, but you should only use the s0-s7 and t0-t9 registers

– The "s" registers are supposed to be for data you want to save for a longer time (like a variable)

– The "t" registers are "temporary" which means that the data is not important and you can throw it away immediately if you want to.

Page 17: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Loading and Storing• Registers need to be able to save items in

memory and load them from memory. • There are a few MIPS commands to help you

– la – load address. Put an address into a register– lw – load word – put a whole word into a register– sw – store word – take a word from a register and

save in memory• Load Address – this command will put an

address location into a register. You need to do this in order to use the address later on

• Load Word – This takes an address in memory (located in a register) and then puts the data from the address into another register

• Store Word – This takes data from a register and stores data into a place in memory (place in memory is stored in a register)

Page 18: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Memory in MIPS: LA• Memory in MIPS is broken into two parts. The ".text"

segment, which contains the code and the ".data" segment which contains data (don’t worry about the stack)

• You don't need to actually know the real address of memory in MIPS, instead you can use labels for your memory

• For example, to store an integer in memory you would type.datamyNumber: .word 25

• This indicates that you're storing 32 bits of data (a word) in a place that is accessed by the word "myNumber"

• If you want to load or store this word later, you need to load the address that "myNumber" represents into a register

• This is done with the command "la" – load addressla $t0, myNumber # put address of myNumber

# into $t0

Page 19: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Memory in MIPS: LW

• Once you've loaded the address into a register, you're able to retrieve the data or store new data into that place in memory

• Load Word is used to retrieve data.• It requires a register that will store the new

data, the register that is storing the address and an integer that determines how many addresses farther you want to move

• Example:– lw $t0,0($t3) # take the address in ($t3 + 0) and take

#the data from the address and store in $t0– lw $s3,16($t1) # take the address from ($t1 + 16) and take

the # data from the address and store in $s3

Page 20: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Memory in MIPS: SW

• The Store Word command is used to take data from a register and save it at a place in memory

• The place in memory is taken from another register plus a constant

• Example:– sw $t0,0($t3) # take the data from $t0 and store at

the # address at ($t3+0) – sw $s3,16($t1) # take the data from $s3 and store

at the # address at ($t1 + 16)

Page 21: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Example Load and Store// take the value of A[8]+5 and store in A[8]int [] A = new int[16];A[12] = 5 + A[8];

la $t3, A # load address of array Alw $t0,32($t3) # load A[8] into $t0addi $t0,$t0,5 # 5 + A[8]sw $t0,48($t3) # save $t0 at address A[12]

• Why is it 32($t3) if we want to get A[8]? • Because (32 = 4 bytes for one integer * position 8 in array)

Page 22: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

MIPS mini-review

• So far we’ve learned the following MIPS commands

We know registers need to be loaded from memoryAlso remember, we load words (4 bytes), but need to get them using bytes!!

Page 23: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Instruction Formatting• We know a little about instructions and registers. • When we use instructions and registers, it is

using a format that can be easily changed into binary, a language the processor can understand

• Different instructions have different formatting. • Each instruction is 32-bits, and the different parts

of the instruction are separated and placed in specific spots within the 32 bits

• Just like with floating point numbers, different parts of the 32 bits were used for different things. It’s the same with instruction formatting

Page 24: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Instruction Formatting

Op – the opcode or the bits used to tell what kind of instruction is being executedRs, Rt and Rd – are registersImmediate – is a constant numberTarget – is an address in memory

Page 25: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Types of Instructions

• There are three types of instructions that are used in MIPS

• R-type instructions – These use 3 variables (operands). – Ex. A = B+C

• I-type instructions – These use 2 variables and a constant. – Ex. A = B+7

• J-type instructions – These change the control of the program and require only an address to Jump to – Ex. Function calls, loops, branch statements

Page 26: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

R-Type Instructions (Register-Register)

Page 27: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

I-Type Instructions (Register – Immediate)

• “Immediate” is the word used for a constant• Add Immediate Example

– addi $1,$2,100 // $1= $2 + 100;• The immediate is the constant (16 bits of it)

Page 28: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

I-Type with Load/Store• A load and store operation will also use

the “immediate” I-Type format. • It uses the immediate part to store how

much to add to the memory address• Load Word Example

lw $1,100($2) # $1 = Memory[100+$2]

Page 29: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

J-Type Instruction (Jump)• J Type is used to jump to another address in

memory (the address is usually a label, like the "la" command)

• Jump Example - jump will change what the next instruction to be executed is– j 1000

• This “j” will go to address 1000. It will cause the processor to start executing whatever command is at address 1000

Page 30: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

The Program Counter• The program counter (PC) is a special register that

keeps track of where the next instruction is in memory.

• After the current instruction finishes, the CPU will look in the PC to see where the next one is

• Operations like “J-Type” can change the PC because they go to somewhere else in the program.

• They change the PC, so the computer executes a different instruction

• The PC is sequential, (meaning it goes 1,2,3,4,5) in the order of instructions executed

• A jump or branch instruction can change the order

Page 31: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Program Counter• The PC stores the address of the next

instruction • Every time an instruction finishes, the

counter goes to the next address in memory, unless a J-type tells it something different.

• If we are doing byte addressing and each instruction is 32 bits (4 bytes), every instruction is 4 bytes away.

• The PC might start at 0x100 -> 0x104 -> 0x108

Page 32: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

The PC and If Statements

Does the PC go to address 0x1000c?? No!! We branch to another statement

Text

Page 33: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Branch Statements

• There are a few useful branch statements which allow assembly programmers to use the if/else condition

• beq – Branch on Equal – this command will go to an address in memory if two registers are equal– Example: beq $t0,$t1,label1– If $t0 == $t1, then go to the address that is marked by

the label "label1"• bne – Branch not equal – this command will go to

an address in memory if the two registers are not equal– Example: bne $s0,$s1,loop– If $s0 != $s1 then go to the address marked by the

label "loop"

Page 34: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

I-Type Instruction (branching)

• Branching instructions also change the PC. • A branch will use the I-type format

Branch Not Equal Example

bne $1, $2, 100 # If ($1!= $2) goto [PC+4+(100*4)]

Page 35: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Another J-Type Example

Jump Register will change the PC to the address that is stored in register 31. By setting the PC to this value, the program will continue running, but at the new addressthat was in the register 31

Page 36: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

How functions work

Notice the “jal 0x30408”. That will setPC to 0x30408 and R31 to 0x1000cNotice “jr $31” will jump to the address stored in R31, which is 0x1000c

Page 37: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

MIPS Instructions• There is also a file on the website that shows all MIPS Instructions

• Arithmetic Operations• add add $1,$2,$3 $1 = $2 + $3 3 operands• subtract sub $1,$2,$3 $1 = $2 – $3 3 operands• add immediate addi $1,$2,5 $1 = $2 + 5 + constant• add unsigned addu $1,$2,$3 $1 = $2 + $3 3 operands• subtract unsigned subu $1,$2,$3 $1 = $2 – $3 3 operands• add imm. unsign. addiu $1,$2,5 $1 = $2 + 5 + constant• multiply mult $2,$3 Hi, Lo = $2 x $3 64-bit signed product• multiply unsigned multu $2,$3 Hi, Lo = $2 x $3 64-bit

unsigned product• divide div $2,$3 Lo = $2 ÷ $3, Lo = quotient,

Hi = $2 mod $3• divide unsigned divu $2,$3 Lo = $2 ÷ $3,Hi = Rem.• Move from Hi mfhi $1 $1 = Hi Used to get copy of Hi• Move from Lo mflo $1 $1 = Lo Used to get copy of Lo

Page 38: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

MIPS Instructions• Logical Operations• and and $1,$2,$3 $1 = $2 & $3 Bitwise AND• or or $1,$2,$3 $1 = $2 | $3 Bitwise OR• xor xor $1,$2,$3 $1 = $2 XOR $3 Bitwise XOR• nor nor $1,$2,$3 $1 = ~($2 | $3) Bitwise NOR• and immediate andi $1,$2,10 $1 = $2 & 10 Bitwise AND reg, const• or immediate ori $1,$2,10 $1 = $2 | 10 Bitwise OR reg,

const• xor immediate xori $1, $2,10 $1 = ~$2 &~10 Bitwise XOR reg, const• shift left logical sll $1,$2,10 $1 = $2 << 10 Shift left by

constant• shift right logical srl $1,$2,10 $1 = $2 >> 10 Shift right by

constant• shift right arithm. sra $1,$2,10 $1 = $2 >> 10 Shift right (sign

extend)• shift left logical sllv $1,$2,$3 $1 = $2 << $3 Shift left by var• shift right logical srlv $1,$2, $3 $1 = $2 >> $3 Shift right by var• shift right arithm. srav $1,$2, $3 $1 = $2 >> $3 Shift right arith.

by var

Page 39: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

MIPS Instructions• Data Transfer Operations• Store word SW R3, 500(R4) # Memory[500+R4] =

R3• Store half word SH R3, 502(R2)• Store byte SB R2, 41(R3)• Load Word LW R1, 30(R2) # R1 =Memory[R2

+ 30]• Load Halfword LH R1, 40(R3) • Load Halfword Uns LHU R1, 40(R3) • Load Byte LB R1, 40(R3) • Load Byte Unsigned LBU R1, 40(R3) • Load Upper Imm. LUI R1, 40 (16 bits

shifted left by 16)

Pseudo-Commands (special commands that make things easier for humans, but that do not really exist in the ISA)

• Load Address la $t0,x # load address of label x into $t0• Load Immediate li $t0,56 # load immediate value

into $t0

Page 40: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

MIPS Instructions• Compare and Branch• branch on equal beq $1,$2,100 if ($1 == $2) go to

PC+4+100• branch on not eq. bne $1,$2,100 if ($1!= $2) go to

PC+4+100*4 • set on less than slt $1,$2,$3 if ($2 < $3) $1=1; else

$1=0• set less than imm. slti $1,$2,100 if ($2 < 100) $1=1; else

$1=0• set less than uns. sltu $1,$2,$3 if ($2 < $3) $1=1; else

$1=0• set l. t. imm. uns. sltiu $1,$2,100 if ($2 < 100)

$1=1; else $1=0• jump j 10000 go to 40000• jump register jr $31 go to $31• jump and link jal 10000 $31 = PC + 4; go to

40000

Page 41: IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education

Summary of MIPS Assembly

• We have begun to look at how MIPS works• We know a few instructions and we know that

they have corresponding instruction formats, which the machine uses to run the instruction

• We know about the Program Counter and how we get the next instruction that needs to run

• Next time we will start looking at programming a MIPS program