mips wrap up n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
MIPS Wrap-up PowerPoint Presentation
Download Presentation
MIPS Wrap-up

Loading in 2 Seconds...

play fullscreen
1 / 20

MIPS Wrap-up - PowerPoint PPT Presentation


  • 381 Views
  • Uploaded on

MIPS Wrap-up. Local Variables and Procedure Examples. Outline. Example MIPS program Example of local variables Class discussion on sort(x,y,z) Review of MIPS. Example MIPS Program. .data #page 7 str: .asciiz “the answer = “ #page 7 .text #page 8

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'MIPS Wrap-up' - cleavon


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
mips wrap up

MIPS Wrap-up

Local Variables and Procedure Examples

outline
Outline
  • Example MIPS program
  • Example of local variables
  • Class discussion on sort(x,y,z)
  • Review of MIPS
example mips program
Example MIPS Program

.data #page 7

str: .asciiz “the answer = “ #page 7

.text #page 8

.globl main #page 7

main: li $v0, 4 #page 15

la $a0, str #page 17

syscall #page 9

li $v0, 1

li $a0, 5

syscall #page 9

end: li $v0, 10

syscall #page 8

local variables
Local Variables
  • Local variables are used in procedures to store temporary information
    • relevant for lifetime of that procedure only
  • In high level languages, allocation of memory for such local variables is carried out at declaration/initialisation time
  • When the procedure finishes, the used memory is restored
  • How?
    • (also see the book, page 138, also A.6)
local variables in mips
Local Variables in MIPS
  • In MIPS a temporary variable is implemented by use of a register
    • this is the only resource to play with!
  • But a register used in ‘this’ procedure might also be used by ‘that’ procedure
  • A register used in this procedure might have been used in the main program or calling procedure.
  • “Housekeeping” is required.
slide7

Why Are Stacks So Great? (Patterson)

Stacking of Subroutine Calls & Returns and Environments:

A

A:

CALL B

CALL C

C:

RET

RET

B:

A

B

A

B

C

A

B

A

Each procedure invocation makes use of the stack,

and during its lifetime ‘owns’ its part of the stack

(‘stack frame’).

(Me!)

use stack for local variables
Use Stack for Local Variables
  • Just as the stack is used to store return addresses and parameters
  • It is also used to save and restore the data referenced by registers,
    • when these are used as temporary store by a procedure, for local variables.
example swap
Example (swap)

swap(int *x, int *y)

/*swaps x and y*/

{

int temp;

temp = *x;

*x = *y;

*y = temp;

}

swap in mips
Swap in MIPS

SWAP: addi $29,$29,-4 #adjust stack pointer for $16

sw $16, 0($29) #save $16 on stack (push)

#main body follows

add $16,$0,$5 #temp = x (assume in $5)

add $5,$0,$6 #x = y (assume in $6)

add $6,$0,$16 #y = temp

#restore stack as follows:

lw $16,0($29) #$16 gets its old value back

addi $29,$29,4 #restore stack pointer (pop)

jr $31 #continue from (1) above

exercise sort x y z
Exercise sort(x,y,z)
  • Write a MIPS procedure that given 3 ints x,y,z, will sort them in ascending order.
mips review
MIPS Review

software

instruction set

hardware

(PATTERSON)

slide13

(PATTERSON)

Obtain instruction from program storage

Instruction

Fetch

Determine required actions and instruction size

Instruction

Decode

Locate and obtain operand data

Operand

Fetch

Compute result value or status

Execute

Deposit results in storage for later use

Result

Store

Determine successor instruction

Next

Instruction

slide14

(PATTERSON)

Register (direct)

(R-type)

op

rs

rt

rd

register

Immediate

op

rs

rt

immed

Base+index

(I-type)

op

rs

rt

immed

Memory

register

+

Branch

op

rs

rt

immed

Memory

PC

+

mips arithmetic instructions patterson
MIPS arithmetic instructions (PATTERSON)

Instruction Example Meaning Comments

add add $1,$2,$3 $1 = $2 + $3 3 operands; exception possible

subtract sub $1,$2,$3 $1 = $2 – $3 3 operands; exception possible

add immediate addi $1,$2,100 $1 = $2 + 100 + constant; exception possible

add unsigned addu $1,$2,$3 $1 = $2 + $3 3 operands; no exceptions

subtract unsigned subu $1,$2,$3 $1 = $2 – $3 3 operands; no exceptions

add imm. unsign. addiu $1,$2,100 $1 = $2 + 100 + constant; no exceptions

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 = remainder

Hi = $2 mod $3

divide unsigned divu $2,$3 Lo = $2 ÷ $3, Unsigned quotient & remainder

Hi = $2 mod $3

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

slide16

MIPS logical instructions (PATTERSON)

Instruction Example Meaning Comment

and and $1,$2,$3 $1 = $2 & $3 3 reg. operands; Logical AND

or or $1,$2,$3 $1 = $2 | $3 3 reg. operands; Logical OR

xor xor $1,$2,$3 $1 = $2 Å $3 3 reg. operands; Logical XOR

nor nor $1,$2,$3 $1 = ~($2 |$3) 3 reg. operands; Logical NOR

and immediate andi $1,$2,10 $1 = $2 & 10 Logical AND reg, constant

or immediate ori $1,$2,10 $1 = $2 | 10 Logical OR reg, constant

xor immediate xori $1, $2,10 $1 = ~$2 &~10 Logical XOR reg, constant

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 variable

shift right logical srlv $1,$2, $3 $1 = $2 >> $3 Shift right by variable

shift right arithm. srav $1,$2, $3 $1 = $2 >> $3 Shift right arith. by variable

slide17

MIPS data transfer instructions (PATTERSON)

Instruction Comment

SW 500(R4), R3 Store word

SH 502(R2), R3 Store half

SB 41(R3), R2 Store byte

LW R1, 30(R2) Load word

LH R1, 40(R3) Load halfword

LHU R1, 40(R3) Load halfword unsigned

LB R1, 40(R3) Load byte

LBU R1, 40(R3) Load byte unsigned

LUI R1, 40 Load Upper Immediate (16 bits shifted left by 16)

slide18

Compare and Branch (PATTERSON)

  • Compare and Branch #see pages 15,16 of SPIM documentation
  • BEQ rs, rt, offset if content(rs) == content(rt) then
          • PC-relative branch
  • BNE rs, rt, offset <>
  • BLEZ rs, offset if content(rs) <= 0 then PC-relative branch
  • BGTZ rs, offset >0
  • BLT <
  • BGEZ >=0
slide19

MIPS jump, branch, compare instructions (PATTERSON)

Instruction Example Meaning

branch on equal beq $1,$2,100 if ($1 == $2) go to PC+4+100 Equal test; PC relative branch

branch on not eq. bne $1,$2,100 if ($1!= $2) go to PC+4+100 Not equal test; PC relative

set on less than slt $1,$2,$3 if ($2 < $3) $1=1; else $1=0 Compare less than; 2’s comp.

set less than imm. slti $1,$2,100 if ($2 < 100) $1=1; else $1=0 Compare < constant; 2’s comp.

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

set l. t. imm. uns. sltiu $1,$2,100 if ($2 < 100) $1=1; else $1=0 Compare < constant; natural

jump j 10000 go to 10000 Jump to target address

jump register jr $31 go to $31 For switch, procedure return

jump and link jal 10000 $31 = PC + 4; go to 10000 For procedure call

next time
Next Time...
  • Back to basics
    • binary representations
    • integer numbers
    • negative numbers
    • arithmetic
    • floating point numbers