Computer Science 210 Computer Organization

Slides:



Advertisements
Similar presentations
Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines.
Advertisements

The University of Adelaide, School of Computer Science
Chapter 10 And, Finally.... Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display A Final Collection of ISA-related.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Lilian Blot LINEAR DATA STRUCTURE LINKED LIST Abstract Data Structure Autumn 2014 TPOP 1.
CPSC 388 – Compiler Design and Construction
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Overview I/O – memory mapped programmed / interrupt driven Traps mechanism & RET Subroutines & JSR & JSRR & RET Interrupt mechanism & RTI.
S. Barua – CPSC 240 CHAPTER 10 THE STACK Stack - A LIFO (last-in first-out) storage structure. The.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Choice for the rest of the semester New Plan –assembler and machine language –Operating systems Process scheduling Memory management File system Optimization.
Chapter 9 & 10 Subroutines and Interrupts. JSR Instruction: JSR offset (11 bit) xxxxxxxxxxx [PC ]  R7, JMP Offset Jump to Subroutine at offset.
COMPSCI 105 S Principles of Computer Science Linked Lists 1.
Chapters 4 & 5: LC-3 Computer Architecture Machine Instructions Assembly language Programming in Machine and Assembly Language.
Computer Science 210 Computer Organization Introduction to Subroutines.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Activation records and function invocation.
Computer Science 210 Computer Organization More on Assembler.
COMPSCI 105 SS 2015 Principles of Computer Science
Tirgul 10. What We Will See Today:  Linked lists  Graphs  Trees  Iterators and Generators.
Introduction to Computing Systems and Programming The LC-2.
Computer Science 210 Computer Organization Machine Language Instructions: Control.
Computer Science 210 Computer Organization
Storage Allocation Mechanisms
Computer Science 210 Computer Organization
Fundamentals of Programming II Linked Lists
Lecture 6 of Computer Science II
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
Chapter 7 & 9.2 Assembly Language
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
HKN ECE 220: Fall 2017 Midterm 1 AJ Schroeder, Evan Lissoos, Utsav Kawrani 23rd September, 2017.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 10 The Stack.
Computer Science 210 Computer Organization
Chapter 7 LC-2 Assembly Language.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
HKN ECE 220: Spring 2018 Midterm 1
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
TRAP Routines Subroutines Privileged Instructions
Chapter 9 TRAP Routines and Subroutines
Node Applications in Abstract Data Types
Chapter 7 LC-2 Assembly Language.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 7 Assembly Language
Computer Science 210 Computer Organization
TRAP Routines Privileged Instructions Subroutines
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
Course Overview PART I: overview material PART II: inside a compiler
HKN ECE 220: Fall 2018 Midterm 1 Andrew Fortunat, Jeff Chang, Srijan Chakraborty, Kanad Sarkar February 16, 2019.
Linked Lists.
Chapter 9 TRAP Routines and Subroutines
Chapter 9 TRAP Routines and Subroutines
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Revised based on textbook author’s notes.
Implementing Functions: Overview
Presentation transcript:

Computer Science 210 Computer Organization Dynamic Storage and Heap Management

Arrays and Linked Structures from lists import ArrayList, LinkedList names = ArrayList() numbers = LinkedList() for i in (range 1, 6): names.add("Name" + i) numbers.add(i) An array-based structure allocates storage for several elements A linked structure allocates storage for each element as needed

Nodes in Linked Structures class Node(object): def __init__(self, data, next) self.data = data self.next = next class LinkedList(AbstractList): def __init__(self): self.head = self.tail = None AbstractList.__init__(self) def add(self, item): newNode = Node(item, None) if self.tail is None: self.head = newNode else: self.tail.next = newNode self.tail = newNode self.size += 1 data next head data next data next tail

Nodes in Linked Structures class Node(object): def __init__(self, data, next) self.data = data self.next = next class LinkedList(AbstractList): def __init__(self): self.head = self.tail = None AbstractList.__init__(self) def add(self, item): newNode = Node(item, None) if self.tail is None: self.head = newNode else: self.tail.next = newNode self.tail = newNode; self.size += 1 data next head data next data next tail In assembler, links (head, tail, next) are addresses

Dynamic Storage and the System Heap x0000 Operating system The system stack provides dynamic storage for subroutine calls The system heap provides dynamic storage for linked nodes and other dynamically allocated objects The stack pointer moves up, and the heap pointer moves down x3000 User program code System heap System stack xFDFF I/O device registers xFFFF

Heap Management Languages like Python, Java, C#, and Lisp allow the programmer to request new dynamic memory; unused memory is recycled automatically (garbage collection) Languages like C, C++, ObjectiveC, and Ada require explicit allocation and deallocation of dynamic memory

A Simple LC-3 Heap Manager Our heap manager allows programs to allocate and deallocate nodes for linked structures (no automatic garbage collection) Each node has just two fields, a data field and a next field

The Heap Interface The heap structure consists of a heap pointer (in R5, which now can’t be used for anything else) three new subroutines, INITHEAP, NEW, and DISPOSE

Subroutine INITHEAP The subroutine INITHEAP is called at program startup This routine initializes the heap pointer and formats the heap memory into a sequence of linked nodes (sometimes also called a free list)

Subroutine NEW The subroutine NEW is called to obtain a pointer to a new node This routine receives the data element in R1 and a pointer to the next node in R2 It returns a pointer to the new node in R3 The empty pointer is x0000

Create a Linked List of Two Nodes ;; Author: Ken Lambert ;; This program uses the system heap to create and link together two nodes. .ORIG x3000 ;; Main program register usage: ; R1 = the datum for a new node ; R2 = the next link for a new node ; R3 = a pointer to the new node ; Main program code JSR INITHEAP ; Initialize the heap LD R1, X ; Set the parameter for the first datum AND R2, R2, #0 ; Set the parameter for the next link JSR NEW ST R3, HEAD ; Reset HEAD to the new node LD R1, Y ; Set the parameter for the second datum LD R2, HEAD ; Set the parameter for the next link HALT ; Main program data variables X .FILL 6 ; A data element Y .FILL 5 ; A data element HEAD .BLKW 1 ; A pointer to the first node in the list

Create a Linked List with a Loop ;; Author: Ken Lambert ;; This program uses the system heap to create and ;; link together five nodes. .ORIG x3000 ;; Main program register usage: ; R1 = the datum for a new node ; R2 = the next link for a new node ; R3 = a pointer to the new node ; Main program code JSR INITHEAP ; Initialize the heap LD R1, MAXDATUM ; Set the parameter for the first datum AND R2, R2, #0 ; Set the parameter for the next link WHILE JSR NEW ST R3, HEAD ; Set HEAD to the new node ADD R2, R3, #0 ; Set the parameter for the next link ADD R1, R1, #-1 ; Set the parameter for the next datum BRp WHILE ; Return to top of loop HALT ; Main program data variables MAXDATUM .FILL 5 HEAD .BLKW 1

Process a Linked List: Output ; Output process LD R2, CR ; Get carriage-return character LD R3, DIGITOFFSET ; Get digit offset LD R1, HEAD ; Get pointer to first node OUTLOOP BRz ENDOUT ; Stop if it's empty LDR R0, R1, #0 ; Access and output the datum ADD R0, R0, R3 OUT ADD R0, R2, #0 ; Output the carriage-return LDR R1, R1, #1 ; Move to the next node BR OUTLOOP ENDOUT HALT ; Main program data variables HEAD .BLKW 1 ; This will have been set to an address CR .FILL #13 DIGITOFFSET .FILL #48 ; ???

Disposing of a Linked List names = LinkedList() # Add a bunch of names to the list and process them names = None # The garbage collector automatically recycles nodes to the heap

Disposing of a Linked List ; Disposal process LD R1, HEAD ; Get the pointer to the first node DISLOOP BRz ENDDIS ; Stop if it's empty LDR R2, R1, #1 ; Save the pointer to the next node JSR DISPOSE ADD R1, R2, #0 ; Move to the next node BR DISLOOP ENDDIS ST R1, HEAD ; HEAD should now be empty HALT

Subroutine NEW R5 data next data next data next data next ; Returns the address of a new node in R3 ; Input parameters: R1 - the datum for the node ; R2 - the next field of the node ; R5 - the heap pointer ; Output parameters: R3 - the address of the new node ; R5 - the heap pointer NEW STR R1, R5, #0 ; Store the datum in the new node ADD R5, R5, #1 ; Move heap pointer to the next field of the new node ADD R3, R5, #0 ; Save a copy of the heap pointer LDR R5, R5, #0 ; Reset the heap pointer to the address contained in the next field STR R2, R3, #0 ; Set the next field of the new node ADD R3, R3, #-1 ; Set the output parameter to the address of the new node RET R5 data next data next data next data next

Subroutine NEW R5 R1 next data next data next data next ; Returns the address of a new node in R3 ; Input parameters: R1 - the datum for the node ; R2 - the next field of the node ; R5 - the heap pointer ; Output parameters: R3 - the address of the new node ; R5 - the heap pointer NEW STR R1, R5, #0 ; Store the datum in the new node ADD R5, R5, #1 ; Move heap pointer to the next field of the new node ADD R3, R5, #0 ; Save a copy of the heap pointer LDR R5, R5, #0 ; Reset the heap pointer to the address contained in the next field STR R2, R3, #0 ; Set the next field of the new node ADD R3, R3, #-1 ; Set the output parameter to the address of the new node RET R5 R1 next data next data next data next

Subroutine NEW R5 R1 next data next data next data next ; Returns the address of a new node in R3 ; Input parameters: R1 - the datum for the node ; R2 - the next field of the node ; R5 - the heap pointer ; Output parameters: R3 - the address of the new node ; R5 - the heap pointer NEW STR R1, R5, #0 ; Store the datum in the new node ADD R5, R5, #1 ; Move heap pointer to the next field of the new node ADD R3, R5, #0 ; Save a copy of the heap pointer LDR R5, R5, #0 ; Reset the heap pointer to the address contained in the next field STR R2, R3, #0 ; Set the next field of the new node ADD R3, R3, #-1 ; Set the output parameter to the address of the new node RET R5 R1 next data next data next data next

Subroutine NEW R5 R1 next data next data next data next R3 ; Returns the address of a new node in R3 ; Input parameters: R1 - the datum for the node ; R2 - the next field of the node ; R5 - the heap pointer ; Output parameters: R3 - the address of the new node ; R5 - the heap pointer NEW STR R1, R5, #0 ; Store the datum in the new node ADD R5, R5, #1 ; Move heap pointer to the next field of the new node ADD R3, R5, #0 ; Save a copy of the heap pointer LDR R5, R5, #0 ; Reset the heap pointer to the address contained in the next field STR R2, R3, #0 ; Set the next field of the new node ADD R3, R3, #-1 ; Set the output parameter to the address of the new node RET R5 R1 next data next data next data next R3

Subroutine NEW R5 R1 next data next data next data next R3 ; Returns the address of a new node in R3 ; Input parameters: R1 - the datum for the node ; R2 - the next field of the node ; R5 - the heap pointer ; Output parameters: R3 - the address of the new node ; R5 - the heap pointer NEW STR R1, R5, #0 ; Store the datum in the new node ADD R5, R5, #1 ; Move heap pointer to the next field of the new node ADD R3, R5, #0 ; Save a copy of the heap pointer LDR R5, R5, #0 ; Reset the heap pointer to the address contained in the next field STR R2, R3, #0 ; Set the next field of the new node ADD R3, R3, #-1 ; Set the output parameter to the address of the new node RET R5 R1 next data next data next data next R3

Subroutine NEW R5 R1 R2 data next data next data next R3 ; Returns the address of a new node in R3 ; Input parameters: R1 - the datum for the node ; R2 - the next field of the node ; R5 - the heap pointer ; Output parameters: R3 - the address of the new node ; R5 - the heap pointer NEW STR R1, R5, #0 ; Store the datum in the new node ADD R5, R5, #1 ; Move heap pointer to the next field of the new node ADD R3, R5, #0 ; Save a copy of the heap pointer LDR R5, R5, #0 ; Reset the heap pointer to the address contained in the next field STR R2, R3, #0 ; Set the next field of the new node ADD R3, R3, #-1 ; Set the output parameter to the address of the new node RET R5 R1 R2 data next data next data next R3

Subroutine NEW R5 R1 R2 data next data next data next R3 ; Returns the address of a new node in R3 ; Input parameters: R1 - the datum for the node ; R2 - the next field of the node ; R5 - the heap pointer ; Output parameters: R3 - the address of the new node ; R5 - the heap pointer NEW STR R1, R5, #0 ; Store the datum in the new node ADD R5, R5, #1 ; Move heap pointer to the next field of the new node ADD R3, R5, #0 ; Save a copy of the heap pointer LDR R5, R5, #0 ; Reset the heap pointer to the address contained in the next field STR R2, R3, #0 ; Set the next field of the new node ADD R3, R3, #-1 ; Set the output parameter to the address of the new node RET R5 R1 R2 data next data next data next R3

Subroutine DISPOSE R5 data next data next data next data next R1 ; Returns an old node (addressed by R1) to the heap ; Input parameters: R1 - a pointer to the old node ; R5 - the heap pointer ; Output parameter: R5 - the heap pointer DISPOSE ADD R1, R1, #1 ; Advance to the old node's next field STR R5, R1, #0 ; Store the heap pointer there ADD R5, R1, #-1 ; Aim the heap pointer at the old node RET R5 data next data next data next data next R1

Subroutine DISPOSE R5 data next data next data next data next R1 ; Returns an old node (addressed by R1) to the heap ; Input parameters: R1 - a pointer to the old node ; R5 - the heap pointer ; Output parameter: R5 - the heap pointer DISPOSE ADD R1, R1, #1 ; Advance to the old node's next field STR R5, R1, #0 ; Store the heap pointer there ADD R5, R1, #-1 ; Aim the heap pointer at the old node RET R5 data next data next data next data next R1

Subroutine DISPOSE R5 data next data next data next data next R1 ; Returns an old node (addressed by R1) to the heap ; Input parameters: R1 - a pointer to the old node ; R5 - the heap pointer ; Output parameter: R5 - the heap pointer DISPOSE ADD R1, R1, #1 ; Advance to the old node's next field STR R5, R1, #0 ; Store the heap pointer there ADD R5, R1, #-1 ; Aim the heap pointer at the old node RET R5 data next data next data next data next R1

Subroutine DISPOSE R5 data next data next data next data next R1 ; Returns an old node (addressed by R1) to the heap ; Input parameters: R1 - a pointer to the old node ; R5 - the heap pointer ; Output parameter: R5 - the heap pointer DISPOSE ADD R1, R1, #1 ; Advance to the old node's next field STR R5, R1, #0 ; Store the heap pointer there ADD R5, R1, #-1 ; Aim the heap pointer at the old node RET R5 data next data next data next data next R1

Subroutine INITHEAP ;; Subroutine INITHEAP ; Initializes the heap pointer and formats the heap memory as a free list ; Input parameter: R5 - the heap pointer ; Output parameter: R5 - the heap pointer ; Other registers: R1 - address of the next field of current node ; R2 - address of the next node ; R3 - counter for the loop INITHEAP LD R5, HEAPTOP ; Initialize the heap pointer ADD R1, R5, #1 ; Pointer to the next field of the current node ADD R2, R5, #2 ; Pointer to the next node LD R3, HEAPSIZE ; Counter for the number of nodes HEAPLOOP BRz ENDHEAP STR R2, R1, #0 ; Store pointer to the next node in the next current ; node ADD R1, R1, #2 ; Advance the pointers ADD R2, R2, #2 ADD R3, R3, #-1 ; Decrement the counter BR HEAPLOOP ENDHEAP RET ; Data variables for subroutine INITHEAP HEAPTOP .FILL x302F ; Address of the beginning of the heap HEAPSIZE .FILL #10 ; Number of nodes in the initial heap

Extensions Add checks for heap underflow with error handling Add capability for nodes of varying sizes Coalescing and compaction in the free list Garbage collection (probably worth an honors thesis)

Introduction to Programming in C For Wednesday Introduction to Programming in C