The University of Adelaide, School of Computer Science

Similar presentations

Presentation on theme: "The University of Adelaide, School of Computer Science"— Presentation transcript:

1 The University of Adelaide, School of Computer Science11 April 2017CSF 2009The MIPS Assembly Language:Procedure Calls and ExamplesChapter 2Chapter 2 — Instructions: Language of the Computer

2 The University of Adelaide, School of Computer Science11 April 2017Procedure CallingSteps requiredPlace parameters in registersTransfer control to procedureAcquire storage for procedurePerform procedure’s operationsPlace result in register for callerReturn to place of callChapter 2 — Instructions: Language of the Computer — 2Chapter 2 — Instructions: Language of the Computer

4 Procedure Call InstructionsThe University of Adelaide, School of Computer Science11 April 2017Procedure Call InstructionsProcedure call: jump and linkjal ProcedureLabelAddress of following instruction put in $raJumps to target addressProcedure return: jump registerjr $raCopies $ra to program counterCan also be used for computed jumpse.g., for case/switch statementsChapter 2 — Instructions: Language of the Computer — 4Chapter 2 — Instructions: Language of the Computer

7 The University of Adelaide, School of Computer Science11 April 2017Procedure Example II(Multiple calls or Recursion)Procedures that call other proceduresFor nested call, caller needs to save on the stack:Its return addressAny arguments and temporaries needed after the callRestore from the stack after the callChapter 2 — Instructions: Language of the Computer — 7Chapter 2 — Instructions: Language of the Computer

10 The University of Adelaide, School of Computer Science11 April 2017Local Data on the StackLocal data allocated by calleee.g., C automatic variablesProcedure frame (activation record)Used by some compilers to manage stack storageChapter 2 — Instructions: Language of the Computer — 10Chapter 2 — Instructions: Language of the Computer

11 The University of Adelaide, School of Computer Science11 April 2017Memory LayoutText: program codeStatic data: global variablese.g., static variables in C, constant arrays and strings$gp initialized to address allowing ±offsets into this segmentDynamic data: heapE.g., malloc in C, new in JavaStack: automatic storageChapter 2 — Instructions: Language of the Computer — 11Chapter 2 — Instructions: Language of the Computer

19 Effect of Compiler OptimizationThe University of Adelaide, School of Computer Science11 April 2017Effect of Compiler OptimizationCompiled with gcc for Pentium 4 under LinuxChapter 2 — Instructions: Language of the Computer — 19Chapter 2 — Instructions: Language of the Computer

20 Effect of Language and AlgorithmThe University of Adelaide, School of Computer Science11 April 2017Effect of Language and AlgorithmChapter 2 — Instructions: Language of the Computer — 20Chapter 2 — Instructions: Language of the Computer

23 Comparison of Array vs. PtrThe University of Adelaide, School of Computer Science11 April 2017Comparison of Array vs. PtrMultiply “strength reduced” to shiftArray version requires shift to be inside loopPart of index calculation for incremented ic.f. incrementing pointerCompiler can achieve same effect as manual use of pointersInduction variable eliminationBetter to make program clearer and saferChapter 2 — Instructions: Language of the Computer — 23Chapter 2 — Instructions: Language of the Computer