34
Natawut Nupairoj Assembly Language 1 Subroutines

Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Embed Size (px)

Citation preview

Page 1: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 1

Subroutines

Page 2: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 2

Subroutines

• A subroutine is a piece of program codes that performs some particular functions.– Function, Procedure, Method

• A subroutine must behave:– in-line execution: flow naturally.– no unintended side effects: must declare

explicitly which registers will be changed which will be not after the call.

– multiple arguments: should allow a caller to pass more than one argument.

Page 3: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 3

Subroutine

• Thus, CPU must provide:• Call/return mechanism.

– Save the address of the instruction being executed (pc) of the caller so that, when return, it can resume execution at the next instruction.

• A register protection mechanism.– How to save the values of the register before the call

and restore the values back when returns from calling.

• An argument passing convention.– An agreement between caller and callee on how

arguments are to be passed.

Page 4: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 4

Call/Return Mechanism

• Two special instructions: call and ret.• Use “call” to call for a particular subroutine.• Use “ret” to return from the subroutine.• Both have a delay slot.• Example:

.global sub_a

sub_a: save %sp, -96, %sp

...

call sub_b

nop

...

.global sub_bsub_b: save %sp, -64, %sp

...

...

...retrestore ! AFTER ret

Page 5: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 5

Register Saving

• Subroutine must explicitly declare which registers will be changed, which won’t.

• Subroutine also needs to use registers for computation.

• To reduce the side-effect (due to modifying registers), we will save values in registers before doing anything and restore back before return.

• In most CPUs, we will push/pop values in registers to/from the stack.

• In SPARC, we have save/restore instructions.

Page 6: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 6

Register Saving

• Save: allocate space in stack and save registers (in special ways).

• Restore: deallocate space and restore registers (in special ways).

• Pushing/Poping registers to/from stack requires lots of time.

• In SPARC, we use the concept “Register Window” (performed by Save andRestore).

Page 7: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 7

SPARC Register

• SPARC has 8 global registers and 128 general registers.

• For general registers, only 24 registers are active or “mapped” at any time.

• Thus this makes 24 + 8 = 32 registers available.• The mapping mechanism is called “register

window”.• SPARC has two internal pointers:

– CWP: Current Window Pointer– WIM: Window Invalid Mark

Page 8: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 8

SPARC Register WindowCW P24

registers in% i0 - % i7

loca l% l0 - % l7

out% o0 - % o7

global% g0 - % g7

Un-mapped M apped

W IM

Page 9: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 9

Effect of Save Instruction

24registers in

% i0 - % i7

loca l% l0 - % l7

out% o0 - % o7

global% g0 - % g7

Un-mapped

M apped

CW P

W IM

Page 10: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 10

Effect of Save Instruction

in% i0 - % i7

loca l% l0 - % l7

out% o0 - % o7

in% i0 - % i7

loca l% l0 - % l7

out% o0 - % o7

Before "Save"

After "Save"

% fp

% sp % fp

% sp

Page 11: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 11

Effect of Save Instruction

S tack area fo r A

S tack area fo r A

S tack area fo r B

% fp

% sp

(% fp)

(% sp) % fp

% sp

in% i0 - % i7

local% l0 - % l7

out% o0 - % o7

in% i0 - % i7

local% l0 - % l7

out% o0 - % o7

Before "Save"

After "Save"

% fp

% sp % fp

% sp

Before “Save”

After “Save”

Page 12: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 12

Effect of Save Instruction

• Provide a new set of local registers.• Provide a new set of out registers.• Make the old “out” registers into the new “in”

registers.• In the process, the old %sp becomes the new

%fp.• Global registers remain unchanged.

Page 13: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 13

Register Window Overflow

• Since we have only 128 general registers, we can save at most 7 sets of registers.

• This means we can “save” 7 times without having to “restore”.

• What happen if we run out of registers ?• We save it to the main memory, provided in

the stack !!!• Remember, every time we allocate spaces in

stack with “save”, we always reserve 64 bytes to save registers.

Page 14: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 14

Register Window Overflow

CW P

7

5

4

3

2

1

0W IM

6

CW P

7

5

4

3

2

1

0

W IM

6

Save Registers

Local Vars

Page 15: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 15

Arguments to Subroutines

• How we can pass arguments to a subroutine ?• Remember, the “out” registers of the caller

become the “in” registers of the called subroutine.

• However, we can passed at most 6 registers (%o0-%o5) because %o6 is the stack pointer and %o7 contains the return address.

• The called subroutine accesses the arguments via the “in” registers.

Page 16: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 16

Passive Arguments

in% i0 - % i7

loca l% l0 - % l7

out% o0 - % o7

in% i0 - % i7

loca l% l0 - % l7

out% o0 - % o7

Before "Save"

After "Save"

% fp

% sp % fp

% sp

Page 17: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 17

Our Fifth Program

int mul(int a, int b)

{

int r;

register int i;

r = 0;

i = b;

while(i > 0) {

r = r + a;

i--;

}

return r;

}

main()

{

int r;

short x, y;

x = 10;

y = 30;

r = mul(x, y);

}

Page 18: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 18

Our Fifth Program

define(mul_a_r, i0)

define(mul_b_r, i1)

define(mul_r_s, -4)

define(mul_i_r, l0)

define(r_s, -4)

define(x_s, -6)

define(y_s, -8)

.global mul

mul: save %sp, (-64-4)&-8, %sp

clr %l1

st %l1, [%fp + mul_r_s] ! r = 0;

mov %mul_b_r, %mul_i_r ! i = b;

Page 19: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 19

Our Fifth Program

loop: cmp %mul_i_r, 0 ! start while loop

ble done

nop

ld [%fp + mul_r_s], %l1

add %l1, %mul_a_r, %l1 ! r + a

st %l1, [%fp + mul_r_s] ! r = r + a;

sub %mul_i_r, 1, %mul_i_r ! i--;

ba loop

nop

Page 20: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 20

Our Fifth Program

done: ld [%fp + mul_r_s], %i0 ! return r;

ret

restore ! After ret

.global main

main: save %sp, (-64-8)&-8, %sp

mov 10, %l0

sth %l0, [%fp + x_s] ! x = 10;

mov 30, %l0

sth %l0, [%fp + y_s] ! y = 30;

Page 21: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 21

Our Fifth Program

ldsh [%fp + x_s], %o0 ! prepare first arg.

ldsh [%fp + y_s], %o1 ! prepare second arg.

call mul

nop

st %o0, [%fp + r_s]

mov 1, %g1

ta 0

Page 22: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 22

Optimizing the Common Case

• Why does SPARC have 128 registers (which can provide upto 7 saves before overflowed) ?

• Why does SPARC have 8 “out” registers to pass arguments to a subroutine ?

• The SPARC designers studied real programs and conclude that these are quite common.

• We can have more registers, but it may cost more and we will not gain much improvement.

• This is called “optimizing the common case”.

Page 23: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 23

Return Values

• A subroutine that returns a value is called a function.

• We can store the returned value in the “in” registers of the called subroutine and they will become the “out” registers when return.

• In sub_a: In sub_b:... ...

call sub_b add %o0, %l2, %l3

nop mov %l3, %i0

st %o0, [%fp-4] ret

... restore

Page 24: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 24

Return Values

• Or we can use “restore” to return value to any register.

restore %l3, %l0, %i5

• This adds %l3 with %l0 (both from the called subroutine registers) then stores the result in %i5 of the calling subroutine (or after “restore”).

• However, typical common convention is to return value in %o0 (of the calling subroutine).

Page 25: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 25

Passing many Arguments

• In some subroutines, passing only 6 arguments may not be enough.

• If more than 6 arguments are required, we store the rest in the stack before calling.

• For example, main calls:sub1(1, 2, 3, 4, 5, 6, 7, 8);

• We put first 6 arguments in %o0-%o5.• Then put the last two arguments in the stack.• Thus, we must allocate more spaces before

calling.

Page 26: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 26

Passing many Arguments(main stack)

last tw o args

64 bytes to saveregisters

loca l vars.

64 bytes to saveregisters

loca l vars.

% fp % fp

% sp

% sp

% sp + 64 + 4

% sp + 64 + 8

Page 27: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 27

Passing many Arguments

last tw o args

64 bytes to saveregisters

loca l vars.

% fp

% sp

% fp + 64 + 4

% fp + 64 + 8

formain

64 bytes to saveregisters

loca l vars.

forsub1

Page 28: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 28

Passing many Arguments

main: save %sp, -64, %sp

...

add %sp, -8, %sp ! Allocate for 2 args.

mov 8, %o0

st %o0, [%sp + 8+64]

mov 7, %o0

st %o0, [%sp + 4+64]

mov 6, %o5

mov 5, %o4

mov 4, %o3

mov 3, %o2

mov 2, %o1

Page 29: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 29

Passing many Arguments

call _sub1

mov 1, %o0 ! Delay Slot

sub %sp, -8, %sp ! Release stack.

...

• In sub1:sub1: save %sp, (-92&-8), %sp

ld [%fp + 8+64], %o0 ! The last two args.

ld [%fp + 4+64], %o1

add %o0, %o1, %o0

add %i5, %o0, %o0 ! The sixth argument.

sub %o0, %i4, %o0 ! The fourth argument.

...

Page 30: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 30

Our Sixth Program

main()

{

int x, y;

x = 12;

y = x + 1;

x = sub1(2, 0, x, y, x+5, 10, 3, y);

}

sub1(int a, int b, int c, int d, int e, int f, int g, int h)

{

int r;

r = a + b + c + d + e + f + g + h;

return r;

}

Page 31: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 31

Our Sixth Program

define(x_s, -4) ! main

define(y_s, -8)

define(arg7_s, 64+4)

define(arg8_s, 64+8)

define(a_r, %i0) ! sub1

define(b_r, %i1)

define(c_r, %i2)

define(d_r, %i3)

define(e_r, %i4)

define(f_r, %i5)

define(g_s, arg7_s) ! 7th arg.

define(h_s, arg8_s) ! 8th arg.

define(r_s, -4)

Page 32: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 32

Our Sixth Program

.global main

main: save %sp, (-64-8)&-8, %sp

mov 12, %l0

st %l0, [%fp + x_s] ! x = 12;

add %l0, 1, %l0

st %l0, [%fp + y_s] ! y = x + 1;

add %sp, -8, %sp ! Reserve stack for

! the 7th and 8th args.

mov 2, %o0 ! The 1st arg.

mov 0, %o1 ! The 2nd arg.

ld [%fp + x_s], %o2 ! The 3rd arg.

ld [%fp + y_s], %o3 ! The 4th arg.

Page 33: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 33

Our Sixth Program

ld [%fp + x_s], %l0

add %l0, 5, %l0

mov %l0, %o4 ! The 5th arg.

mov 10, %o5 ! The 6th arg.

mov 3, %l0

st %l0, [%sp + arg7_s] ! 3 is the 7th arg.

ld [%fp + y_s], %l0

st %l0, [%sp + arg8_s] ! Y is the 8th arg.

call sub1

nop

st %o0, [%fp + x_s] ! Store result from sub1

sub %sp, -8, %sp ! Release stack

mov 1, %g1 ! Exit program

ta 0

Page 34: Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs

Natawut Nupairoj Assembly Language 34

Our Sixth Program.global sub1

sub1: save %sp, (-64-4)&-8, %sp

add %i0, %i1, %l0

add %l0, %i2, %l0

add %l0, %i3, %l0

add %l0, %i4, %l0

add %l0, %i5, %l0

ld [%fp + g_s], %l1 ! Read the 7th arg.

add %l0, %l1, %l0

ld [%fp + h_s], %l1 ! Read the 8th arg.

add %l0, %l1, %l0

st %l0, [%fp + r_s] ! Store result to r.

ld [%fp + r_s], %o0 ! return r;

ret

restore %o0, 0, %o0 ! After ret