CPU Procedure Calls Memory Addressing Modes
CS 154: Computer Architecture Lecture #6
Winter 2020
Ziad Matni, Ph.D.
Dept. of Computer Science, UCSB
Administrative
Lab 03 how is that going?
1/27/20 Matni, CS154, Wi20 2
Lecture Outline
CPU Procedure Calls
The MIPS Calling Convention
Memory Addressing Modes Character Representations
Parallelism and Synchronization
1/27/20 Matni, CS154, Wi20 3
The MIPS Calling Convention In Its Essence
Remember: Preserved vs Unpreserved Regs
Preserved: $s0 $s7, and $ra, and $sp (by default) Unpreserved: $t0 $t9, $a0 $a3, and $v0 $v1
Values held in Preserved Regs immediately before a function call MUST be the same immediately after the function returns.
Use the stack memory to save these
Values held in Unpreserved Regs must always be assumed to change after a function call is performed.
$a0 $a3 are for passing arguments into a function $v0 $v1 are for passing values from a function
1/27/20 Matni, CS154, Wi20 4
Example
C/C++ code:
int fact (int n)
{
if (n < 1) return 1;else return n * fact(n – 1);}Remember: Argument n in $a0 Result in $v01/27/20 Matni, CS154, Wi20 5int fact (int n){ if (n < 1) return 1; else return n * fact(n – 1);}Example continued…fact:addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) move $s0, $a0 li $t0, 1 blt $s0, $t0, else mult $v0, $s0 mflo $v0 addi $a0, $a0, -1 jal fact1/27/20 # adjust stack for 2 items# push (save) return address# push (save) argumentelse: # restore original n# restore return addresslw $s0, 0($sp)lw $ra, 4($sp)addi $sp, $sp, 8 # pop 2 items from stack jr $ramain: li $v0, 1 li $a0, 5 jal fact6# Expect to see returned value in $v0 Variable Storage ClassesRECALL: A C/C++ variable is generally a location in memory A variable has type (e.g. int, char)and storage class (automatic vs. static) Automatic variables: local to a part of the program, created & discarded Static variables: global vars (declared outside or using static in C/C++) MIPS software reserves the global pointer register, $gp, to get access toautomatic variables. 1/27/20 Matni, CS154, Wi20 7Memory Layout Text: program code Static data: global variables e.g., static variables in C, constant arrays and strings $gp initialized to address allowing offsets into this segment Heap: dynamic data e.g., malloc/free in C, new in C++,used for linked lists, dynamic arrays, etc… Stack: automatic storage1/27/20 Matni, CS154, Wi20 8 Stack & Heap in MIPS The stack is used for saving vars when procedures (functions) are called Also used to store some local vars to the function that cant fit in registers, like local arrays or structures The stack starts in the high end of memory and grows down The heap is used for saving vars that are dynamic data structures The heap starts in the low end (after static data) and grows up Allows the stack and heap to grow toward each other, allowing efficient use of memory.1/27/20 Matni, CS154, Wi20 9 Character Data in ComputersByte-encoded character sets like: ASCII (7 bits, i.e. 128 characters) No longer used, in favor of UTF-8, which is… Unicode: 8, 16, and 32-bit character set Used in Java, C++ wide characters, … Contains most of the worlds alphabets, plus symbols UTF-8, UTF-16: variable-length encodings (8-bits, 16-bits, respectively) 1/27/20 Matni, CS154, Wi20 10 Character Data in Assembly Must be stored in memory (Use the .data directive) Loading them from memory to a register requires: lw (load word), lh (load half-word), or lb (load byte) Especially if you want to do an operation on the data(like to change the value of the data)Or la (load address) Especially if you want to do a syscall on the data(you need the address for that) When you use lh or lb, the sign is extend to 32 bits Equivalents with sw (store word), sh (store half-word), and sb (store byte) 1/27/20 Matni, CS154, Wi20 11Representation of Strings Characters combined = strings 3 choices for representing a string:1. 1st position of the string is reserved to give the length of a string (int)2. Theres an accompanying var for the length of the string (usually in a structure)3. The last position of a string is indicated by a EOS character (null or ) C/C++ uses #3 So, the string UCSB is 5 bytes because the last one is 1/27/20 Matni, CS154, Wi20 12 ExampleC code (naive), i.e. with null-terminated stringvoid strcpy (char x[], char y[]) {int i;i = 0;while ((x[i]=y[i])!=’