1 / 34

Subroutines

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.

Download Presentation

Subroutines

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Subroutines 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. 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. 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_b sub_b: save %sp, -64, %sp ... ... ... ret restore ! AFTER ret 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. 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). 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 Assembly Language

  8. SPARC Register Window Assembly Language

  9. Effect of Save Instruction Assembly Language

  10. Effect of Save Instruction Assembly Language

  11. Effect of Save Instruction Before “Save” After “Save” 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. 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. Assembly Language

  14. Register Window Overflow 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. Assembly Language

  16. Passive Arguments Assembly Language

  17. 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

  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; 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 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; 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 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”. 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 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). 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. Assembly Language

  26. Passing many Arguments(main stack) Assembly Language

  27. Passing many Arguments 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 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. ... Assembly Language

  30. 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

  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) 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. 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 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 Assembly Language

More Related