MIPS ISA and MIPS Assembly CS301 Prof. Szajda Administrative HW - - PowerPoint PPT Presentation

mips isa and mips assembly
SMART_READER_LITE
LIVE PREVIEW

MIPS ISA and MIPS Assembly CS301 Prof. Szajda Administrative HW - - PowerPoint PPT Presentation

MIPS ISA and MIPS Assembly CS301 Prof. Szajda Administrative HW #2 due Wednesday (9/11) at 5pm Lab #2 due Friday (9/13) 1:30pm Read Appendix B5, B6, B.9 and Chapter 2.5-2.9 (if you have not already done so!) MIPS ISA MIPS ISA


slide-1
SLIDE 1

MIPS ISA and MIPS Assembly

CS301

  • Prof. Szajda
slide-2
SLIDE 2

Administrative

  • HW #2 due Wednesday (9/11) at 5pm
  • Lab #2 due Friday (9/13) 1:30pm
  • Read Appendix B5, B6, B.9 and Chapter

2.5-2.9 (if you have not already done so!)

slide-3
SLIDE 3

MIPS ISA

slide-4
SLIDE 4

MIPS ISA

  • Small number of simple instructions (RISC)

w Instructions are fixed size of 32b w Very rigid structure

  • Load/Store Architecture

w Few addressing modes

slide-5
SLIDE 5

Register File

  • Register file

w 32 integer w 32 single precision floating point

  • Integer

w Volatile - scratch registers

§ $t0 - $t9 ($8-$15, $24-$25)

w Non-volatile - called function does save/ restore

§ $s0 - $s7 ($16-$23)

What’s going on here?!

slide-6
SLIDE 6

Special Integer Registers

  • Zero

w $0

  • Return values

w $v0, $v1 ($2, $3)

  • Function arguments

w $a0-$a3 ($4-$7) w If more than 4 registers required, then place parameters “above” frame pointer (at higher address)

  • Stack pointer - $sp ($29)
  • Frame pointer - $fp ($30)

w Usually not used in our examples

  • Return address - $ra ($31)
slide-7
SLIDE 7

Floating Point Registers

  • 32 single-precision registers

w $f0, $f1, ..., $f31

  • Double precision uses 2 single precision fp registers

w reference even numbered registers

  • Special registers

w Return values

§ $f0-$f3

w Function arguments

§ $f12 - $f15

slide-8
SLIDE 8

Data Movement Instructions

  • move rd, rs

w Move register rs to rd

  • mov.d fd, fs

w Move double precision fp register fs to fd

  • mtc1 fd, rs

w Move rs register to fd

  • mfc1 rd, fs

w Move fp register fs to rd

move to coprocessor 1 move from coprocessor 1

slide-9
SLIDE 9

Load/Store Instructions

  • la rt, address

w load computed address (not contents of location) into register rt

  • lw rt, address

w load word in memory location address into register rt

  • sw rt, address

w store value in register rt into memory at location address

  • li rt, imm

w load integer constant imm into register rt

  • l.d rt, address

w load double precision fp value from memory location address into fp register rt and rt+1

slide-10
SLIDE 10

Arithmetic Instructions

  • add rd, rs, rt

w rd = rs + rt

  • addi rd, rs, imm

w rd = rs + imm

  • sub rd, rs, rt

w rd = rs - rt

  • mul rd, rs, rt

w rd = rs × rt

  • div rd, rs, rt

w rd = rs / rt

  • Floating point versions exist usually with a .d tacked
  • n (add.d)

multiply (without overflow) puts low order 32 bits of product in rd divide (without overflow)

slide-11
SLIDE 11

Shift Operators

  • sll rd, rt, sa

w rd = rt << sa w Zero fill

  • sra rd, rt, sa

w rd = rt >> sa w Sign fill

  • srl rd, rt, sa

w rd = rt >> sa w Zero fill

shift left logical shift right arithmetic shift right logical

slide-12
SLIDE 12

Comparison Instructions

  • slt rd, rs, rt

w Set register rd to 1 if rs < rt, otherwise set rd to 0

  • slti rd, rs, imm

w Set register rd to 1 if rs < imm, otherwise set rd to 0

  • Similar instructions for greater than
  • seq rd, rs, rt

w Set rd to 1 if rs == rt, otherwise set rd to 0

slide-13
SLIDE 13

Branch Instructions

  • 16-b instruction ofgset field (215-1

forward, 215 back)

  • b label

w Unconditionally branch to instruction at label

  • beq rs, rt, label

w Conditionally branch to label if rs == rt

  • bne, bgt, bge, ...
slide-14
SLIDE 14

Jump Instructions

  • 26-bit address field
  • j target

w Unconditionally jump to instruction at target

  • jal target

w Unconditionally jump to instruction at target w Save address of next instruction in register $ra w bal is like jal however jal must be used if target is from another file

jump and link

slide-15
SLIDE 15

Assembly Programs

slide-16
SLIDE 16

Note:

You will only be allowed to use the instructions listed in Appendix B.10 in your programs!

16

slide-17
SLIDE 17

# assign.asm # simple program to modify a global variable .data # add what follows to the data section x: .word 5 # create global integer variable x. Set to 5. .text # add what follows to the text .align 2 # Align on word boundaries .globl main # "exports" the symbol main so it is # accessible to other modules main: # we don't need a frame la $t0, x # $t0 = &x lw $t1, 0($t0) # $t1 = x addi $t1,$t1,2 # $t1 = $t1 + 2 sw $t1, 0($t0) # x = $t1 jr $ra # return - main is a function, too

slide-18
SLIDE 18

Assembly File

  • Segments

w .data

§ Integer (.word), character (.byte), arrays of these, § String (.asciiz)

w .text

§ Instructions § main should be first instruction and needs to be specified as .globl

the “z” is required if you want your string to be null terminated! an “assembler directive”

slide-19
SLIDE 19

MIPS Labels

  • MIPS assembly code contains instructions
  • Data and instructions can be prefaced with a label

followed by a colon

w main:

  • Instructions can have operands that are:

w registers w constants/immediates w addresses

§ Explicit numbers § Register + ofgset § Labels

  • Assembler will replace labels with corresponding

addresses when creating machine language

slide-20
SLIDE 20

Examples

Suppose $s0 = a, $s1 = b, $s2 = c, $s3 = d

  • Write MIPS instructions for the

following code:

b = (c - 1) × (a + d)

slide-21
SLIDE 21

Examples

Suppose $s0 = a, $s1 = b, $s2 = c, $s3 = d

  • Write MIPS instructions for the following code:

if(a > b) c = c+1; else d = d+1;

slide-22
SLIDE 22

Examples

Suppose A is an array of 10 integers

w How would I declare A as a global array?

Suppose $s0 = A, $s1 = g, $s2 = h, $s3 = i

  • Write MIPS instructions for the following code:

g = h +A[i];

slide-23
SLIDE 23

Using Strings

  • ASCII

w 8-bit used to represent characters

  • To access individual characters, use

w lb $t0, 0($sp) # read character w sb $t0, 0($sp) # write character

  • Strings

w Array of characters w Terminated with byte whose value is 0 (null)

slide-24
SLIDE 24

Examples

Suppose A is a string

w How would I declare A as a global variable?

Suppose $s0 = A, $s1 = g, $s2 = h, $s3 = i

  • Write MIPS instructions for the following code:

g = A[i];