Vous êtes sur la page 1sur 27

CPS 104 Computer Organization and Programming Lecture-10 : The MIPS Instruction Set Architecture; MIPS Assembler, SPIM.

Feb. 2, 2004 Gershon Kedem http://kedem.duke.edu/cps104/Lectures

CPS104 Lec-10.1

GK Spring 2004

Review: The Program Counter


Special register (pc) that points to instructions Contains memory address (like a pointer) Instruction fetch is
u

inst = mem[pc]

To fetch next sequential instruction pc


u

= pc + ?

Size of instruction?

CPS104 Lec-10.2

GK Spring 2004

Review: I-Type <op> rt, rs, immediate


31 26 25 21 20 16 15 0

Op

Rs PC

Rt

Immediate Memory

+
4

PC relative addressing Branch Not Equal Example bne $1, $2, 100 # If ($1!= $2) goto [PC+4+100] +4 because by default we increment for sequential u more detailed discussion later in semester

op

rs

rt

immediate

000101

00001

00010

0000 0000 0110 0100

CPS104 Lec-10.3

GK Spring 2004

Review: J-Type:
31 26

<op> target

Op

Target Address PC $31

Jump and Link Example jal 0x0fab8 # PC<- 0x0fab8, $31<-PC+4 $31 set as side effect, used for returning, implicit operand

op

Target

000011 00 0000 0000 0011 1110 1010 1110


Please note, The address is a WORD ADDRESS!
CPS104 Lec-10.4
GK Spring 2004

R Type:
31 26 25 21 20

<OP> rd, rs, rt


16 15 11 10 6 5 0

Op

Rs

Rt

Rd

shamt

func

Jump Register Example jr $31 # PC <- $31

op

rs

rt

rd

shmt

func

000000

11111

00000 00000 00000

001000

CPS104 Lec-10.5

GK Spring 2004

Instructions for Procedure Call and Return


int equal(int a1, int a2) { int tsame; tsame = 0; if (a1 == a2) tsame = 1; return(tsame); } main() { int x,y,same; x = 43; y = 2; same = equal(x,y); // other computation }

0x10000 addi $1, $0, 43 0x10004 addi $2, $0, 2 0x10008 jal 0x30408 ?? 0x1000c 0x30408 0x3040c 0x30410 0x30414 addi $3, $0, 0 bne $1, $2, 4 addi $3, $0, 1 jr $31 $31 ?? ?? ?? 0x1000c 0x1000c 0x1000c 0x1000c 0x1000c
GK Spring 2004

PC 0x10000 0x10004 0x10008 0x30408 0x3040c 0x30410 0x30414 0x1000c

CPS104 Lec-10.6

MIPS Arithmetic Instructions


Instruction
add subtract add immediate add unsigned subtract unsigned add imm. unsign. multiply

Example
add $1,$2,$3 sub $1,$2,$3 addi $1,$2,100 addu $1,$2,$3 subu $1,$2,$3 addiu $1,$2,100 mult $2,$3

Meaning
$1 = $2 + $3 $1 = $2 $3 $1 = $2 + 100 $1 = $2 + $3 $1 = $2 $3 $1 = $2 + 100 Hi, Lo = $2 x $3 Hi, Lo = $2 x $3 Lo = $2 $3, Hi = $2 mod $3 Lo = $2 $3, Hi = $2 mod $3 $1 = Hi $1 = Lo

Comments
3 operands 3 operands + constant 3 operands 3 operands + constant 64-bit signed product 64-bit unsigned product Lo = quotient, Hi = remainder Unsigned quotient Unsigned remainder Used to get copy of Hi Used to get copy of Lo

multiply unsigned multu $2,$3 divide div $2,$3 divide unsigned Move from Hi Move from Lo divu $2,$3 mfhi $1 mflo $1

Which add for address arithmetic? Which for integers?


CPS104 Lec-10.7
GK Spring 2004

MIPS Logical Instructions


Instruction and or xor nor and immediate or immediate xor immediate shift left logical shift right logical shift right arithm. shift left logical shift right logical shift right arithm. Example and $1,$2,$3 or $1,$2,$3 xor $1,$2,$3 nor $1,$2,$3 andi $1,$2,10 ori $1,$2,10 xori $1, $2,10 sll $1,$2,10 srl $1,$2,10 sra $1,$2,10 sllv $1,$2,$3 srlv $1,$2, $3 srav $1,$2, $3 Meaning $1 = $2 & $3 $1 = $2 | $3 $1 = $2 $3 $1 = ~($2 | $3) $1 = $2 & 10 $1 = $2 | 10 $1 = ~$2 &~10 $1 = $2 << 10 $1 = $2 >> 10 $1 = $2 >> 10 $1 = $2 << $3 $1 = $2 >> $3 $1 = $2 >> $3 Comment Bitwise AND Bitwise OR Bitwise XOR Bitwise NOR Bitwise AND reg, const Bitwise OR reg, const Bitwise XOR reg, const Shift left by constant Shift right by constant Shift right (sign extend) Shift left by var Shift right by var Shift right arith. by var

CPS104 Lec-10.8

GK Spring 2004

MIPS Data Transfer Instructions


Instruction SW R3, 500(R4) SH R3, 502(R2) SB R2, 41(R3) LW R1, 30(R2) LH R1, 40(R3) LHU R1, 40(R3) LB R1, 40(R3) LBU R1, 40(R3) LUI R1, 40 Comment Store word Store half Store byte Load word Load half word Load half word unsigned Load byte Load byte unsigned Load Upper Immediate (16 bits shifted left by 16)

Why do we need LUI? LUI R5


CPS104 Lec-10.9

R5 0000 0000
GK Spring 2004

MIPS Compare and Branch


Compare and Branch
beq bne blez bgtz bltz rs, rt, offset rs, rt, offset rs, offset rs, offset rs, offset if R[rs] == R[rt] then PC-relative branch <> if R[rs] <= 0 then PC-relative branch > < >= if R[rs] < 0 then branch and link (into R 31) >=

Compare to zero and Branch

bgez rs, offset bltzal rs, offset bgeal rs, offset


Remaining set of compare and branch take two instructions Almost all comparisons are against zero!

CPS104 Lec-10.10

GK Spring 2004

MIPS jump, branch, compare instructions


Instruction branch on equal Example beq $1,$2,100 Meaning if ($1 == $2) go to PC+4+100 Equal test; PC relative branch if ($1!= $2) go to PC+4+100 Not equal test; PC relative if ($2 < $3) $1=1; else $1=0 Compare less than; 2s comp. if ($2 < 100) $1=1; else $1=0 Compare < constant; 2s comp. if ($2 < $3) $1=1; else $1=0 Compare less than; natural numbers if ($2 < 100) $1=1; else $1=0 Compare < constant; natural numbers go to 10000 Jump to target address go to $31 For switch, procedure return $31 = PC + 4; go to 10000 For procedure call
GK Spring 2004

branch on not eq. bne $1,$2,100 set on less than slt $1,$2,$3

set less than imm. slti $1,$2,100 set less than uns. sltu $1,$2,$3 set l. t. imm. uns. sltiu $1,$2,100 jump jump register jump and link
CPS104 Lec-10.11

j 10000 jr $31 jal 10000

Signed v.s Unsigned Comparison


R1= 000 0000 0000 0000 0001 R2= 000 0000 0000 0000 0010 R3= 111 1111 1111 1111 1111 After executing these instructions: slt r4,r2,r1 slt r5,r3,r1 sltu r6,r2,r1 sltu r7,r3,r1 What are values of registers r4 - r7? Why? r4 = ; r5 = ; r6 = ; r7 = ;

CPS104 Lec-10.12

GK Spring 2004

Multiply / Divide
Start multiply, divide mult rs, rt mthi rd Move to HI or LO mtlo rd
Registers

HI

LO

Why not Third field for destination?


(Hint: how many clock cycles for multiply or divide vs. add?)

CPS104 Lec-10.13

GK Spring 2004

Summary

MIPS has 5 categories of instructions u Arithmetic, Logical, Data Transfer, Conditional Branch, Unconditional Jump 3 Instruction Formats: R-type, I-type, J-type.

Reading Ch. 3, Appendix A

CPS104 Lec-10.14

GK Spring 2004

Review: Assembler and Assembly Language

Machine language is a sequence of binary words. Assembly language is a text representation for machine language plus extras that make assembly language programming easier (more readable too!).

program

compiler

Assembler

Linker

executable code

CPS104 Lec-10.15

GK Spring 2004

Review: The SPIM Simulator

SPIM is a simulator that let you run and debug MIPS assembler programs. XSPIM is an X-window version of the simulator. The simulator allows you to look at the content of registers and memory, and to single step through the simulation. Documentation in postscript format and PDF format are in: http://kedem.cs.duke.edu/cps104/Handouts.html Also available a MAN page for SPIM. Try to run the provided example of an simple assembler program: http://kedem.cs.duke.edu/cps104/Handouts/MIPS.asm

CPS104 Lec-10.16

GK Spring 2004

Assembly Language

One instruction per line. Numbers: are base-10 integers or Hex. Identifiers: alphanumeric, _, string starting in a letter or _ Labels: identifiers starting at the beginning of a line followed by : Comments: everything following # till end-of-line. Instruction format: Space and , separated fields.
u u u

[Label:] <op> Arg1, [Arg2], [Arg3] [# comment] [Label:] <op> arg1, offset(reg) .Directive [arg1], [arg2], . . . [# comment]

CPS104 Lec-10.17

GK Spring 2004

Assembly Language (cont.)

Pseudo-instructions: extending the instruction set for convenience.


Examples: u move $2, $4 Translates to: add $2, $4, $0
u

# $2 = $4, (copy $4 to $2)

li $8, 40 addi $8, $0, 40 sd $4, 0($29) sw $4, 0 ($29) sw $5, 4($29) la $4, 0x1000056c lui $4, 0x1000 ori $4, $4, 0x056c

# $8 = 40, (load 40 into $8)

# mem[$29] = $4; Mem[$29+4] = $5

# Load address $4 = <address>

CPS104 Lec-10.18

GK Spring 2004

Assembly Language (cont.)

The assembler is smart enough to figure out what instructions to use depending on the arguments types. u For Example: u add $1, $2, -345 will be translated into addi $1, $2, -345 div $1, $2, $3 will be translated into div $2, $3 mflo $1 The assembler also translates labels into addresses. For Example one can use: u la $1, array1 to load the address that the label array1 is attached to. u bne $2, $4, loop1 to branch into the instruction that has the label loop1
u
GK Spring 2004

CPS104 Lec-10.19

Assembly Language; Directives


Directives: Examples:
u u u u

.<string>

[arg1], [arg2] . . .

u u

.align n # align datum on 2n byte boundary. .ascii <string> # store a string in memory. .asciiz <string> # store a null terminated string in memory .data [address] # start a data segment. # [optional beginning address] .text [address] # start a code segment. .word w1, w2, . . . , wn # store n words in memory.

CPS104 Lec-10.20

GK Spring 2004

The routine written in C

#include <stdio> int main ( ) { int i; int sum = 0; for(i=0; i <= 100; i++) sum = sum + i*i ; printf(The sum from 0 .. 100 is %d\n, sum) ; }

CPS104 Lec-10.21

GK Spring 2004

Assembly Language Example1:


.text .align 2 main: la loop: lw mul lw add sw addi sw ble la lw jal $14, 4($10) $15, $14, $14 $24, 0($10) $25, $24, $15 $25, 0($10) $8, $14, 1 $8, 4($10) $8, 100, loop $4, str $5, 0($10) printf $10, Temp .data .align 2 .word 0, 0 li $2, 10 syscall

# Exit

Temp: str: .asciiz The sum from 0 .. 100 is %d\n

CPS104 Lec-10.22

GK Spring 2004

MIPS: Software conventions for Registers


0 1 2 3 4 5 6 7 8 ... 15 t7 zero constant 0 at reserved for assembler 16 s0 callee saves ... 23 s7 24 t8 25 t9 26 k0 reserved for OS kernel 27 k1 28 gp Pointer to global area temporary: caller saves 29 sp Stack pointer 30 fp 31 ra frame pointer Return Address (HW) temporary (contd)

v0 expression evaluation & v1 function results a0 arguments a1 a2 a3 t0

CPS104 Lec-10.23

GK Spring 2004

Memory Layout
0x7fffffff

Stack segment

Dynamic data

Data segment
Static data 0x10000000

Text segment
0x400000
CPS104 Lec-10.24

Reserved

GK Spring 2004

Example2
# Example for CPS 104 # Program to add together list of 9 numbers. .text .align .globl main: subiu sw sw sw sw stack sw move move la la la $s0, 20($sp) $v0, $0 $s1, $s0, $s2, $s3, $0 list msg list+36 # / #/ initialize exit code to 0 #\ # \ Initialization # / #/ $sp, $ra, $s3, $s2, $s1, 40 36($sp) 32($sp) 28($sp) 24($sp) # Code 2 main # MAIN procedure Entrance #\ Push the stack # \ Save return address # \ # > Entry Housekeeping # / save registers on

CPS104 Lec-10.25

GK Spring 2004

Example2 (cont.)
# again: lw add li move syscall li move syscall li la syscall addiu bne $t6, 0(s0) $s1, $s1, $t6 $v0, 4 $a0, $s2 $v0, 1 $a0, $s1 $v0, 4 $a0, nln Main code segment # Begin main loop #\ #/ Actual "work" # SPIM I/O #\ # > Print a string #/ #\ # > Print a number #/ #\ # > Print a string (eol) #/ #\ index update and #/ end of loop

$s0, $s0, 4 $s0, $s3, again

CPS104 Lec-10.26

GK Spring 2004

Example2 (cont.)
# move lw lw lw lw lw addiu jr .end # Exit Code $v0, $0 $s0, 20($sp) $s1, 24($sp) $s2, 28($sp) $s3, 32($sp) $ra, 36($sp) $sp, 40 $ra main Data Segment .data # Start of data segment .word 35, 16, 42, 19, 55, 91, 24, 61, 53 .asciiz "The sum is " .asciiz "\n" #\ # \ # \ # \ Closing Housekeeping # / restore registers # / load return address # / Pop the stack #/ exit(0) ; # end of program

list: msg: nln:

CPS104 Lec-10.27

GK Spring 2004