1 / 43

Computer Architecture Discussion

Computer Architecture Discussion. Lecture # 4 Procedures. Question # 30. Procedures purpose. Procedures (subroutines) allow the programmer to structure programs making them : easier to understand and debug and allowing code to be reused

ellery
Download Presentation

Computer Architecture Discussion

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. Computer Architecture Discussion Lecture # 4 Procedures

  2. Question # 30

  3. Procedures purpose • 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)

  4. Procedures

  5. Call / Return Sequence

  6. Details of JAL and JR

  7. Instructions for Procedures

  8. Six Steps in Execution of a Procedure • 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

  9. Parameter Passing

  10. Parameter Passing

  11. Stack Frame

  12. Preserving Registers

  13. Register Conventions - saved • $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. Register Conventions - volatile • $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. Procedures Calling Procedures • 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. Problem When Procedure call Procedures • 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. C program memory Allocation • 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

  18. C program memory Allocation

  19. How stacks are used? • 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

  20. Register / Stack Management

  21. Calling Procedure • • 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. Called Routine • Step-1: Establish stack frame. 􀂾 Subtract the frame size from the stack pointer. subi $sp, $sp, <frame-size> 􀂾 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 <frame size> - 4 to the address in $sp • addi $fp, $sp, <frame-size> - 4

  23. On return from a call • 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, <frame-size> • Step-4: Return 􀂾 Jump to the address in $ra. jr $ra

  24. Fibonacci Procedure (c) f0 = 1; f-1 = -1 fn = fn-1 + fn-2 f = 1, 1, 2, 3, 5, 8, 13, …

  25. Fibonacci Procedure(MIPS)

  26. Recursive algorithms/procedures

  27. Binary search

  28. Binary search (example, needle = 35)

  29. MIPS: Recursive procedures

  30. MIPS: Save/restore $ra in recursive procedure .text ... # main program ... jal proc # invoke procedure ($ra = F1) F1: ... # procedure proc: save $ra ... jal proc # recursive call ($ra = F2) F2: ... restore $ra j $ra # will jump to F1 • Each invocation of proc needs its own place to save $ra—the save location will otherwise be overwritten in the recursive call!

  31. Selection Sort

  32. Recursive binary searchProcedure

  33. Starting a Program

  34. Starting a Program

  35. Starting a Program

  36. The assembly process

  37. The program and the process • #include <stdio.h> • 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. Files associated with the assembly process source • 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 Result Of Careful Software design Program Loaded Into memory Executable Object files Assembler source

  39. Symbolicassemblycode .text .align 2 .globl main 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) jal printf move$v0, $0 lw $ra, 20($sp) addu $sp, $sp, 32 jr $ra .data .align 0 str: .asciiz "The sum from 0 .. 100 is %d\n" This Is what You write 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:

  40. Raw assembly code 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) jal 1048812 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 Bit patterns If you can read this You are a computer Not for human consumption

  42. Parts of an object file • The object file header describes the size and position of the other pieces ofthe 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.

  43. What the linker does

More Related