A couple of useful functions that may be of use
This commit is contained in:
parent
0323fa7149
commit
1a32cfa8c9
|
@ -0,0 +1,42 @@
|
|||
;; add_node Function
|
||||
;; Recieves pointers in R0 R1
|
||||
;; Alters R0 if NULL
|
||||
;; Appends nodes together
|
||||
;; Returns to whatever called it
|
||||
:add_node
|
||||
;; Preserve Registers
|
||||
PUSHR R2 R15
|
||||
PUSHR R1 R15
|
||||
PUSHR R0 R15
|
||||
|
||||
;; Handle if Head is NULL
|
||||
JUMP.NZ R0 @add_node_0
|
||||
POPR R0 R15
|
||||
PUSHR R1 R15
|
||||
JUMP @add_node_2
|
||||
|
||||
:add_node_0
|
||||
;; Handle if Head->next is NULL
|
||||
LOAD32 R2 R0 0
|
||||
JUMP.NZ R2 @add_node_1
|
||||
;; Set head->next = p
|
||||
STORE32 R1 R0 0
|
||||
;; Set p->prev = head
|
||||
STORE32 R0 R1 4
|
||||
JUMP @add_node_2
|
||||
|
||||
:add_node_1
|
||||
;; Handle case of Head->next not being NULL
|
||||
LOAD32 R0 R0 0 ; Move to next node
|
||||
LOAD32 R2 R0 0 ; Get node->next
|
||||
CMPSKIP.E R2 0 ; If it is not null
|
||||
JUMP @add_node_1 ; Move to the next node and try again
|
||||
JUMP @add_node_0 ; Else simply act as if we got this node
|
||||
; in the first place
|
||||
|
||||
:add_node_2
|
||||
;; Restore registers
|
||||
POPR R0 R15
|
||||
POPR R1 R15
|
||||
POPR R2 R15
|
||||
RET R15
|
|
@ -0,0 +1,75 @@
|
|||
;; Readline function
|
||||
;; Recieves Pointer to node in R0
|
||||
;; And Input in R1
|
||||
;; Allocates Text segment on Heap
|
||||
;; Sets node's pointer to Text segment
|
||||
;; Sets R14 to True if EOF reached
|
||||
;; Requires a malloc function to exist
|
||||
;; Returns to whatever called it
|
||||
:Readline
|
||||
;; Preserve registers
|
||||
PUSHR R0 R15
|
||||
PUSHR R1 R15
|
||||
PUSHR R2 R15
|
||||
PUSHR R3 R15
|
||||
PUSHR R4 R15
|
||||
;; Initialize
|
||||
MOVE R4 R0
|
||||
FALSE R0 ; Get where space is free
|
||||
CALLI R15 @malloc
|
||||
MOVE R2 R0
|
||||
FALSE R3
|
||||
:Readline_0
|
||||
FGETC ; Read a Char
|
||||
|
||||
;; Flag if reached EOF
|
||||
CMPSKIP.GE R0 0
|
||||
TRUE R14
|
||||
|
||||
;; Stop if EOF
|
||||
CMPSKIP.GE R0 0
|
||||
JUMP @Readline_2
|
||||
|
||||
;; Handle Backspace
|
||||
CMPSKIP.E R0 127
|
||||
JUMP @Readline_1
|
||||
|
||||
;; Move back 1 character if R3 > 0
|
||||
CMPSKIP.LE R3 0
|
||||
SUBUI R3 R3 1
|
||||
|
||||
;; Hopefully they keep typing
|
||||
JUMP @Readline_0
|
||||
|
||||
:Readline_1
|
||||
;; Replace all CR with LF
|
||||
CMPSKIP.NE R0 13
|
||||
LOADUI R0 10
|
||||
|
||||
;; Store the Byte
|
||||
STOREX8 R0 R2 R3
|
||||
|
||||
;; Prep for next loop
|
||||
ADDUI R3 R3 1
|
||||
|
||||
;; Check for EOL
|
||||
CMPSKIP.NE R0 10
|
||||
JUMP @Readline_2
|
||||
|
||||
;; Otherwise loop
|
||||
JUMP @Readline_0
|
||||
|
||||
:Readline_2
|
||||
;; Set Text pointer
|
||||
CMPSKIP.E R3 0 ; Don't bother for Empty strings
|
||||
STORE32 R2 R4 8
|
||||
;; Correct Malloc
|
||||
MOVE R0 R3 ; Ensure actually allocates exactly
|
||||
CALLI R15 @malloc ; the amount of space required
|
||||
;; Restore Registers
|
||||
POPR R4 R15
|
||||
POPR R3 R15
|
||||
POPR R2 R15
|
||||
POPR R1 R15
|
||||
POPR R0 R15
|
||||
RET R15
|
Loading…
Reference in New Issue