{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# L10 - CS61C Machine Structures Lecture 10 MIPS Branch...

This preview shows pages 1–4. Sign up to view the full content.

CS 61C L10 MIPS Branch II (1) Wawrzynek Fall 2007 © UCB 9/19/2006 Greg Gibeling (for John Wawrzynek) (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS61C – Machine Structures Lecture 10 - MIPS Branch Instructions II CS 61C L10 MIPS Branch II (2) Wawrzynek Fall 2007 © UCB Compiling C if into MIPS (1/2) ° Compile by hand if (i == j) f=g+h; else f=g-h; ° Use this mapping: f : \$s0 g : \$s1 h : \$s2 i : \$s3 j : \$s4 Exit i == j? f=g+h f=g-h (false) i != j (true) i == j

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS 61C L10 MIPS Branch II (3) Wawrzynek Fall 2007 © UCB Compiling C if into MIPS (2/2) ° Final compiled MIPS code: beq \$s3,\$s4,True # branch i==j sub \$s0,\$s1,\$s2 # f=g-h(false) j Fin # goto Fin True: add \$s0,\$s1,\$s2 # f=g+h (true) Fin: Note: Compiler automatically creates labels to handle decisions (branches). Generally not found in HLL code. Exit i == j? f=g+h f=g-h (false) i != j (true) i == j Compile by hand if (i == j) f=g+h; else f=g-h; CS 61C L10 MIPS Branch II (4) Wawrzynek Fall 2007 © UCB Review ° Memory is byte -addressable, but lw and sw access one word at a time. ° A pointer (used by lw and sw ) is just a memory address, so we can add to it or subtract from it (using offset). ° A Decision allows us to decide what to execute at run-time rather than compile-time. ° C Decisions are made using conditional statements within if , while , do while , for . ° MIPS Decision making instructions are the conditional branches : beq and bne . ° New Instructions: lw, sw, beq, bne, j
CS 61C L10 MIPS Branch II (5) Wawrzynek Fall 2007 © UCB From last time: Loading, Storing bytes 1/2 ° In addition to word data transfers ( lw , sw ), MIPS has byte data transfers: ° load byte: lb ° store byte: sb ° same format as lw , sw Ex: lb \$s0, 3(\$s1) contents of memory location with address = sum of “3” + contents of register s1 is copied to the low byte position of register s0. CS 61C L10 MIPS Branch II (6) Wawrzynek Fall 2007 © UCB x Loading, Storing bytes 2/2 ° What do with other 24 bits in the 32 bit register? lb : sign extends to fill upper 24 bits byte loaded …is copied to “sign-extend” This bit xxxx xxxx xxxx xxxx xxxx xxxx zzz zzzz Normally don't want to sign extend chars MIPS instruction that doesn’t sign extend when loading bytes: load byte unsigned: lbu

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}