[SOLVED] CS c++ Java c/c++ mips data structure assembler assembly computer architecture CPU Procedure Calls Memory Addressing Modes

$25

File Name: CS_c++_Java_c/c++_mips_data_structure_assembler_assembly_computer_architecture_CPU_Procedure_Calls_Memory_Addressing_Modes.zip
File Size: 1149.24 KB

5/5 - (1 vote)

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])!=’)i += 1; } Addresses of vars x, y in $a0, $a1 Variable i in $s0 1/27/20 Matni, CS154, Wi20 13 Example in Assembly strcpy:addi $sp,$sp,4 # adjust stack for 1 more item sw $s0, 0($sp) # save $s0, will use it for i add $s0, $zero, $zero # i = 0 (why not use li?)L1: add $t1, $s0, $a1 lbu $t2, 0($t1)add $t3, $s0, $a0 sb $t2, 0($t3)beq $t2, $zero, L2 addi$s0,$s0,1j L1L2: lw $s0, 0($sp)addi $sp, $sp, 4 jr $ra# &y[i] in $t1 (no ref + ix4?)# $t2 = y[i] (i.e. dereferenced)# &x[i] in $t3# x[i] = y[i]# if y[i] == 0 (i.e. ), go to L2 #else,i=i+1# Repeat loop# y[i] == 0: end of string.# Restore old $s0; pop 1 word off stack # return14Branch AddressingI-Type of instruction (beq , bne) Branch instructions specify:Opcode + 2 registers + target address Most branch targets are near the branch instruction in the text segment of memory Either ahead or behind it Addressing can be done relative to the value in PC Reg. (PC-Relative Addressing) Target address = PC + offset (in words) x 4 PC is already incremented by 4 by this time1/27/20 Matni, CS154, Wi20 15 Branching Far AwayIf branch target is too far to encode with 16-bit offset, then assembler will rewrite the code Examplebeq $s0, $s1, L1bne $s0, $s1, L2j L1 L2: …# L1 is far away # rewritten… 1/27/20Matni, CS154, Wi2016 Jump AddressingJ-Type of instruction (j , jal) Jump (j and jal) targets could be anywhere in text segment Encode full address in instruction Direct jump addressing Target address = (address x 4 ) OR (PC[31: 28]) i.e. Take the 4 most sig. bits in PCand concatenate the 26 bits in address fieldand then concatenate another 00 (i.e x 4)1/27/20 Matni, CS154, Wi20 17 Target Addressing Example Assume Loop is at location 800001/27/20 Matni, CS154, Wi20 18Examples:addi $t0, $t0, 42add $t0, $t1, $t3lw $t0, 4($t1) beq $t0, $t1, L1j L11/27/20 Matni, CS154, Wi20 19Addressing Mode SummaryYOUR TO-DOs for the Week Readings! Chapters 2.11 2.13Turn in Lab 3! 1/27/20 Matni, CS154, Wi20 20 1/27/20 Matni, CS154, Wi20 21

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] CS c++ Java c/c++ mips data structure assembler assembly computer architecture CPU Procedure Calls Memory Addressing Modes
$25