Program Memory MIPS memory operations Getting Variable Addresses Advanced structures.

Slides:



Advertisements
Similar presentations
Lecture 13: 10/8/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Advertisements

Goal: Write Programs in Assembly
Lecture 5: MIPS Instruction Set
The University of Adelaide, School of Computer Science
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
1 ECE369 ECE369 Chapter 2. 2 ECE369 Instruction Set Architecture A very important abstraction –interface between hardware and low-level software –standardizes.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Chapter 2 — Instructions: Language of the Computer — 1 Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites.
Analysis of programs with pointers. Simple example What are the dependences in this program? Problem: just looking at variable names will not give you.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
SPIM and MIPS programming
Chapter 2 Instructions: Language of the Computer
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Computer Architecture CSCE 350
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
Chap.2: Instructions: Language of the computer Jen-Chang Liu, Spring 2006 Adapted from
Instructions Set Bo Cheng Instruction Set Design An Instruction Set provides a functional description of a processor. It is the visible.
Assembly Language Chapter 2 MIPS –arithmetic instructions –control instructions Translating Instructions to binary MIPS –memory instructions Advanced data.
Intro to Computer Architecture
1 Lecture 2: MIPS Instruction Set Today’s topic:  MIPS instructions Reminder: sign up for the mailing list cs3810 Reminder: set up your CADE accounts.
Programming With Alice. Alice Free Java based, 3D programming tool Enables the manipulation and interaction of 3D objects Can also.
ZONG Wen Department of Computer Science and Engineering The Chinese University of Hong Kong
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Lecture 4: MIPS Instruction Set Reminders: –Homework #1 posted: due next Wed. –Midterm #1 scheduled Friday September 26 th, 2014 Location: TODD 430 –Midterm.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Chapter 2 Instructions: Language of the Computer Part I.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
©These slides may be freely used, distributed, and incorporated into other works. 1 Addressing Modes For speed… we want fixed-size instructions, and they.
MS108 Computer System I Lecture 3 ISA Prof. Xiaoyao Liang 2015/3/13 1.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /22/2013 Lecture 12: Character Data Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Address alignment When a word (4-bytes) is loaded or stored the memory address must be a multiple of four. This is called an alignment restriction. Addresses.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Instruction Set Architectures Continued. Expanding Opcodes & Instructions.
MIPS Assembly Language Programming
CSCI206 - Computer Organization & Programming
Lecture 4: MIPS Instruction Set
Procedures (Functions)
Procedures (Functions)
In this lecture Global variables Local variables System stack
Assembly Programming using MIPS R3000 CPU
MIPS Assembly.
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
Lecture 4: MIPS Instruction Set
Computer Programming Machine and Assembly.
Systems Architecture I (CS ) Lecture 5: MIPS Instruction Set*
المواطنة في غير ديار الإسلام بين النافين والمثبتين دراسة فقهية نقدية
Registers in the CPU Inside the CPU.
MIPS coding.
CSCI206 - Computer Organization & Programming
September 5 Fang-Yi’s Office Hours Friday 10am  12pm (and another TBD) Programming Language Issues (JAVA vs. C, Pointers vs. Arrays) Representations Instructions.
Instruction Set Architectures Continued
Why arrays are cool 4/25/2007.
Instructions in Machine Language
COMS 361 Computer Organization
Assembly Programming using MIPS R3000 CPU
Variables and Computer Memory
January 16 The books are here. Assignment 1 now due Thursday 18 Jan.
CPU Structure CPU must:
Systems Architecture I (CS ) Lecture 5: MIPS Instruction Set*
Instruction Set Architecture
August 31 addresses Drop box Questions? 31 August 2004
MIPS Assembly.
Presentation transcript:

Program Memory MIPS memory operations Getting Variable Addresses Advanced structures

Registers vs Memory Registers are memory Everything residing in a register has a real home Variables live in memory and are brought into registers When that register is needed for something else, the item fast, temporary in memory (with an associated address) for computations is stored back to memory.

Memory Setup in C/Java int X; What does this do? What does the memory look like? X is located here. An int is 4 bytes, so it takes 4 locations &X &X + 4 “&X” means “address of X”

Operation# comment lw $2, 32($3)# $2 <- M[32 +$3] sw $2, 16($3)# M[16 +$3] <- $2 lb $2, 0($3)# $2 <- M[0 +$3] sb $2, 3($3)# M[3 +$3] <- $2 Load/Store Instructions Displacement addressing mode Register indirect is Displacement with 0 offset lw = load word (4 bytes), lb = load byte (1 byte)

Memory Addressing Modes ModeExampleMeaningUse DisplacementLw R4, 100(R1)$4 <- M[$ ]Arrays w/constant offset Register IndirectLw R4, (R1)$4 <- M[$1]You have pointer IndexedLw R4, (R1+R2)$4 <- M[$1 + $2]$1 has base,$2 has offset Direct or absoluteLw R4, (1001)$4 <- M[1001]Globals, static data? Memory indirectLw <- M[M[$1]]Double-pointer AutoincrementLw R1, (R2) +$4 <- M[$2], $2 = $2 + d Stepping through array in loop AutodecrementLw R1, (R2) -$4 <- M[$2], $2 = $2 - d Stepping through array in loop ScaledLw R4, 100(R2)[R3]$4 <- M[100+$2+ ($3*d)] Indexing arrays of large elements MIPS supports ONLY Displacement mode Register Indirect and Direct are supported implicitly by Displacement

Address depends on variable type Local variables on the stack Global variables declared and allocated in data segment Heap variables (malloc or new)

Declaring, Allocating & Initializing Global Variables C: int GlobalA = 3; int main(int argc, char *argv[]) { } Java: public class MyClass{ public static int GlobalA = 3; }; MIPS:.data GlobalA:.word 0x03;.text main:

Declaring & Initializing Local Variables in HLL C: int main(int argc, char *argv[]) { int LocalA = 5; } Java: public class MyClass { public static void main(String[] argv) { int LocalA = 5; } }; MIPS: add $sp, $sp, -(24 + x + 4)# where x is space for preserved regs addi $t0, $0, 5 sw $t0, 0 ($sp)

Declaring & Initializing Heap Variables in HLL C: int main(int argc, char *argv[]) { int *LocalA = (int *)malloc(4); *LocalA = 5; } add $sp, $sp, -(24 + x + 4)# where x is space for preserved regs addi $a0, $0, 4 jal malloc sw $v0, 0($sp) addi $t0, $0, 5 sw $t0, 0($v0) Java: int main(int argc, char *argv[]) { int LocalA[] = new int[1] LocalA[0] = 5; }

How do I get &A? Global (Static) Vars –la $s0, GlobalA Local (Stack) Vars –addi $s0, $sp, const Dynamic (Heap) Vars –“new” returns the address into $v0 –sw $v0, 16($sp) –HeapA, in stack, points to a location in the heap!!! int GlobalA; int main(int argc, char *argv[]) { int StackA; int *HeapA = new int; } Assembler changes this pseudoinstruction into the real thing

MIPS Memory Notes la $s0, variable is a pseudoinstruction – assembler replaces it (i.e. 0x ) –lui $s0, 0x1004 –ori $s0, $s0, 0x0378 Register spilling - not enough registers, must save a value in memory Alignment – Integers must have addresses that are evenly divisible by word size (in bytes) – All variables’ addresses are divisible by their size

MIPS Example 3 A = B + A; la $t0, A// $t0 = &A (address of A) lw $t1, 0($t0) // $t1 = A lw $t2, 16($sp) // $t2 = B add $t1, $t1, $t2 // $t1= A + B sw $t1, 0($t0) // A = $t1 B = 2 * A; la $t0, A# $t0 = &A (address of A) lw $t1, 0($t0) # $t1 = A sll $t1, $t1, 1 # $t1 = 2 * A sw $t1, 16($sp) # B = 2*A Assumptions: A is a global var B is a local var at 16+$sp

Memory Setup in C/Java C++: int *intarray = new int[10]; Java: int[] intarray = new int[10]; What does this do? What does the memory look like? Where is intarray[5] located? intarray + 20 Where is intarray[i] located? intarray + 4*i intarray intarray + 20 &(intarray[0]) + 20 intarray &(intarray[0])

Declaring & Initializing Global Arrays in HLL int GlobalA = 3; int GlobalB[] = {0x , 0x , 0x200b0001, 0x8b502a, 0x , 0x , 0x , 0x800fffa, 0x , 0x }; int main(int argc, char *argv[]) { } public class MyClass{ public static int GlobalA = 3; public static int GlobalB[] = {0x , 0x , 0x200b0001, 0x8b502a, 0x , 0x , 0x , 0x800fffa, 0x , 0x }; };

Declaring & Initializing Global Arrays in MIPS.data GlobalA:.word 0x03; GlobalB:.word 0x x x200b0001 0x8b502a.word 0x x x x800fffa.word 0x x text main:

Declaring & Initializing Local Arrays int main(int argc, char *argv[]) { int LocalA = 5; int LocalB[] = {1,2,3}; } add $sp, $sp, -(24 + x )# where x is space for preserved regs addi $t0, $0, 5 sw $t0, 12($sp) addi $t0, $0, 1 sw $t0, 0($sp) addi $t0, $0, 2 sw $t1, 4($sp) addi $t0, $0, 3 sw $t1, 8($sp) // and so forth Not possible in Java!!!!! In Java, arrays are references to Array objects.

Declaring & Initializing Heap Arrays in HLL int main(int argc, char *argv[]) { int *LocalA = (int *)malloc(4); *LocalA = 5; int *LocalB = (int *)malloc(12); LocalB[0] = 1; LocalB[1] = 2; LocalB[2] = 3; } public class MyClass{ public static void main(int argc, String argv) { int LocalA[] = new int[1]; LocalA[0] = 5; int LocalB[] = new int[3]; LocalB[0] = 1; LocalB[1] = 2; LocalB[2] = 3; } };

Declaring & Initializing Heap Arrays in MIPS add $sp, $sp, -(24 + x + 8)# where x is space for preserved regs addi $a0, $0, 4 jal malloc sw $v0, 4($sp) // store the reference into the stack addi $t0, $0, 5 sw $t0, 0($v0) // initialize first elements as 5 (*LocalA = 5) addi $a0, $0, 12 jal malloc sw $v0, 0($sp) // store the reference into the stack addi $t0, $0, 1 sw $t0, 0($v0) // LocalB[0] = 1 addi $t0, $0, 2 sw $t0, 4($v0) // LocalB[1] = 2 addi $t0, $0, 3 sw $t0, 8($v0) // LocalB[2] = 3

MIPS Example 5 Translate from C code int A[100]; // ints are 4 bytes in Java/C char B[100]; // chars are 1 byte in C void main() { char c = B[50]; A[1] = A[5] + 7; } Assumptions: A&B are global, c is in the stack, 6 bytes from $sp

MIPS Example 5 Translate int A[100]; // ints are 4 bytes in C/Java char B[100]; // chars are 1 byte in C char c = B[50]; A[1] = A[5] + 7; Assumptions: A & B are global c is in the stack, 6 bytes from $sp Use LoadByte, not LoadWord, because char (in C) is 1 byte la $s1, B lb $t1, 50($s1)# $t1 = B[50]; lw $t0, 5 ($s0); x = A[5];lw $t0, 20 ($s0)# $t0 = A[5]; addi $t0, $t0, 7# $t0 = A[5] + 7; sw $t0, 4 ($s0)# A[1] = A[5] + 7; sb $t1, 6($sp)# c = B[50]; la $s0, A

MIPS Example 6 Translate int A[100]; int i; … x = A[i]; Assumptions: &(A[0]) is in $s0, x is in $t0 i is in $s1

MIPS Example 6 Translate int A[100]; int i; … x = A[i]; lw $t0, $s1 ($s0) Assumptions: &(A[0]) is in $s0, x is in $t0 i is in $s1

Memory Addressing Modes lw $t0, $s1 ($s0) # $t0 = A[i] ModeExampleMeaningUse DisplacementLw R4, 100(R1)$4 <- M[$ ]Arrays w/constant offset Register IndirectLw R4, (R1)$4 <- M[$1]You have pointer IndexedLw R4, (R1+R2)$4 <- M[$1 + $2]$1 has base,$2 has offset Direct or absoluteLw R4, (1001)$4 <- M[1001]Globals, static data? Memory indirectLw <- M[M[$1]]Double-pointer AutoincrementLw R1, (R2) +$4 <- M[$2], $2 = $2 + d Stepping through array in loop AutodecrementLw R1, (R2) -$4 <- M[$2], $2 = $2 - d Stepping through array in loop ScaledLw R4, 100(R2)[R3]$4 <- M[100+$2+ ($3*d)] Indexing arrays of large elements

Memory Addressing Modes lw $t0, ($s1 + $s0) # $t0 = A[i] ModeExampleMeaningUse DisplacementLw R4, 100(R1)$4 <- M[$ ]Arrays w/constant offset Register IndirectLw R4, (R1)$4 <- M[$1]You have pointer IndexedLw R4, (R1+R2)$4 <- M[$1 + $2]$1 has base,$2 has offset Direct or absoluteLw R4, (1001)$4 <- M[1001]Globals, static data? Memory indirectLw <- M[M[$1]]Double-pointer AutoincrementLw R1, (R2) +$4 <- M[$2], $2 = $2 + d Stepping through array in loop AutodecrementLw R1, (R2) -$4 <- M[$2], $2 = $2 - d Stepping through array in loop ScaledLw R4, 100(R2)[R3]$4 <- M[100+$2+ ($3*d)] Indexing arrays of large elements

Memory Addressing Modes lw $t0, 0 ($s0)[$s1] # $t0 = A[i] ModeExampleMeaningUse DisplacementLw R4, 100(R1)$4 <- M[$ ]Arrays w/constant offset Register IndirectLw R4, (R1)$4 <- M[$1]You have pointer IndexedLw R4, (R1+R2)$4 <- M[$1 + $2]$1 has base,$2 has offset Direct or absoluteLw R4, (1001)$4 <- M[1001]Globals, static data? Memory indirectLw <- M[M[$1]]Double-pointer AutoincrementLw R1, (R2) +$4 <- M[$2], $2 = $2 + d Stepping through array in loop AutodecrementLw R1, (R2) -$4 <- M[$2], $2 = $2 - d Stepping through array in loop ScaledLw R4, 100(R2)[R3]$4 <- M[100+$2+ ($3*d)] Indexing arrays of large elements

MIPS Example 6 Translate int A[100]; int i; … x = A[i]; lw $t0, $s1 ($s0) sll $t1, $s1, 2# $t1 = i<<2 or i * 4 Assumptions: &(A[0]) is in $s0, x is in $t0 i is in $s1

MIPS Example 6 Translate int A[100]; int i; … x = A[i]; sll $t1, $s1, 2# $t1 = i<<2 or i * 4 add $t1, $s0, $t1# $t1 = (&A[0] +i*4) or &(A[i]) Assumptions: &(A[0]) is in $s0, x is in $t0 i is in $s1

MIPS Example 6 Translate int A[100]; int i; … x = A[i]; sll $t1, $s1, 2# $t1 = i<<2 or i * 4 add $t1, $s0, $t1# $t1 = (i*4+&A[0]) or &(A[i]) lw $t0, 0($t1)# $t0 = A[i]; Assumptions: &(A[0]) is in $s0, x is in $t0 i is in $s1

Objects Member variables are a constant offset from the beginning of the object Member functions have hidden “this” pointer as first argument

Linked List class Link { private: Link*next; void*data; public: Link(){next=NULL;data=NULL;} inline void SetData(void *d){data = d;} inline void *GetData(){return data;} inline void SetNext(Link *n){next = n;} inline Link *GetNext(){return next;} }; If “this” pointer (address of current Link) is stored in $a0, what is the memory location of the current object’s “data” variable? 4 + $a0 C++ syntax: Link * means “reference to a Link” In Java, all Objects must use references void * means “reference to an item of unknown type”

Assembly code for Link For all methods, assume “this” pointer is in $a0, first argument is in $a1 Place the return value in $v0. SetData GetData this->data = d // this.data = d return this->data // return this.data sw $a1, 4 ($a0) lw $v0, 4($a0) Which is the source? Which is the destination? d this.data Is d in a register or in memory? Is this.data in a register or in memory? reg $a1 memory Which is the source? Which is the destination? this.data $v0 Is this.data in memory or a register? Is $v0 in a register or in memory? memory reg $v0

Dynamically Allocated Structures Linked Lists The memory is not contiguous – it is scattered about. Allocated dynamically, so we must call malloc or new allocator gives you the address of the beginning of the allocated memory in $v0

Linked List class LinkedList { private: Link*head; Link*tail; public: LinkedList(); ~LinkedList(); void InsertHead(void *data); void InsertTail(void *data); void *RemoveHead(); }; class LinkedList { private: Linkhead; Linktail; public: LinkedList(); ~LinkedList(); void InsertHead(Object data); void InsertTail(Object data); Object RemoveHead(); }; C++ syntax Java syntax

Code for void InsertTail(void *data) “this” Linked List is in $a0, data is in $a1 Link link = new Link(); //Link *link = new Link(); link.data = data; //link->data = data; this.tail.next = link; //this->tail->next = link; this.tail = link; //this->tail = link; jal new # don’t worry about args Where does “new” place the address of the new Link? $v0 sw $a1, 4 ($v0) lw $t0, 4 ($a0) # $t0 = this.tail sw $v0, 0 ($t0) # this.tail.next = link sw $v0, 4 ($a0) # this.tail = link Java Syntax //C++ Syntax

Lab 2 No pseudoinstructions in first part (no la) Dynamically allocate links –System call –Look in manual for the proper inputs/outputs