1 / 17

10/8: Lecture Topics

10/8: Lecture Topics. C Brainteaser from last time More on Procedure Call More than four arguments A recursive example Starting a Program Exercise 3.2 from H+P. 5+ Arguments. Up to four arguments can be passed in registers $a0-$a3 To pass more than four, Push them on the stack

Download Presentation

10/8: Lecture Topics

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. 10/8: Lecture Topics • C Brainteaser from last time • More on Procedure Call • More than four arguments • A recursive example • Starting a Program • Exercise 3.2 from H+P

  2. 5+ Arguments • Up to four arguments can be passed in registers $a0-$a3 • To pass more than four, • Push them on the stack • Set the frame pointer ($fp) to point to them • The frame pointer provides a stable base register for the duration of the procedure

  3. Stack Allocation $sp Low address Local vars. Callee’s stack frame Saved $s regs. Saved $ra Saved args $fp $sp $sp Caller’s stack frame Caller’s stack frame $fp $fp Before During After High address

  4. Recursive Procedure Call • A recursive procedure calls itself • With the stack, no more difficult than nested procedure call int fact(int n) { if(n < 1) return 1; else return (n * fact(n-1)); }

  5. Factorial Implementation fact: subi $sp, $sp, 8 sw $ra, 4($sp) sw $a0, 0($sp) slt $t0, $a0, 1 beq $t0, $zero, L1 add $v0, $zero, 1 add $sp, $sp, 8 jr $ra L1: sub $a0, $a0, 1 jal fact lw $a0, 0($sp) lw $ra, 4($sp) add $sp, $sp, 8 mult $v0, $a0, $v0 jr $ra

  6. Starting a Program • Two phases from source code to execution • Compile time • compiler • assembler • linker • Run time • loader

  7. Compile Time • You’re experts on compiling from source to assembly • Two parts to translating from assembly to machine language: • Instruction encoding • Translating labels to addresses • Label translations go in the symbol table

  8. Modular Program Design • Small projects may use only one file • Any time any one line changes, recompile and reassemble the whole thing • For larger projects, recompilation time is significant • Solution: split project into modules • compile and assemble modules separately • link the object files

  9. The Linker • The linker’s job is to “stitch together” the object files: 1. Place the data modules in memory 2. Determine the addresses of data and labels 3. Match up references between modules

  10. Placing Modules in Memory • Link a word processor application: text Editing text text static data Spell checking static data static data Paperclip

  11. Determining Addresses • Some addresses change during memory layout • Modules were compiled in isolation • Absolute addresses must be relocated text text

  12. Resolving References • The editing module calls a routine from the spell checker • That symbol is unresolved at compile time • The linker matches unresolved symbols to locations in other modules

  13. Libraries • Some code is used so often, it is bundled into libraries for common access • Libraries contain most of the code you use but didn’t write: e.g., printf() • Library code is (often) merged with yours at link time

  14. The Executable • End result of compiling, assembling, and linking: the executable • Contains: • Header, listing the lengths of the other segments • Text segment • Static data segment • Potentially other segments, depending on architecture & OS conventions

  15. Run Time • We’ll learn a lot more about this during the OS part of the course • In a nutshell: • Some dynamic linking may occur • The segments are loaded into memory • The OS transfers control to the program and it runs

  16. Exercise 3.2 • 25% Correct: computes the mode and the number of times the mode occurs • 50% Part way there • Computes the number of times the A[4999] occurs • Some other computation involving looping over the array • 25% Incorrect or no answer

  17. Key Observations add $a1, $a1, $a1 add $v0, $t5, $zero add $a1, $a1, $a1 add $v1, $t4, $zero add $v0, $zero, $zero next: addi $t0, $t0, 4 add $t0, $zero, $zero bne $t0, $a1, outer outer: add $t4, $a0, $t0 lw $t4, 0($t4) add $t5, $zero, $zero add $t1, $zero, $zero inner: add $t3, $a0, $t1 lw $t3, 0($t3) bne $t3, $t4, skip addi $t5, $t5, 1 skip: addi $t1, $t1, 4 bne $t1, $a1, inner slt $t2, $t5, $v0 bne $t2, $zero, next

More Related