Unformatted text preview:

1 ECE 15B COMPUTER ORGANIZATION Dr. Rahul Singh!April 23, 2009 Lecture 7!Procedures!Electrical and Computer Engineering University of California, Santa Barbara!April 23, 2009!Lecture 7: Procedures!1!Announcements!• Project #1!• Due Friday May 1 by 5:00 pm!• Start early!• Quiz #1!• Graded !• Will return in next class!Electrical and Computer Engineering University of California, Santa Barbara!April 23, 2009!Lecture 7: Procedures!2!Review: Logical Operators!• Two basic logical operators:!• AND !• outputs 1 only if both inputs are 1!• OR !• outputs 1 if at least one input is 1!• In general, can define them to accept >2 inputs!• In MIPS assembly, both of these accept exactly 2 inputs and produce 1 output!• Again, rigid syntax, simpler hardware!Truth Table a b a AND b (a ∩ b) a OR b (a ∪ b) 0 0 0 0 0 1 0 1 1 0 0 1 1 1 1 1 Electrical and Computer Engineering University of California, Santa Barbara!April 16, 2009!Lecture 6: Logic & Shift Instructions, Bytes and Overflow!3!Review: MIPS Logical Operators!• Syntax of logical operators:!• reg1 = reg2 AND reg3 (bitwise)!• register1 = register2 OR register3 (bitwise)!• Same as above, except second operand is an immediate!• MIPS logical operators are all bitwise!• Meaning that the first bit of the output is produced by the respective first bits of the inputs, the second bit of the output is produced by the second bits of the inputs, etc.!and!reg1, reg2,reg3!or!reg1, reg2,reg3!andi!reg1, reg2,immediate!ori!reg1, reg2,immediate!2 Electrical and Computer Engineering University of California, Santa Barbara!• Note:!• anding a bit with 0 produces a 0 at the output !• anding a bit with 1 produces the original bit at the output!• This can be used to create a mask.!• Example:!April 16, 2009!Lecture 6: Logic & Shift Instructions, Bytes and Overflow!4!Review: Logical Operators Uses!input: !1011!0011!0101!1100!0110! 1101! 0101! 1010!mask:!0000!0000!0000!0000!0000! 1111! 1111! 1111!result of anding these!0000!0000!0000!0000!0000! 1101! 0101! 1010!zeroʼd bits! masked last 12 bits!Electrical and Computer Engineering University of California, Santa Barbara!April 16, 2009!Lecture 6: Logic & Shift Instructions, Bytes and Overflow!5!Review: Shift Instructions!• Syntax of logical operators:!• Shift left logical !• Shifts reg2 left by immediate number of bits, fills emptied bits with 0ʼs and places result in reg1 !• Shift right logical !• Shifts reg2 right by immediate number of bits, fills emptied bits with 0ʼs and places result in reg1!! !!• Shift right arithmetic !• Shifts reg2 right by immediate number of bits, fills emptied bits by sign extending and places result in reg1 !• Note: immediate <= 32!sll!reg1, reg2, immediate!srl!reg1, reg2, immediate!sra!reg1, reg2, immediate!Electrical and Computer Engineering University of California, Santa Barbara!April 16, 2009!Lecture 6: Logic & Shift Instructions, Bytes and Overflow!6!Review: Loading, Storing bytes!• In addition to word data transfers (lw, sw), MIPS has byte data transfers:!• load byte into reg1 from memory location!• sign extends to fill upper 24 bits!! !!• store byte from register1 to memory location!…is copied to “sign-extend”!xxxx xxxx xxxx xxxx xxxx xxxx!byte"loaded!This bit! xzzz zzzz!lb!reg1, offset(reg #)!sb!reg1, offset(reg #)!Electrical and Computer Engineering University of California, Santa Barbara!April 16, 2009!Lecture 6: Logic & Shift Instructions, Bytes and Overflow!7!Review: Overflow in Arithmetic!• Some languages detect overflow (Ada), some donʼt (C)!• MIPS solution is 2 kinds of arithmetic instructions to recognize 2 choices:!• add (add), add immediate (addi) and subtract (sub) cause overflow to be detected!• add unsigned (addu), add immediate unsigned (addiu) and subtract unsigned (subu) do not cause overflow detection!• Compiler selects appropriate arithmetic!• MIPS C compilers produce$addu, addiu, subu!3 Electrical and Computer Engineering University of California, Santa Barbara!April 23, 2009!Lecture 7: Procedures!8!C functions!main() { int a,b,c; ... c = sum(a,b); /*a,b,c:$s0,$s1,$s2*/ ... } /* really dumb sum function */ int sum(int x, int y){ return x+y; } What information must"compiler/programmer "keep track of?!What instructions can !accomplish this?!Electrical and Computer Engineering University of California, Santa Barbara!April 23, 2009!Lecture 7: Procedures!9!Function Call Bookkeeping!• Registers play a major role in keeping track of information for function calls!• Register conventions:!• Return address !$ra!• Arguments !$a0, $a1, $a2, $a3!• Return value !$v0, $v1!• Local variables !$s0, $s1, … , $s7!• The stack is also used; more later!Electrical and Computer Engineering University of California, Santa Barbara!April 23, 2009!Lecture 7: Procedures!10!Instruction Support for Functions! ... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } address"1000 "1004 "1008 "1012 "1016 C!M"I"P"S!In MIPS, all instructions are 4 bytes, and stored in memory just like data. So here we show the addresses of where the programs are stored.!Electrical and Computer Engineering University of California, Santa Barbara!April 23, 2009!Lecture 7: Procedures!11!Instruction Support for Functions! ... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } address"1000 !add !$a0,$s0,$zero !# x = a"1004 !add !$a1,$s1,$zero !# y = b "1008 !addi !$ra,$zero,1016 !#$ra=1016"1012 !j !sum !#jump to sum"1016 ! ...!!2000 sum: add !$v0,$a0,$a1"2004 !jr !$ra !# new instruction C!M"I"P"S!4 Electrical and Computer Engineering University of California, Santa Barbara!April 23, 2009!Lecture 7: Procedures!12! ... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } address"1000 !add !$a0,$s0,$zero !# x = a"1004 !add !$a1,$s1,$zero !# y = b "1008 !addi !$ra,$zero,1016 !#$ra=1016"1012 !j !sum !#jump to sum"1016 ! ...!!2000 sum: add !$v0,$a0,$a1"2004 !jr !$ra !# new instruction Instruction Support for Functions!C!M"I"P"S!Question: Why use jr here? Why not simply use j? Answer: sum might be called by many functions, so can’t return to a fixed place. The calling proc to sum must be able to say when done, “return here” somehow.


View Full Document

UCSB ECE 15 - COMPUTER ORGANIZATION

Documents in this Course
Load more
Download COMPUTER ORGANIZATION
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view COMPUTER ORGANIZATION and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view COMPUTER ORGANIZATION 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?