Supporting Procedure in Computer Hardware - Computer System | CPSC 440, Study notes of Computer Architecture and Organization

Material Type: Notes; Class: Computer System Architecture; Subject: Computer Science; University: California State University - Fullerton; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/16/2009

koofers-user-n2e
koofers-user-n2e ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPSC 440, Dr. Wang 1
CPSC440 Lecture 3
Supporting Procedure in Computer Hardware:
When calling a procedure the program must:
1) Place the parameters for the procedure;
2) Transfer control to the procedure;
3) Acquire the storage resources needed
4) Perform the desired task;
5) Place the results for the calling program;
6) Return control to the point of origin.
Which of these are done by the caller? Which of them are done by the callee?
Special registers for procedure calling:
1) $a0 - $a3: parameters;
2) $v0, $v1: return values;
3) $ra: return address (current PC + 4).
i) Using more registers โ€“ stack
Special register $sp for stack pointer.
Example:
int leaf( int g, int h, int i, int j )
{
int f;
f = (g + h) โ€“ (i + j);
return f;
}
in MIPS:
leaf:
addi $sp, $sp, -12
sw $t1, 8($sp)
sw $t0, 4($sp)
sw $s0, 0($sp)
add $t0, $a0, $a1
add $t1, $a2, $a3
sub $s0, $t0, $t1
add $v0, $s0, $zero
lw $s0, 0($sp)
lw $t0, 4($sp)
lw $t1, 8($sp)
addi $sp, $sp, 12
jr $ra
Want to save some time from storing and recovering registers? Temporary and saved register convention:
1) $t0 - $t9: are temporary and not preserved;
2) $s0 - $s7: are saved and preserved.
ii) Nested Procedures โ€“ preserve return address and parameters
Example:
n! = 1 if (n=0), n! = n x (n-1)! if (n>0).
pf3
pf4

Partial preview of the text

Download Supporting Procedure in Computer Hardware - Computer System | CPSC 440 and more Study notes Computer Architecture and Organization in PDF only on Docsity!

CPSC440 Lecture 3

Supporting Procedure in Computer Hardware:

When calling a procedure the program must:

  1. Place the parameters for the procedure;
  2. Transfer control to the procedure;
  3. Acquire the storage resources needed
  4. Perform the desired task;
  5. Place the results for the calling program;
  6. Return control to the point of origin.

Which of these are done by the caller? Which of them are done by the callee?

Special registers for procedure calling:

  1. $a0 - $a3: parameters;
  2. $v0, $v1: return values;
  3. $ra: return address (current PC + 4).

i) Using more registers โ€“ stack Special register $sp for stack pointer.

Example: int leaf( int g, int h, int i, int j ) { int f; f = (g + h) โ€“ (i + j); return f; }

in MIPS:

leaf: addi $sp, $sp, - sw $t1, 8($sp) sw $t0, 4($sp) sw $s0, 0($sp) add $t0, $a0, $a add $t1, $a2, $a sub $s0, $t0, $t add $v0, $s0, $zero lw $s0, 0($sp) lw $t0, 4($sp) lw $t1, 8($sp) addi $sp, $sp, 12 jr $ra

Want to save some time from storing and recovering registers? Temporary and saved register convention:

  1. $t0 - $t9: are temporary and not preserved;
  2. $s0 - $s7: are saved and preserved.

ii) Nested Procedures โ€“ preserve return address and parameters

Example: n! = 1 if (n=0), n! = n x (n-1)! if (n>0).

in C: int fact (int n) { if (n == 0) return (1); else return (n*fact(n-1)); }

in MIPS:

fact: addi $sp, $sp, - sw $ra, 4($sp) sw $a0, 0($sp) slti $t0, $a0, 1 beg $t0, $zero, L addi $v0, $zero, 1 addi $sp, $sp, 8 jr $ra L1: addi $a0, $a0, - jal fact lw $a0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 mul $v0, $a0, $v jr $ra

Preserved No Preserved Saved registers: $s0 - $s7 Temporary registers: $t0 - $t Stack pointer: $sp Everything in the Stack

Argument registers: $a0 - $a

Return address: $ra Return value registers: $v0, $v

iii) Allocating Space for New Data โ€“ the frame pointer $fp and the global pointer $gp

$fp

$sp

Beyond Numbers โ€“ processing strings:

lb $t0, 0($sp)

Arrays vs. Pointers:

Example: Clear1 (int array[], int size) { int i; for (i = 0; i<size; i++) array[i] = 0; }

in MIPS:

move $t0, $zero Loop1: sll $t1, $t0, 2 add $t2, $a0, $t sw $zero, 0($t2) addi $t0, $t0, 1 slt $t3, $t0, $a bne $t3, $zero, Loop

Another example:

Clear2 (int *array, int size) { int *p; for (p = &(array[0]); p<&(array[size]); p=p+1) *p = 0; }

in MIPS:

move $t0, $a Loop2: sw $zero, 0($t0) addi $t0, $t0, 4 sll $t1, $a1, 2 add $t2, $a0, $t slt $t3, $t0, $t bne $t3, $zero, Loop

An improved version:

move $t0, $a sll $t1, $a1, 2 add $t2, $a0, $t Loop2: sw $zero, 0($t0) addi $t0, $t0, 4 slt $t3, $t0, $t bne $t3, $zero, Loop

Notice the different between the two versions.