Docsity

UCSB Lecture 7: Electrical & Computer Engineering Procedures, Slides of Electrical and Electronics Engineering

Toggle sidebar
bg1
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!
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download UCSB Lecture 7: Electrical & Computer Engineering Procedures and more Slides Electrical and Electronics Engineering in PDF only on Docsity!

ECE 15B COMPUTER ORGANIZATION

Dr. Rahul Singh April 23, 2009

Lecture 7

Procedures

Lecture 7: Procedures^ Electrical and Computer Engineering University of California, Santa Barbara April 23, 2009^1

Announcements

• Project

• Due Friday May 1 by 5:00 pm

• Start early

• Quiz

• Graded

• Will return in next class

Electrical and Computer Engineering Lecture 7: Procedures (^) University of California, Santa Barbara April 23, 2009^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,reg

or reg1, reg2,reg

andi reg1, reg2,immediate

ori reg1, reg2,immediate

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: Lecture 6: Logic & Shift Instructions, Bytes and Overflow April 16, 2009 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 Lecture 6: Logic & Shift Instructions, Bytes and Overflow Electrical and Computer Engineering University of California, Santa Barbara April 16, 2009 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 reg
  • Shift right logical
  • Shifts reg2 right by immediate number of bits, fills emptied bits with 0ʼs and places result in reg
  • Shift right arithmetic
  • Shifts reg2 right by immediate number of bits, fills emptied bits by sign extending and places result in reg
  • 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 This bit loaded 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

Lecture 7: Procedures^ Electrical and Computer Engineering University of California, Santa Barbara April 23, 2009^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. Lecture 7: Procedures^ Electrical and Computer Engineering University of California, Santa Barbara April 23, 2009^13 Instruction Support for Functions

  • Single instruction to jump and save return address
    • jump and link (jal)
  • Not using jal:
  • Using jal :
  • Why have a jal_?_
    • Make the common case fast:
      • function calls are very common.
    • Also, you donʼt need to know where the code is loaded into memory with jal. 1008 addi $ra,$zero, 1016 # $ra = 1016 1012 j sum #go to sum 1008 jal sun # $ra=1012, go to sum Electrical and Computer Engineering Lecture 7: Procedures (^) University of California, Santa Barbara April 23, 2009^14 Instruction Support for Functions
  • Syntax for jal (jump and link) is same as for j (jump):
  • jal should really be called laj for “link and jump”:
  • Step 1 (link)
  • Save address of next instruction into $ra
  • (Why next instruction? Why not current one?)
  • Step 2 (jump)
  • Jump to the given label

jal label

Electrical and Computer Engineering Lecture 7: Procedures (^) University of California, Santa Barbara April 23, 2009^15 Instruction Support for Functions

  • Syntax for jr (jump register):
    • jr instruction provides a register containing an address to jump to - Instead of providing a label to jump to - Only useful if we know exact address to jump
  • Very useful for function calls
    • jal stores return address in register ($ra)
    • jr $ra jumps back to that address

jr register

Lecture 7: Procedures^ Electrical and Computer Engineering University of California, Santa Barbara April 23, 2009^16 Nested Procedures int sumSquare(int x, int y) { return mult(x,x)+ y; }

  • Something called sumSquare function
  • Now sumSquare is calling mult function
    • So thereʼs a value in $ra that sumSquare will jump back to - But this will be overwritten by the call to mult.
    • Need to save sumSquare return address before call to mult. Lecture 7: Procedures^ Electrical and Computer Engineering University of California, Santa Barbara April 23, 2009^17 Nested Procedures
  • In general, may need to save some other info in addition to $ra.
  • When a C program is ran, there are 3 important memory areas allocated:
  • Static : Variables declared once per program, cease to exist only after execution completes.
  • Example: C globals
  • Heap : Variables declared dynamically
  • Stack: Space to be used by procedure during execution; this is where we can save register values Electrical and Computer Engineering Lecture 7: Procedures (^) University of California, Santa Barbara April 23, 2009^18 C Memory Allocation 0 Address ∞ Stack Space for saved procedure information $sp stack pointer Variables declared once per program Static Code^ Program Explicitly created space, e.g., malloc(); C pointers Heap Electrical and Computer Engineering Lecture 7: Procedures (^) University of California, Santa Barbara April 23, 2009^19 Using the Stack
  • So we have a register $sp which always points to the last used space in the stack.
  • To use stack, we decrement this pointer by the amount of space we need and then fill it with info.
  • So, how do we compile this? int sumSquare(int x, int y) { return mult(x,x)+ y; }

Lecture 7: Procedures^ Electrical and Computer Engineering University of California, Santa Barbara April 23, 2009^24 Register Conventions

  • Calle r
    • the calling function
  • Calle e
    • the function being called
  • When callee returns from executing, the caller needs to know which registers may have changed and which are guaranteed to be unchanged.
  • Register Conventions
    • A set of generally accepted rules as to which registers will be unchanged after a procedure call (jal) and which may be changed. Lecture 7: Procedures^ Electrical and Computer Engineering University of California, Santa Barbara April 23, 2009^25 Saved Register Conventions
  • $
  • No Change
  • Always 0.
  • $s0-$s
  • Restore if you change.
  • Very important, thatʼs why theyʼre called saved registers.
  • If the callee changes these in any way, it must restore the original values before returning.
  • $sp
  • Restore if you change.
  • The stack pointer must point to the same place before and after the jal call
  • Or else the caller wonʼt be able to restore values from the stack. HINT -- All saved registers start with S Electrical and Computer Engineering Lecture 7: Procedures (^) University of California, Santa Barbara April 23, 2009^26 Volatile Register Conventions
  • $ra
    • Can Change.
    • The jal call itself will change this register.
    • Caller needs to save on stack if nested call.
  • $v0-$v
    • Can Change
    • These will contain the new returned values.
  • $a0-$a
    • Can change.
    • These are volatile argument registers.
    • Caller needs to save if theyʼll need them after the call.
  • $t0-$t
    • Can change.
    • Called temporary: any procedure may change them at any time.
    • Caller needs to save if theyʼll need them afterwards. Electrical and Computer Engineering Lecture 7: Procedures (^) University of California, Santa Barbara April 23, 2009^27 Register Conventions
  • What do these conventions mean?
  • If function R calls function E , then
  • Function R must save any temporary registers that it may be using onto the stack before making a jal call.
  • Function E must save any S (saved) registers it intends to use before garbling up their values
  • Remember : Caller/callee need to save only temporary/saved registers they are using, not all registers.

Lecture 7: Procedures^ Electrical and Computer Engineering University of California, Santa Barbara April 23, 2009^28

  • Prologue
    • Save other registers if needed
  • Body (...call other functions…)
  • Epilogue
    • Restore other regs if need be ra memory Basic Structure of a Function entry_label: addi $sp,$sp, -framesize sw $ra,framesize-4($sp) # $save $ra lw $ra, framesize-4($sp) # restore $ra addi $sp,$sp, framesize jr $ra Lecture 7: Procedures^ Electrical and Computer Engineering University of California, Santa Barbara April 23, 2009^29 Example main() { int i,j,k,m; /* i-m:$s0-$s3 */ ... i = mult(j,k); ... m = mult(i,i); ... } int mult (int mcand, int mlier){ int product; product = 0; while (mlier > 0) { product += mcand; mlier -= 1; } return product;} Electrical and Computer Engineering University of California, Santa Barbara __start:... . add $a0,$s1,$0 # arg0 = j" . add $a1,$s2,$0 # arg1 = k " . jal mult # call mult" . add $s0,$v0,$0 # i = mult()" ... . add $a0,$s0,$0 # arg0 = i . add $a1,$s0,$0 # arg1 = I . jal mult # call mult . add $s3,$v0,$0 # m = mult()" ... done: Lecture 7: Procedures April 23, 2009^30 Example
  • Notes:
  • main function ends with done
  • not $jr $ra, so no need to save $ra onto stack
  • All variable in main function are saved registers
  • no need to save these onto stack Electrical and Computer Engineering Lecture 7: Procedures (^) University of California, Santa Barbara April 23, 2009^31 mult: add $t0,$0,$0 # prod= loop: slt $t1,$0,$a1 # mlr > 0?" . beq $t1,$0,Fin # no=>Fin" add $t0,$t0,$a0 # prod+=mc" addi $a1,$a1,-1 # mlr-=1" j loop # goto loop Fin: add $v0,$t0,$0 # $v0=prod . jr $ra # return Example int mult (int mcand, int mlier){ int product = 0; while (mlier > 0) { product += mcand; mlier -= 1; } return product;}