sidan 1
play

Sidan 1 The activation concept The activation concept 1. Wakes up, - PDF document

Problem: The activation concept ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! ! Bal bell ! 3 Jr $31 ! ! ! ! What happend Main ! here? ! 1. Executing program, wants bell ! Jr $31 2. Creates son, Tells


  1. Problem: The activation concept ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! ! Bal bell ! 3 Jr $31 ! ! ! ! What happend Main ! here? ! 1. Executing program, wants “bell” ! Jr $31 2. Creates son, Tells him “bell”, ! Falls asleep What will happen here? Computer Engineering ActivationConcept page 1 Computer Engineering ActivationConcept page 2 The activation concept The activation concept Grandson 1. Wakes up, 1. Wakes up, Son Son starts executing “bell”, wants “help” starts executing “bell”, wants “help” 2. Creates son, tells him help, falls asleep Main Main 1. Executing program, wants “bell” 1. Executing program, wants “bell” 2. Creates son, 2. Creates son, Tells him “bell”, Tells him “bell”, Falls asleep Falls asleep 3. Asleep 3. Asleep Computer Engineering ActivationConcept page 3 Computer Engineering ActivationConcept page 4 The activation concept The activation concept Grandson Grandson 1. Wakes up. 1. Wakes up. starts executing “help” starts executing “help” 2. Finished. Vanishes, Wakes up his parent Son 1. Wakes up, Son 1. Wakes up, starts executing “bell”, wants “help” starts executing “bell”, wants “help” 2. Creates son, tells him help, falls asleep 2. Creates son, tells him help, falls asleep 3. Asleep 3. Asleep Main Main 1. Executing program, wants “bell” 1. Executing program, wants “bell” 2. Creates son, 2. Creates son, Tells him “bell”, Tells him “bell”, Falls asleep Falls asleep 3. Asleep 3. Asleep Computer Engineering ActivationConcept page 5 Computer Engineering ActivationConcept page 6 Sidan 1

  2. The activation concept The activation concept 1. Wakes up, Son starts executing “bell”, wants “help” 2. Creates son, tells him help, falls asleep 3. Asleep 4. Finished, Vanishes, wakes up his parent Main Main 1. Executing program, wants “bell” 1. Executing program, wants “bell” 2. Creates son, 2. Creates son, Tells him “bell”, Tells him “bell”, Falls asleep Falls asleep 3. Asleep 3. Asleep 4. Executing Computer Engineering ActivationConcept page 7 Computer Engineering ActivationConcept page 8 A parents responsibilities to his The main points: unborn son: ■ We talk about activations, ■ Be prepared for the son to trash $tx-registers. not the code being executed. ■ Put input arguments into $a0 - $a3. ■ Last in - first out – implies a stack ■ Give him a return address (by Bal- instr.). ■ OK for different activations ■ Tell him which code to execute (also by Bal). to share the same instructions Computer Engineering ActivationConcept page 9 Computer Engineering ActivationConcept page 10 The son’s responsibility to his The activations responsibility to sleeping parent: himself ■ Leave the stack like he found it. ■ Need space for local variables? – Create space on stack. Use $tx registers as scratchpads. ■ Return results (if any) into registers $v0 - $v1. ■ Want to change $ax-register? – Save the incoming values into local variables. ■ Leave the $ax-registers like he found them. ■ Want to create a son? ■ OK to change $tx-registers and not restore them. – Protect the $tx-registers. – Protect the return address ($31). by saving them on the stack Computer Engineering ActivationConcept page 11 Computer Engineering ActivationConcept page 12 Sidan 2

  3. What does one activation own? Stack the activation records: ■ Return address to his parent. Grandson used activation record “grandson” ■ His incoming parameters. Son (asleep) ■ A place to put his results. The concept gives saved activation unique context. record “son” ■ Some local variables. Main (asleep) ■ Which code to execute (his PC). saved activation record “main” Called his “activation record” Computer Engineering ActivationConcept page 13 Computer Engineering ActivationConcept page 14 The user stack The user stack ■ $sp points to top- of- stack ■ Stack grows toward lower addresses. ■ Basic stack operations: $sp Push $t5 onto stack Pop from stack into $a2 User stack (part of the Addi $sp $sp -4 Lw $a2 0($sp) data memory) Sw $t5 0($sp) Addi $sp $sp 4 Computer Engineering ActivationConcept page 15 Computer Engineering ActivationConcept page 16 Wrong!!!!! Framepointer Low address <= $sp points here -8($fp) Var2 Push $t5 onto stack Pop from stack into $a2 -4($fp) Var1 Sw $t5 -4($sp) Addi $sp $sp 4 <= $fp points here 0($fp) Old fp Addi $sp $sp -4 Lw $a2 -4($sp) 4($fp) Return addr This method writes Never use a negative beyond T.O.S first, displacement with Caller’s then fixes $sp respect to T.O.S. stack reason: interrupts. High address Computer Engineering ActivationConcept page 17 Computer Engineering ActivationConcept page 18 Sidan 3

  4. Example Pseudocode Procedure sum(N:integer):integer; N Compute Σ i if (N=1) then return (1) i=1 else Define as recursive definition: T := N + sum(N-1); return (T) 1 if N = 1 end; sum(N) = N + sum(N-1) otherwise Computer Engineering ActivationConcept page 19 Computer Engineering ActivationConcept page 20 The Code Where do we start? sum: addi $sp $sp -4 #store return address on stack sw $31 0($sp) addi $sp $sp -4 #store incoming parameter on stack ■ Parameter comes in in $a0. sw $a0 0($sp) ■ Return address in $31. if: ori $t0 $r0 1 #test: fixed point? bne $a0 $t0 else then: ori $v0 $r0 1 ■ This activation will probably need a son b exit else: addi $a0 $a0 -1 #sum(N-1) in $v0, original $a0 lost (because of the recursion) so: bal sum #recursive call to sum – Local variables. lw $t0 0($sp) #retrive the incoming parameter value – save $31. add $v0 $v0 $t0 #$v0 has N + T – save $a0. exit: lw $31 4($sp) #restore return address – the variable T? addi $sp $sp 8 #Stack cleaned jr $31 #end activation Computer Engineering ActivationConcept page 21 Computer Engineering ActivationConcept page 22 Procedure Call Place parameters in a place where the procedure can access 1. them ($a0 - $a3) Transfer control to the procedure (bal bell) 2. Acquire the storage resources needed for the procedure. 3. Perform the desired task. 4. Place the result value in a place where the calling program 5. can access it. ($v0 - $v1) Return control to the point of origin.(jr $31) 6. Computer Engineering ActivationConcept page 23 Sidan 4

More recommend