340 likes | 360 Views
Subroutines. 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.
E N D
Subroutines Assembly Language
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. Assembly Language
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. Assembly Language
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_b sub_b: save %sp, -64, %sp ... ... ... ret restore ! AFTER ret Assembly Language
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. Assembly Language
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). Assembly Language
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 Assembly Language
SPARC Register Window Assembly Language
Effect of Save Instruction Assembly Language
Effect of Save Instruction Assembly Language
Effect of Save Instruction Before “Save” After “Save” Assembly Language
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. Assembly Language
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. Assembly Language
Register Window Overflow Assembly Language
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. Assembly Language
Passive Arguments Assembly Language
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); } Our Fifth Program Assembly Language
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; Assembly Language
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 Assembly Language
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; Assembly Language
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 Assembly Language
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”. Assembly Language
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 Assembly Language
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). Assembly Language
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. Assembly Language
Passing many Arguments(main stack) Assembly Language
Passing many Arguments Assembly Language
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 Assembly Language
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. ... Assembly Language
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; } Our Sixth Program Assembly Language
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) Assembly Language
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. Assembly Language
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 Assembly Language
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 Assembly Language