mips instructions, mars debugging, kb vs. kibewang/cs260/ln/2.1_mips_mars... · 2014. 6. 17. ·...

16
MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture Week 2.1 Mon 2014/06/16

Upload: others

Post on 25-Aug-2021

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

MIPS Instructions,

MARS Debugging,

KB vs. KiB

CptS 260Introduction to Computer Architecture

Week 2.1Mon 2014/06/16

Page 2: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

Programming: Learn by Doing It!

• Process-Based Skill

– creative endeavor

• Not (much) memorization– Language keywords & constructs– Techniques, tips & tricks

“Cats” R. Wauler

Look It Up

• green reference card

• 2.2 Figure 2.1 (p.64)

• Appendix A.10 (p.A-54)

Page 3: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

“Doing It”: HWs and Exams

Part I download

Part IIreading

……

Part III [_] To Do

… [_] To Do

Appendix reading

Programming HW PDF Midterm/Final Exam

Write a function that does <something>.

something (m, n):$a0 (int) m$a1 (int) n

It shall return (in $v0) < some result>.

something:

<your code here>

Page 4: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

MIPS Instructions: (Integer) Arithmetic[P&H14] §2.2

• add rd, rs, rt rd = rs + rt (just like C)• addi rd, rs, C rd = rs + C |C| = 16 bits (2’s complement)

or: 32 bits (pseudo-instruction)• sub• subi (MARS pseudo-instruction)

• mul rd, rs, rt { rd, LO } = rs * rt (lower 32 bits)HI = (upper 32 bits)

• div rd, rs, rt rd = rs \ rt truncates!

Page 5: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

• sll rd, rs, k rd = rs << k (just like C)

• srl rd, rs, k rd = rs >> k w/o sign-extension• sra rd, rs, k rd = rs >> k with sign-extension

• and rd, rs, rt rd = rs & rt• andi rd, rs, C rd = rs & C

• or rd, rs, rt rd = rs | rt• ori rd, rs, C rd = rs | C

• nor rd, rs, rt rd = ~(rs | rt)

It’s the Other Way Around:C is Just Like MIPS!!

MIPS Instructions: Logical and Bitwise[P&H14] §2.6

Page 6: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

CPU Architectures

Load/store :RISC (MIPS)vector

Register-memoryIntel x86, IBM 360

Register plus memoryMotorola 68000, VAX

registers

memory

registers

memory

Page 7: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

• lw rd, offset (rp ) rt = *<word *>(rp + offset) (32 bits)• sw rs, offset (rp ) *<word *>(rp + offset) = rs

• lh/sh halfword (16 bits)• lhu unsigned halfword (no sign extension)

• lb/sb byte (8 bits)• lbu unsigned byte (no sign extension)

• li rd, C rd = C• la rd, label rd = & label

• lui rd, C <upper 16 bits of rd> = C(lower 16 bits of rd unaffected)

MIPS Instructions: Data Transfer[P&H14] §2.2

Page 8: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

Values, Addresses, and Pointers

.data

len: .word 00x1001 0000

0x1001 0004

My_t: .word 150x1001 0008

0x1001 000c S: .asciiz “good …

… “bye,” …

c: ‘a’ d: ‘d’

… “cru” …

… “el w” …

… “orld” …

// C valuefor (int tmp = len; …)

// C address-ofint * P = &len;

// C name-of-array is// its own addresschar * Q = S; // == &S

# MIPS `lw` (et al.)lw $t3, len # tmp

# lw/lh/lb load# sw/sh/sb store

# MIPS `la`la $t1, len # P

la $t7, S # Q

address value

Page 9: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

Program Counter $pc (Is a Pointer!)

$pc

.text

if (t1 == t2)++t2;

else--t1;

nop;

bne $t1, $t2, Elseaddi $t2, $t2, 1j Eh

Else:addi $t1, $t1, –1

Eh:nop

bne0000

0x0040:

addi0004

j0008

000c

0x0040 0010

$t1 $t2 +0xc (bytes)

$t2$t2 0x0001

addi $t1$t1 0xffff

0010 nop

0x0040 0008

32 bits

• Changing $pc is � a jump! (goto)• And vice versa

Page 10: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

Munging $pc: Jump and Branch

.text

bne0000

addi0004

j0008

000c

0x0040 0010

$t1 $t2 +0xc (bytes)

$t2$t2 0x0001

addi $t1$t1 0xffff

0010 nop

• default $pc += 4 bytes• jump $pc = 26-bit address• branch $pc += 16-bit offset

Page 11: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

• j label Jump unconditionally (26 bits: same segment)• jr rs Jump to register (32 bits: anywhere!!)

• beq rs, rt, label if (rs == rt) PC += ∆label (16 bits)• bne if (rs != rt) …

• blt, ble (<), (<=)• bgt, bge (>), (>=)

• beqz, bnez rs, label (== 0), (!= 0)• bltz, blez, bgtz, bgez

• slt, slti Don’t use these – too obscure!!(try them once, then you’ll agree)

MIPS Instructions: Decisions and Gotos[P&H14] §2.7

Page 12: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

MARS: Introduction to Debugging

• Also a Process-Based Skill!

– investigative endeavor

• Tool Support� printf() single-step debugger

Page 13: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

MIPS Example: Reverse an Array of Ints

• As a C function

void reverse(int * A, int n){

if (n <= 1) return;int * lo = A;int * hi = A + n – 1;

while (lo < hi)swap (*lo++, *hi––);

}

// array of n elements

// two pointers// ≡ &A[n – 1]: “last element”

// until meet or cross

3 5 8 4 2 9

A

n

lo hi = &A[n –1]

E = &A[n] = A + n

// mixing C and MIPSswap(int * P, int * Q){

lw $tp, *Plw $tq, *Qsw $tq, *Psw $tp, *Q

}

Page 14: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

swap(int * P, int * Q){

lw $tp, *Plw $tq, *Q

sw $tq, *Psw $tp, *Q

}

void reverse(int * A, int n){

if (n <= 1) return;int * lo = A;int * hi = A + n – 1;

while (lo < hi)swap (*lo++, *hi––);

}

MIPS Example: Reverse an Array of Ints

• As a MIPS program (main: block)

Page 15: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

MARS Execute / Data / Registers

• After compilation (F3)

array:

Page 16: MIPS Instructions, MARS Debugging, KB vs. KiBewang/cs260/ln/2.1_MIPS_MARS... · 2014. 6. 17. · MIPS Instructions, MARS Debugging, KB vs. KiB CptS 260 Introduction to Computer Architecture

Not KiB -bing Around:Digital Quantity Prefixes

• 210 ≈ 103: a “K” (kilo) – but not quite

• Int’l System of Quantities(1998!)

– Int’l ElectrotechnicalCommission (IEC)

• [P&H14] §1.1 Figure 1.1(p.6)