Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.

Similar presentations

Presentation on theme: " Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused."— Presentation transcript:



3  Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused  Procedures allow the programmer to concentrate on one portion of the code at a time › parameters act as barriers between the procedure and the rest of the program and data, allowing the procedure to be passed values (arguments) and to return values (results)





8  Main routine (caller) places parameters in a place where the procedure (callee) can access them › $a0 - $a3: four argument registers  Caller transfers control to the callee  Callee acquires the storage resources needed  Callee performs the desired task  Callee places the result value in a place where the caller can access it › $v0 - $v1: two value registers for result values  Callee returns control to the caller › $ra: one return address register to return to the point of origin





13  $0: No Change. Always 0.  $s0-$s7: Restore if you change. Very important, that’s why they’re called saved registers. If the callee changes these in any way, it must restore the original values before returning.  $sp: Restore if you change. The stack pointer must point to the same place before and after the jal call, or else the caller won’t be able to restore values from the stack.  HINT -- All saved registers start with S!

14  $ra: Can Change. The jal call itself will change this register. Caller needs to save on stack if nested call.  $v0-$v1: Can Change. These will contain the new returned values.  $a0-$a3: Can change. These are volatile argument registers. Caller needs to save if they’ll need them after the call.  $t0-$t9: Can change. That’s why they’re called temporary: any procedure may change them at any time. Caller needs to save if they’ll need them afterwards.

15  Suppose we want a procedure ‘sprod’ that returns x1*y1 + x2*y2.  Abstractly we can think of this as: sprod(x1,y1,x2,y2){ a = prod(x1,y1);#x1*y1 b = prod(x2,y2);#x2*y2 return add2(a,b);#a + b }  Procedure ‘sprod’ calls ‘prod’ and ‘add2’.

16  jal procAddress › causes $31 to contain the next address  So each time that a procedure is called (eg, when ‘prod’ is called inside ‘sprod’) › $31 gets overwritten (‘clobbered’)  so that the return address for the outer procedure is no longer available!  Stacks can get us out of this problem.

17  When a C program is run, there are 3 important memory areas allocated: › Static: Variables declared once per program, cease to exist only after execution completes. E.g., C globals › Heap: Variables declared dynamically › Stack: Space to be used by procedure during execution; this is where we can save register values


19  A stack is used to store the sequence of return addresses when there are nested calls  The calling (outer) procedure › pushes address in $31 onto stack › calls the procedure ($31 contains return address) › pops stack to restore $31 (after procedure returns)  The called procedure... before returning › if it has called another procedure, it overwrites $31 with a pop from the stack › jr $31


21  Step-1: Pass the arguments: The first four arguments (arg0-arg3) are passed in registers $a0-$a3 Remaining arguments are pushed onto the stack (in reverse order arg5 is at the top of the stack).  Step-2: Save caller-saved registers Save registers $t0-$t9 if they contain live values at the call site.  Step-3: Execute a jal instruction.

22  Step-1: Establish stack frame. Subtract the frame size from the stack pointer. subi $sp, $sp, Typically, minimum frame size is 32 bytes (8 words).  Step-2: Save callee saved registers in the frame. Register $fp is always saved. Register $ra is saved if routine makes a call. Registers $s0-$s7 are saved if they are used.  Step-3: Establish Frame pointer Add the stack - 4 to the address in $sp  addi $fp, $sp, - 4

23  Step-1: Put returned values in registers $v0, [$v1]. (if values are returned)  Step-2: Restore callee-saved registers. Restore $fp and other saved registers. [$ra, $s0 - $s7]  Step-3: Pop the stack Add the frame size to $sp. addi $sp, $sp,  Step-4: Return Jump to the address in $ra. jr $ra

24 f 0 = 1; f -1 = -1 f n = f n-1 + f n-2 f = 1, 1, 2, 3, 5, 8, 13, …













37 #include int main (int argc, char *argv[]) { int i; int sum = 0; for (i = 0; i <= 100; i = i + 1) sum = sum + i * i; printf ("The sum from 0.. 100 is %d\n", sum); }

38  The steps › Software design is translated into source code › Source code is entered using an editor, making source files › Source files are assembled or compiled producing object files › Object files are linked into an executable file › Executable files are loaded into memory and then run Executable Object files Assembler source Program Loaded Into memory Result Of Careful Software design

39 Types of assembly lines:  Machine instruction  sw $ra,20($sp)  Pseudoinstruction › Data-definition .asciiz “The sum from” › Assembler directive .text .globl main .align 0  Label main: loop:.text.align2.globlmain main: subu$sp, $sp, 32 sw$ra, 20($sp) sd$a0, 32($sp) sw$0, 24($sp) sw$0, 28($sp) loop: lw$t6, 28($sp) mul$t7, $t6, $t6 lw$t8, 24($sp) addu$t9, $t8, $t7 sw$t9, 24($sp) addu$t0, $t6, 1 sw$t0, 28($sp) ble$t0, 100,loop la$a0, str lw$a1, 24($sp) jalprintf move$v0, $0 lw$ra, 20($sp) addu$sp, $sp, 32 jr$ str:.asciiz"The sum from 0.. 100 is %d\n" This Is what You write

40 addiu$29, $29, -32 sw$31, 20($29) sw$4, 32($29) sw$5, 36($29) sw$0, 24($29) sw$0, 28($29) lw$14, 28($29) lw$24, 24($29) multu$14, $14 addiu$8, $14, 1 slti$1, $8, 101 sw$8, 28($29) mflo$15 addu$25, $24, $15 bne$1, $0, -9 sw$25, 24($29) lui$4, 4096 lw$5, 24($29) jal1048812 addiu$4, $4, 1072 lw$31, 20($29) addiu$29, $29, 32 jr$31 move$2, $0 You sometimes read This in the debugger, Especially when you Are learning

41 00100111101111011111111111100000 10101111101111110000000000010100 10101111101001000000000000100000 10101111101001010000000000100100 10101111101000000000000000011000 10101111101000000000000000011100 10001111101011100000000000011100 10001111101110000000000000011000 00000001110011100000000000011001 00100101110010000000000000000001 00101001000000010000000001100101 10101111101010000000000000011100 00000000000000000111100000010010 00000011000011111100100000100001 00010100001000001111111111110111 10101111101110010000000000011000 00111100000001000001000000000000 10001111101001010000000000011000 00001100000100000000000011101100 00100100100001000000010000110000 10001111101111110000000000010100 00100111101111010000000000100000 00000011111000000000000000001000 00000000000000000001000000100001 If you can read this You are a computer Not for human consumption

42  The object file header describes the size and position of the other pieces of the file.  The text segment › contains the machine language code for routines in the source file. These routines may be unexecutable because of unresolved references.  The data segment › contains a binary representation of the data in the source file. The data also may be incomplete because of unresolved references to labels in other files.  The relocation information › identifies instructions and data words that depend on absolute addresses. These references must change if portions of the program are moved in memory  The symbol table › associates addresses with external labels in the source file and lists unresolved references.  The debugging information › contains a concise description of the way in which the program was compiled, so a debugger can find which instruction addresses correspond to lines in a source file and print the data structures in readable form.


Download ppt " Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused."

Similar presentations

Ads by Google