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

sidan 1
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Sidan 1

Computer Engineering ActivationConcept page 1

Problem:

! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! ! Bal bell ! 3 Jr $31 ! ! ! ! ! ! ! Jr $31 !

What happend here? What will happen here?

Computer Engineering ActivationConcept page 2

The activation concept

Main

  • 1. Executing program, wants “bell”
  • 2. Creates son,

Tells him “bell”, Falls asleep

Computer Engineering ActivationConcept page 3

The activation concept

Main

  • 1. Executing program, wants “bell”
  • 2. Creates son,

Tells him “bell”, Falls asleep

  • 3. Asleep

Son

  • 1. Wakes up,

starts executing “bell”, wants “help”

Computer Engineering ActivationConcept page 4

The activation concept

Main

  • 1. Executing program, wants “bell”
  • 2. Creates son,

Tells him “bell”, Falls asleep

  • 3. Asleep

Son

  • 1. Wakes up,

starts executing “bell”, wants “help”

  • 2. Creates son, tells him help, falls asleep

Grandson

Computer Engineering ActivationConcept page 5

The activation concept

Main

  • 1. Executing program, wants “bell”
  • 2. Creates son,

Tells him “bell”, Falls asleep

  • 3. Asleep

Son

  • 1. Wakes up,

starts executing “bell”, wants “help”

  • 2. Creates son, tells him help, falls asleep
  • 3. Asleep

Grandson

  • 1. Wakes up.

starts executing “help”

Computer Engineering ActivationConcept page 6

The activation concept

Main

  • 1. Executing program, wants “bell”
  • 2. Creates son,

Tells him “bell”, Falls asleep

  • 3. Asleep

Son

  • 1. Wakes up,

starts executing “bell”, wants “help”

  • 2. Creates son, tells him help, falls asleep
  • 3. Asleep

Grandson

  • 1. Wakes up.

starts executing “help”

  • 2. Finished. Vanishes, Wakes up his parent
slide-2
SLIDE 2

Sidan 2

Computer Engineering ActivationConcept page 7

The activation concept

Main

  • 1. Executing program, wants “bell”
  • 2. Creates son,

Tells him “bell”, Falls asleep

  • 3. Asleep

Son

  • 1. Wakes up,

starts executing “bell”, wants “help”

  • 2. Creates son, tells him help, falls asleep
  • 3. Asleep
  • 4. Finished, Vanishes, wakes up his parent

Computer Engineering ActivationConcept page 8

The activation concept

Main

  • 1. Executing program, wants “bell”
  • 2. Creates son,

Tells him “bell”, Falls asleep

  • 3. Asleep
  • 4. Executing

Computer Engineering ActivationConcept page 9

The main points:

■ We talk about activations,

not the code being executed.

■ Last in - first out

– implies a stack

■ OK for different activations

to share the same instructions

Computer Engineering ActivationConcept page 10

A parents responsibilities to his unborn son:

■ Be prepared for the son to trash $tx-registers. ■ Put input arguments into $a0 - $a3. ■ Give him a return address (by Bal- instr.). ■ Tell him which code to execute (also by Bal).

Computer Engineering ActivationConcept page 11

The son’s responsibility to his sleeping parent:

■ Leave the stack like he found it. ■ Return results (if any) into registers $v0 - $v1. ■ Leave the $ax-registers like he found them. ■ OK to change $tx-registers and not restore them.

Computer Engineering ActivationConcept page 12

The activations responsibility to himself

■ Need space for local variables?

– Create space on stack. Use $tx registers as scratchpads.

■ Want to change $ax-register?

– Save the incoming values into local variables.

■ Want to create a son?

– Protect the $tx-registers. – Protect the return address ($31).

by saving them on the stack

slide-3
SLIDE 3

Sidan 3

Computer Engineering ActivationConcept page 13

What does one activation own?

■ Return address to his parent. ■ His incoming parameters. ■ A place to put his results. ■ Some local variables. ■ Which code to execute (his PC).

Called his “activation record”

Computer Engineering ActivationConcept page 14

Stack the activation records:

Main (asleep) Son (asleep) Grandson saved activation record “main” saved activation record “son” used activation record “grandson”

The concept gives unique context.

Computer Engineering ActivationConcept page 15

The user stack

■ $sp points to top- of- stack User stack (part of the data memory)

$sp

Computer Engineering ActivationConcept page 16

The user stack

■ Stack grows toward lower addresses. ■ Basic stack operations:

Push $t5 onto stack Pop from stack into $a2 Addi $sp $sp -4 Lw $a2 0($sp) Sw $t5 0($sp) Addi $sp $sp 4

Computer Engineering ActivationConcept page 17

Wrong!!!!!

Push $t5 onto stack Pop from stack into $a2 Sw $t5 -4($sp) Addi $sp $sp 4 Addi $sp $sp -4 Lw $a2 -4($sp) This method writes Never use a negative beyond T.O.S first, displacement with then fixes $sp respect to T.O.S.

reason: interrupts.

Computer Engineering ActivationConcept page 18

Framepointer

Var2 Var1 Old fp Return addr Caller’s stack

<= $fp points here <= $sp points here 0($fp) 4($fp)

  • 4($fp)
  • 8($fp)

Low address High address

slide-4
SLIDE 4

Sidan 4

Computer Engineering ActivationConcept page 19

Example

N

Compute Σ i

i=1

Define as recursive definition: 1 if N = 1 sum(N) = N + sum(N-1) otherwise

Computer Engineering ActivationConcept page 20

Pseudocode

Procedure sum(N:integer):integer; if (N=1) then return (1) else T := N + sum(N-1); return (T) end;

Computer Engineering ActivationConcept page 21

Where do we start?

■ Parameter comes in in $a0. ■ Return address in $31. ■ This activation will probably need a son

(because of the recursion) so:

– Local variables. – save $31. – save $a0. – the variable T?

Computer Engineering ActivationConcept page 22

The Code

sum: addi $sp $sp -4 #store return address on stack sw $31 0($sp) addi $sp $sp -4 #store incoming parameter on stack sw $a0 0($sp) if:

  • ri

$t0 $r0 1 #test: fixed point? bne $a0 $t0 else then:

  • ri

$v0 $r0 1 b exit else: addi $a0 $a0 -1 #sum(N-1) in $v0, original $a0 lost bal sum #recursive call to sum lw $t0 0($sp) #retrive the incoming parameter value add $v0 $v0 $t0 #$v0 has N + T exit: lw $31 4($sp) #restore return address addi $sp $sp 8 #Stack cleaned jr $31 #end activation

Computer Engineering ActivationConcept page 23

Procedure Call

1.

Place parameters in a place where the procedure can access them ($a0 - $a3)

2.

Transfer control to the procedure (bal bell)

3.

Acquire the storage resources needed for the procedure.

4.

Perform the desired task.

5.

Place the result value in a place where the calling program can access it. ($v0 - $v1)

6.

Return control to the point of origin.(jr $31)