computer architecture discussion n.
Skip this Video
Loading SlideShow in 5 Seconds..
Computer Architecture Discussion PowerPoint Presentation
Download Presentation
Computer Architecture Discussion

Loading in 2 Seconds...

play fullscreen
1 / 43

Computer Architecture Discussion - PowerPoint PPT Presentation

  • Uploaded on

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Computer Architecture Discussion' - ellery

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
computer architecture discussion

Computer Architecture Discussion

Lecture # 4


procedures purpose
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)
six steps in execution of a procedure
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
register conventions saved
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!
register conventions volatile
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.
procedures calling procedures
Procedures Calling Procedures
  • Suppose we want a procedure ‘sprod’ that returns x1*y1 + x2*y2.
  • Abstractly we can think of this as:


a = prod(x1,y1); #x1*y1

b = prod(x2,y2); #x2*y2

return add2(a,b); #a + b


  • Procedure ‘sprod’ calls ‘prod’ and ‘add2’.
problem when procedure call procedures
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.
c program memory allocation
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

how stacks are used
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
calling procedure
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.
called routine
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
on return from a call
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

fibonacci procedure c
Fibonacci Procedure (c)

f0 = 1; f-1 = -1

fn = fn-1 + fn-2

f = 1, 1, 2, 3, 5, 8, 13, …

mips save restore ra in recursive procedure
MIPS: Save/restore $ra in recursive procedure



# 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!
the program and the process
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);
  • }
files associated with the assembly process
Files associated with the assembly process


  • 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



Careful Software




Into memory






symbolic assembly code


.align 2

.globl main


subu $sp, $sp, 32

sw $ra, 20($sp)

sd $a0, 32($sp)

sw $0, 24($sp)

sw $0, 28($sp)


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


.align 0


.asciiz "The sum from 0 .. 100 is %d\n"


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:
raw assembly code
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

bit patterns

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


parts of an object file
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.