Skip to content

Instantly share code, notes, and snippets.

@insipx
Created June 2, 2016 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save insipx/945baf4b8591e4084a3b91382fc883cd to your computer and use it in GitHub Desktop.
Save insipx/945baf4b8591e4084a3b91382fc883cd to your computer and use it in GitHub Desktop.
;-------------------------
;BuildLst()
;------------------------
;---Local Variables----
size: .BLOCK 2 ; size
head: .BLOCK 2
stringrf: .BLOCK 2 ;the addr of the str from readSo
next: .BLOCK 2 ;the addr where we can start putting stuff in the heap
node: .BLOCK 2
;----------------------
buildLst: NOP0
SAVEA
SAVEX
CLRA
CLRX
STA size,d
loop: NOP0
CLRA
CALL readSO ; calling readSO to read the string
STA stringrf,d ;storing the reference to the String into temp
;------------------------------
CPA 0,i ; if readSo returns 0 we are done
BREQ done
;-------------------------------
LDA 4,i ;length of 4 bytes
STA -2,s
SUBSP 2,i
CALL new
ADDSP 2,i
STA node,d ; storing the ref from new into node
;-----------------------------; this is the place in the stack we will be using
LDA size,d ;Loading size into A
TSTA ;testing against 0
BREQ first ;if it's the first element, branch to first
LDA node,d
; STA next,n ;store the ref to current node in next for the prev node
MOVE node,d,next,d
ADD next,d,2,i
LDA 0,i
STA next,n
BR insert
;-----------------------------
back: LDA stringrf,d ;store string in first node cell
STA node,n
;----------------------------- ;make ref cell just 0
MOVE node,d,next,d ;LDA node,d STA next,d
ADD next,d,2,i
LDA 0,i
STA next,n
INC size,d
;-----------------------------
BR loop
first: MOVE node,d,head,d
BR back
;--------------------------------------------------------
;-------Insertion Sort-----------------------------------
;--------------------------------------------------------
;Local Vars
;-----------------------------
htemp: .BLOCK 2 ;current element starting at head
htnext: .BLOCK 2 ;next ele in linked list.....cell:[htemp][htnext]
htSr: .BLOCK 2 ; this is the curr string we are on whilst going through the linkedlist
prev: .BLOCK 2 ; used for if the ele being inserted is greater than current
;-----------------------------
insert: NOP0
MOVE head,d,htemp,d ;the first run through will be a bit different than what "iloop" is
;------------------------------ getting the addresses ;mainly, if the element being inserted
MOVE htemp,d,htnext,d ;> ; ;is greater than 'head', we use a different
ADD htnext,d,2,i ;> ;subprogram (actually that's the only diff)
;--------------------------------
MOVE htemp,n,htSr,d ;must do first element first
CLRA
;-------------------------------- ;call ScompTo
PUSH stringrf,d
PUSH htSr,d
CALL ScompTo
ADDSP 4,i
;---------------------------------
CPA 0,i
BREQ link
BRLT less
BRGT greatr2 ; 'greatr2' because if it's greater here, we have a different method
;to take care of that
;-------------------------------
iloop: NOP0
;------------------------------------checks if next addr is null
MOVE htemp,n,htSr,d ;r
PUSH stringrf,d
PUSH htSr,d
CALL ScompTo
ADDSP 4,i
CPA 0,i
BRLT less
BRGT greatr
temp: .BLOCK 2
less: NOP0
;--------------------------------
MOVE htnext,n,temp,d ; check if the 'next' is null
LDA temp,d
CPA 0,i
BREQ link ; if it is, we just tack it onto the end of the list
;--------------------------------- ; now we need to iterate thorugh the LinkedList
MOVE htemp,d,prev,d ; move current into previous
MOVE htnext,n,htemp,d ; move the next into curr
MOVE htemp,d,htnext,d ; get the nextnode for htnext
ADD htnext,d,2,i ;
BR iloop ;keep looping until we get to greater
greatr: NOP0 ;general case for if string we are inserting is greater
MOVE stringrf,d,node,n ;than an inserted string
MOVE htemp,d,next,n
LDA prev,d
ADD prev,d,2,i
MOVE node,d,prev,n
INC size,d
BR loop
greatr2: NOP0 ;this one is for if the current element is greater
MOVE stringrf,d,node,n ;than the 'head' element
MOVE htemp,d,next,n ;move the head into next
MOVE node,d,head,d
INC size,d
BR loop
link: NOP0
;--------------------------------------;this just tacks an element onto the end
MOVE stringrf,d,node,n ;load string into currNode
MOVE 0,i,next,n ;make it's reference null
MOVE node,d,htnext,n ;connect the prev ele
INC size,d ;inc size
BR loop
done: LDA head,d
RET0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment