Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 210 Computer Organization

Similar presentations


Presentation on theme: "Computer Science 210 Computer Organization"— Presentation transcript:

1 Computer Science 210 Computer Organization
Dynamic Storage and Heap Management

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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)

10 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

11 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

12

13 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

14 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 ; ???

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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)

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


Download ppt "Computer Science 210 Computer Organization"

Similar presentations


Ads by Google