Professional Documents
Culture Documents
Computer Science 210 Computer Organization: Dynamic Storage and Heap Management
Computer Science 210 Computer Organization: Dynamic Storage and Heap Management
Computer Organization
private E data;
data next
private Node<E> next;
System stack
The stack pointer moves up, and
xFDFF
the heap pointer moves down
I/O device registers
xFFFF
Heap Management
;; This program uses the system heap to create and link together two nodes.
.ORIG x3000
.ORIG x3000
names = null; // 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
;; Subroutine NEW
; 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
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
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
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
R3
Subroutine NEW
;; Subroutine NEW
; 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
R3
Subroutine NEW
;; Subroutine NEW
; 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
R3
Subroutine NEW
;; Subroutine NEW
; 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
R3
Subroutine DISPOSE
;; Subroutine DISPOSE
; 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
R1
Subroutine DISPOSE
;; Subroutine DISPOSE
; 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
R1
Subroutine DISPOSE
;; Subroutine DISPOSE
; 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
R1
Subroutine DISPOSE
;; Subroutine DISPOSE
; 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
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