Completed conversion of new tab/space policy for stage1 assembly programs

This commit is contained in:
Jeremiah Orians 2016-09-05 19:18:20 -04:00
parent 5dd9e79770
commit 0323fa7149
No known key found for this signature in database
GPG Key ID: 7457821534D2ACCD
6 changed files with 376 additions and 319 deletions

View File

@ -1,8 +1,9 @@
:start
LOADUI R15 $stack ; Put stack at end of program
LOADUI R15 $stack ; Put stack at end of program
;; We will be using R14 for our condition codes
;; We will be using R13 for storage of Head
;; Main program
;; Reads contents of Tape_01 and writes desired contents onto Tape_02
;; Accepts no arguments and HALTS when done
@ -17,16 +18,17 @@
CALLI R15 @ReadFile
;; Done reading File
LOADUI R0 0x1100 ; Close TAPE_01
LOADUI R0 0x1100 ; Close TAPE_01
FCLOSE
;; Enter Editor Loop
MOVE R13 R1 ; Set R13 to Head
MOVE R13 R1 ; Set R13 to Head
CALLI R15 @EditorLoop
;; And We are Done
HALT
;; Readfile function
;; Recieves pointer to head in R1
;; Creates Nodes and imports text until EOF
@ -38,7 +40,7 @@
CALLI R15 @malloc
;; Get another line into list
PUSHR R1 R15
LOADUI R1 0x1100 ; Read from tape_01
LOADUI R1 0x1100 ; Read from tape_01
CALLI R15 @Readline
POPR R1 R15
SWAP R0 R1
@ -48,6 +50,7 @@
JUMP.Z R14 @ReadFile
RET R15
;; Readline function
;; Recieves Pointer to node in R0
;; And Input in R1
@ -64,12 +67,12 @@
PUSHR R4 R15
;; Initialize
MOVE R4 R0
FALSE R0 ; Get where space is free
FALSE R0 ; Get where space is free
CALLI R15 @malloc
MOVE R2 R0
FALSE R3
:Readline_0
FGETC ; Read a Char
FGETC ; Read a Char
;; Flag if reached EOF
CMPSKIP.GE R0 0
@ -110,11 +113,11 @@
:Readline_2
;; Set Text pointer
CMPSKIP.E R3 0 ; Don't bother for Empty strings
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
MOVE R0 R3 ; Ensure actually allocates exactly
CALLI R15 @malloc ; the amount of space required
;; Restore Registers
POPR R4 R15
POPR R3 R15
@ -123,6 +126,7 @@
POPR R0 R15
RET R15
;; addline Function
;; Recieves pointers in R0 R1
;; Alters R0 if NULL
@ -152,12 +156,12 @@
:addline_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 @addline_1 ; Move to the next node and try again
JUMP @addline_0 ; Else simply act as if we got this node
; in the first place
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 @addline_1 ; Move to the next node and try again
JUMP @addline_0 ; Else simply act as if we got this node
; in the first place
:addline_2
;; Restore registers
@ -166,6 +170,7 @@
POPR R2 R15
RET R15
;; Primative malloc function
;; Recieves number of bytes to allocate in R0
;; Returns pointer to block of that size in R0
@ -176,7 +181,7 @@
;; Get current malloc pointer
LOADR R1 @malloc_pointer
;; Deal with special case
CMPSKIP.NE R1 0 ; If Zero set to our start of heap space
CMPSKIP.NE R1 0 ; If Zero set to our start of heap space
LOADUI R1 0x4000
;; update malloc pointer
@ -192,14 +197,15 @@
:malloc_pointer
NOP
;; Editor Loop
;; Provides user interaction
;; Requires R13 to be pointer to Head
;; Internally loops
;; Returns nothing
;; Editor Loop
;; Provides user interaction
;; Requires R13 to be pointer to Head
;; Internally loops
;; Returns nothing
:EditorLoop
FALSE R1 ; Read from tty
FGETC ; Read a Char
FALSE R1 ; Read from tty
FGETC ; Read a Char
;; Quit if q
CMPSKIP.NE R0 113
@ -217,7 +223,7 @@
;; Move forward if f
CMPUI R14 R0 102
JUMP.NE R14 @EditorLoop_1
LOAD32 R0 R13 0 ; Load head->next
LOAD32 R0 R13 0 ; Load head->next
;; If head->next isn't null make it the new head
CMPSKIP.E R0 0
@ -228,7 +234,7 @@
;; Move backward if b
CMPUI R14 R0 98
JUMP.NE R14 @EditorLoop_2
LOAD32 R0 R13 4 ; Load head->prev
LOAD32 R0 R13 4 ; Load head->prev
;; If head->prev isn't null make it the new head
CMPSKIP.E R0 0
@ -242,7 +248,7 @@
;; Change Head's Text
COPY R0 R13
FALSE R1 ; Read from tty
FALSE R1 ; Read from tty
CALLI R15 @Readline
JUMP @EditorLoop
@ -261,7 +267,7 @@
CALLI R15 @GetRoot
CALLI R15 @PrintAll
LOADUI R0 0x1101 ; Close TAPE_02
LOADUI R0 0x1101 ; Close TAPE_02
FCLOSE
JUMP @EditorLoop
@ -293,6 +299,7 @@
:EditorLoop_7
JUMP @EditorLoop
;; GetRoot function
;; Walks backwards through nodes until beginning
;; Recieves node pointer in R0 and Returns result in R0
@ -315,6 +322,7 @@
POPR R1 R15
RET R15
;; Printall Function
;; Prints all lines to Interface in R1
;; Starting at node in R0
@ -326,13 +334,13 @@
PUSHR R1 R15
PUSHR R2 R15
:PrintAll_0
LOAD32 R2 R0 0 ; Store Head->Next in R2
LOAD32 R0 R0 8 ; Set R0 to Head->Text
CALLI R15 @PrintLine ; Prints Head->Text
CMPSKIP.NE R2 0 ; If Head->Next is NULL
JUMP @PrintAll_1 ; Stop Looping
MOVE R0 R2 ; Otherwise Move to Next Node
JUMP @PrintAll_0 ; And Loop
LOAD32 R2 R0 0 ; Store Head->Next in R2
LOAD32 R0 R0 8 ; Set R0 to Head->Text
CALLI R15 @PrintLine ; Prints Head->Text
CMPSKIP.NE R2 0 ; If Head->Next is NULL
JUMP @PrintAll_1 ; Stop Looping
MOVE R0 R2 ; Otherwise Move to Next Node
JUMP @PrintAll_0 ; And Loop
:PrintAll_1
;; Restore registers
POPR R2 R15
@ -340,6 +348,7 @@
POPR R0 R15
RET R15
;; Printline function
;; Recieves a string pointer in R0
;; Prints string interface specified in R1
@ -358,13 +367,13 @@
CMPSKIP.NE R2 0
JUMP @PrintLine_1
:PrintLine_0
LOADXU8 R0 R2 R3 ; Load char from string
LOADXU8 R0 R2 R3 ; Load char from string
;; Don't print NULLs
CMPSKIP.NE R0 0
JUMP @PrintLine_1
FPUTC ; Print the char
ADDUI R3 R3 1 ; Prep for next loop
FPUTC ; Print the char
ADDUI R3 R3 1 ; Prep for next loop
JUMP @PrintLine_0
:PrintLine_1
@ -375,6 +384,7 @@
POPR R0 R15
RET R15
;; AppendLine Function
;; Recieves a Node in R0
;; Creates a new Node and appends it
@ -393,13 +403,13 @@
;; Check if head->Next is null
LOAD32 R2 R1 0
CMPSKIP.E R2 0 ; If head->Next is something
STORE32 R0 R2 4 ; Set head->next->prev to p
CMPSKIP.E R2 0 ; If head->Next is something
STORE32 R0 R2 4 ; Set head->next->prev to p
;; Setup p and head
STORE32 R2 R0 0 ; p->next = head->next
STORE32 R1 R0 4 ; p->prev = head
STORE32 R0 R1 0 ; head->next = p
STORE32 R2 R0 0 ; p->next = head->next
STORE32 R1 R0 4 ; p->prev = head
STORE32 R0 R1 0 ; head->next = p
;; Restore Registers
POPR R2 R15
@ -407,6 +417,7 @@
POPR R0 R15
RET R15
;; InsertLine Function
;; Recieves a Node in R0
;; Creates a new Node and prepends it
@ -425,13 +436,13 @@
;; Check if Head->Prev is Null
LOAD32 R2 R1 4
CMPSKIP.E R2 0 ; If head->prev is something
STORE32 R0 R2 0 ; Set head->prev->next to p
CMPSKIP.E R2 0 ; If head->prev is something
STORE32 R0 R2 0 ; Set head->prev->next to p
;; Setup p and head
STORE32 R2 R0 4 ; p->prev = head->prev
STORE32 R1 R0 0 ; p->next = head
STORE32 R0 R1 4 ; head->prev = p
STORE32 R2 R0 4 ; p->prev = head->prev
STORE32 R1 R0 0 ; p->next = head
STORE32 R0 R1 4 ; head->prev = p
;; Restore Registers
POPR R2 R15
@ -450,24 +461,25 @@
PUSHR R2 R15
;; Initialize
MOVE R1 R0
LOAD32 R0 R1 4 ; put p->prev in R0
LOAD32 R2 R1 0 ; put p->next in R2
LOAD32 R0 R1 4 ; put p->prev in R0
LOAD32 R2 R1 0 ; put p->next in R2
;; Keep links
CMPSKIP.E R0 0 ; If p->prev is not null
STORE32 R2 R0 0 ; p->prev->next = p->next
CMPSKIP.E R0 0 ; If p->prev is not null
STORE32 R2 R0 0 ; p->prev->next = p->next
CMPSKIP.E R2 0 ; If p->next is not null
STORE32 R0 R2 4 ; p->next->prev = p->prev
CMPSKIP.E R2 0 ; If p->next is not null
STORE32 R0 R2 4 ; p->next->prev = p->prev
;; Attempt to save what is left of the list
CMPSKIP.NE R0 0 ; If p->prev is null
MOVE R0 R2 ; return p->next
CMPSKIP.NE R0 0 ; If p->prev is null
MOVE R0 R2 ; return p->next
;; Restore Registers
POPR R2 R15
POPR R1 R15
RET R15
;; Where our stack begins
:stack

View File

@ -1,6 +1,7 @@
:start
FALSE R14 ; R14 will be storing our current address
LOADUI R15 $end ; We will be using R15 for our stack
FALSE R14 ; R14 will be storing our current address
LOADUI R15 $end ; We will be using R15 for our stack
;; Main program functionality
;; Reads in Tape_01 and writes out results to Tape_02
@ -16,8 +17,8 @@
;; Perform main loop
:main_0
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
;; Check for EOF
CMPSKIP.GE R0 0
@ -34,48 +35,50 @@
:main_1
;; Close up as we are done
LOADUI R0 0x1100 ; Close TAPE_01
LOADUI R0 0x1100 ; Close TAPE_01
FCLOSE
LOADUI R0 0x1101 ; Close TAPE_02
LOADUI R0 0x1101 ; Close TAPE_02
FCLOSE
HALT
;; Dehex functionality
;; Accepts byte in R0
;; Prints address every 4th byte
;; Alters R0
;; Returns to whatever called it
;; Dehex functionality
;; Accepts byte in R0
;; Prints address every 4th byte
;; Alters R0
;; Returns to whatever called it
:dehex
PUSHR R1 R15 ; Preserve R1
PUSHR R0 R15 ; Save byte until after address
ANDI R0 R14 3 ; Get mod 4 of address
LOADUI R1 0x1101 ; We want it on TAPE_02
CMPSKIP.E R0 0 ; if not zero
JUMP @dehex_0 ; Skip placing address
PUSHR R1 R15 ; Preserve R1
PUSHR R0 R15 ; Save byte until after address
ANDI R0 R14 3 ; Get mod 4 of address
LOADUI R1 0x1101 ; We want it on TAPE_02
CMPSKIP.E R0 0 ; if not zero
JUMP @dehex_0 ; Skip placing address
;; Prepend new line
LOADUI R0 10 ; First print line feed
FPUTC ; Write it
LOADUI R0 10 ; First print line feed
FPUTC ; Write it
;; Write out address
COPY R0 R14 ; Prep for call
CALLI R15 @hex32 ; Let it handle the details
COPY R0 R14 ; Prep for call
CALLI R15 @hex32 ; Let it handle the details
;; Write out : char
LOADUI R0 58 ; Prep
FPUTC ; Write it
LOADUI R0 58 ; Prep
FPUTC ; Write it
;; Write out tab
LOADUI R0 9 ; Prep
FPUTC ; Write it
LOADUI R0 9 ; Prep
FPUTC ; Write it
:dehex_0
POPR R0 R15 ; Restore byte recieved
CALLI R15 @hex8 ; Use a subset
POPR R0 R15 ; Restore byte recieved
CALLI R15 @hex8 ; Use a subset
LOADUI R0 32 ; Prep for writing space
FPUTC ; Write it
POPR R1 R15 ; Restore R1
RET R15 ; Return to caller
LOADUI R0 32 ; Prep for writing space
FPUTC ; Write it
POPR R1 R15 ; Restore R1
RET R15 ; Return to caller
;; hex32 functionality
;; Accepts 32bit value in R0
@ -83,26 +86,27 @@
;; Returns to whatever called it
:hex32
PUSHR R0 R15
SR0I R0 16 ; Do high word first
SR0I R0 16 ; Do high word first
CALLI R15 @hex16
POPR R0 R15
:hex16
PUSHR R0 R15
SR0I R0 8 ; Do high byte first
SR0I R0 8 ; Do high byte first
CALLI R15 @hex8
POPR R0 R15
:hex8
PUSHR R0 R15
SR0I R0 4 ; Do high nybble first
SR0I R0 4 ; Do high nybble first
CALLI R15 @hex4
POPR R0 R15
:hex4
ANDI R0 R0 0x000F ; isolate nybble
ADDUI R0 R0 48 ; convert to ascii
CMPSKIP.LE R0 57 ; If nybble was greater than '9'
ADDUI R0 R0 7 ; Shift it into 'A' range of ascii
FPUTC ; Print char
RET R15 ; Get next nybble or return if done
ANDI R0 R0 0x000F ; isolate nybble
ADDUI R0 R0 48 ; convert to ascii
CMPSKIP.LE R0 57 ; If nybble was greater than '9'
ADDUI R0 R0 7 ; Shift it into 'A' range of ascii
FPUTC ; Print char
RET R15 ; Get next nybble or return if done
;; Where we are putting our stack
:end

View File

@ -1,9 +1,13 @@
:start
LOADUI R2 10 ; We will be using R2 for our counter
LOADUI R2 10 ; We will be using R2 for our counter
;; Prep TAPE_01
LOADUI R0 0x1100
FOPEN_READ
;; Main loop of more functionality
;; Modifies R0, R1 and R2
;; Does not utilize stack or heap
:main
;; Read a byte
LOADUI R1 0x1100
@ -18,20 +22,20 @@
FPUTC
;; Check for LF
CMPSKIP.NE R0 10 ; Skip if not line feed
SUBI R2 R2 1 ; Decrement on line feed
CMPSKIP.NE R0 10 ; Skip if not line feed
SUBI R2 R2 1 ; Decrement on line feed
;; Loop if not Zero
CMPSKIP.E R2 0 ; Skip if counter is zero
CMPSKIP.E R2 0 ; Skip if counter is zero
JUMP @main
;; Otherwise provide main loop functionality
FGETC ; Wait for key press
LOADUI R2 10 ; Reset counter
JUMP @main ; And loop
FGETC ; Wait for key press
LOADUI R2 10 ; Reset counter
JUMP @main ; And loop
:main_1
;; Close up as we are done
LOADUI R0 0x1100 ; Close TAPE_01
LOADUI R0 0x1100 ; Close TAPE_01
FCLOSE
HALT

View File

@ -1,7 +1,7 @@
:start
LOADUI R10 0x0F ; Byte mask
LOADUI R11 1 ; Our toggle
LOADUI R13 0x600 ; Where we are starting our Stack
LOADUI R10 0x0F ; Byte mask
LOADUI R11 1 ; Our toggle
LOADUI R13 0x600 ; Where we are starting our Stack
;; R14 is storing our condition code
;; R15 is storing our nybble
@ -13,9 +13,12 @@
LOADUI R0 0x1101
FOPEN_WRITE
;; Main program loop
;; Halts when done
:loop
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
;; Check for EOF
CMPI R14 R0 0
@ -23,27 +26,32 @@
CALLI R13 @finish
:.L1
LOADUI R1 0 ; Write to Char to TTY
FPUTC ; Print the Char
CALLI R13 @hex ; Convert it
CMPI R14 R0 0 ; Check if it is hex
JUMP.L R14 @loop ; Don't use nonhex chars
JUMP.Z R11 @.L99 ; Jump if toggled
LOADUI R1 0 ; Write to Char to TTY
FPUTC ; Print the Char
CALLI R13 @hex ; Convert it
CMPI R14 R0 0 ; Check if it is hex
JUMP.L R14 @loop ; Don't use nonhex chars
JUMP.Z R11 @.L99 ; Jump if toggled
;; Process first byte of pair
AND R15 R0 R10 ; Store First nibble
FALSE R11 ; Flip the toggle
AND R15 R0 R10 ; Store First nibble
FALSE R11 ; Flip the toggle
JUMP @loop
:.L99
SL0I R15 4 ; Shift our first nibble
AND R0 R0 R10 ; Mask out top
ADD R0 R0 R15 ; Combine nibbles
LOADI R11 1 ; Flip the toggle
LOADUI R1 0x1101 ; Write the combined byte
FPUTC ; To TAPE_02
JUMP @loop ; Try to get more bytes
SL0I R15 4 ; Shift our first nibble
AND R0 R0 R10 ; Mask out top
ADD R0 R0 R15 ; Combine nibbles
LOADI R11 1 ; Flip the toggle
LOADUI R1 0x1101 ; Write the combined byte
FPUTC ; To TAPE_02
JUMP @loop ; Try to get more bytes
;; Hex function
;; Converts Ascii chars to their hex values
;; Or -1 if not a hex char
;; Returns to whatever called it
:hex
;; Deal with line comments starting with #
CMPUI R14 R0 35
@ -85,17 +93,21 @@
TRUE R0
RET R13
:ascii_comment
LOADUI R1 0x1100 ; Read from TAPE_01
FGETC ; Read another char
CMPUI R14 R0 10 ; Stop at the end of line
LOADUI R1 0 ; Write to TTY
FPUTC ; The char we just read
JUMP.NE R14 @ascii_comment ; Otherwise keep looping
LOADUI R1 0x1100 ; Read from TAPE_01
FGETC ; Read another char
CMPUI R14 R0 10 ; Stop at the end of line
LOADUI R1 0 ; Write to TTY
FPUTC ; The char we just read
JUMP.NE R14 @ascii_comment ; Otherwise keep looping
JUMP @ascii_other
;; Finish function
;; Cleans up at the end of the program
;; Performs the HALT
:finish
LOADUI R0 0x1100 ; Close TAPE_01
LOADUI R0 0x1100 ; Close TAPE_01
FCLOSE
LOADUI R0 0x1101 ; Close TAPE_02
LOADUI R0 0x1101 ; Close TAPE_02
FCLOSE
HALT

View File

@ -1,10 +1,10 @@
:start
LOADUI R8 @table ; Where we are putting our address pointers
LOADUI R9 0xFF ; Byte mask
LOADUI R10 0x0F ; nybble mask
LOADUI R11 1 ; Our toggle
FALSE R12 ; Our PC counter
LOADUI R13 0x600 ; Where we are starting our Stack
LOADUI R8 @table ; Where we are putting our address pointers
LOADUI R9 0xFF ; Byte mask
LOADUI R10 0x0F ; nybble mask
LOADUI R11 1 ; Our toggle
FALSE R12 ; Our PC counter
LOADUI R13 0x600 ; Where we are starting our Stack
;; We will be using R14 for our condition codes
;; We will be using R15 for holding our processed nybbles
@ -16,57 +16,58 @@
LOADUI R0 0x1101
FOPEN_WRITE
;; Function for collecting the address of all labels
:getLables
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
;; Check for EOF
CMPSKIP.GE R0 0
JUMP @stage2
;; Check for Label
CMPSKIP.NE R0 58 ; If the Char is : the next char is the label
CMPSKIP.NE R0 58 ; If the Char is : the next char is the label
CALLI R13 @storeLabel
;; Check for pointer to label
CMPUI R14 R0 64 ; If the Char is @ the next char is the pointer to a label
CMPUI R14 R0 64 ; If the Char is @ the next char is the pointer to a label
JUMP.NE R14 @.L0
;; Ignore the pointer for now
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
ADDUI R12 R12 2 ; The pointer will end up taking 2 bytes
ADDUI R12 R12 2 ; The pointer will end up taking 2 bytes
JUMP @getLables
:.L0
;; Otherwise attempt to process
CALLI R13 @hex ; Convert it
CMPSKIP.GE R0 0 ; Don't record, nonhex values
JUMP @getLables ; Move onto Next char
CALLI R13 @hex ; Convert it
CMPSKIP.GE R0 0 ; Don't record, nonhex values
JUMP @getLables ; Move onto Next char
;; Determine if we got a full byte
JUMP.Z R11 @.L1 ; Jump if toggled
JUMP.Z R11 @.L1 ; Jump if toggled
;; Deal with case of first half of byte
FALSE R11 ; Flip the toggle
FALSE R11 ; Flip the toggle
JUMP @getLables
:.L1
;; Deal with case of second half of byte
TRUE R11 ; Flip the toggle
ADDUI R12 R12 1 ; increment PC now that we have a full byte
TRUE R11 ; Flip the toggle
ADDUI R12 R12 1 ; increment PC now that we have a full byte
JUMP @getLables
;; Function for storing the address of the label
:storeLabel
;; Get the char of the Label
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
;; We require 4 bytes to store the pointer values
SL0I R0 2 ; Thus we multiply our label by 4
SL0I R0 2 ; Thus we multiply our label by 4
;; Store the current Program counter
STOREX R12 R8 R0
@ -81,85 +82,89 @@
REWIND
;; Reset our toggle and counter, just in case
LOADUI R11 1 ; Our toggle
FALSE R12 ; Our PC counter
LOADUI R11 1 ; Our toggle
FALSE R12 ; Our PC counter
:loop
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
;; Check for EOF
CMPSKIP.GE R0 0
JUMP @finish
;; Check for Label
CMPUI R14 R0 58 ; Make sure we jump over the label
CMPUI R14 R0 58 ; Make sure we jump over the label
JUMP.NE R14 @.L97
;; Consume next char
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
JUMP @loop
:.L97
;; Check for Pointer
CMPUI R14 R0 64 ; If it is a pointer Deal with it
JUMP.NE R14 @.L98 ; Otherwise attempt to process it
CMPUI R14 R0 64 ; If it is a pointer Deal with it
JUMP.NE R14 @.L98 ; Otherwise attempt to process it
CALLI R13 @storePointer
JUMP @loop
:.L98
;; Process Char
LOADUI R1 0 ; Write to Char to TTY
FPUTC ; Print the Char
CALLI R13 @hex ; Convert it
CMPI R14 R0 0 ; Check if it is hex
JUMP.L R14 @loop ; Don't use nonhex chars
JUMP.Z R11 @.L99 ; Jump if toggled
LOADUI R1 0 ; Write to Char to TTY
FPUTC ; Print the Char
CALLI R13 @hex ; Convert it
CMPI R14 R0 0 ; Check if it is hex
JUMP.L R14 @loop ; Don't use nonhex chars
JUMP.Z R11 @.L99 ; Jump if toggled
;; Process first byte of pair
AND R15 R0 R10 ; Store First nibble
FALSE R11 ; Flip the toggle
AND R15 R0 R10 ; Store First nibble
FALSE R11 ; Flip the toggle
JUMP @loop
:.L99
SL0I R15 4 ; Shift our first nibble
AND R0 R0 R10 ; Mask out top
ADD R0 R0 R15 ; Combine nibbles
TRUE R11 ; Flip the toggle
LOADUI R1 0x1101 ; Write the combined byte
FPUTC ; To TAPE_02
ADDUI R12 R12 1 ; increment PC now that we have a full byte
JUMP @loop ; Try to get more bytes
SL0I R15 4 ; Shift our first nibble
AND R0 R0 R10 ; Mask out top
ADD R0 R0 R15 ; Combine nibbles
TRUE R11 ; Flip the toggle
LOADUI R1 0x1101 ; Write the combined byte
FPUTC ; To TAPE_02
ADDUI R12 R12 1 ; increment PC now that we have a full byte
JUMP @loop ; Try to get more bytes
:storePointer
;; Correct the PC to reflect the size of the pointer
ADDUI R12 R12 2 ; Exactly 2 bytes
ADDUI R12 R12 2 ; Exactly 2 bytes
;; Get the char of the Label
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
;; Since we stored a full pointer taking up 4 bytes
SL0I R0 2 ; Thus we multiply our label by 4 to get where it is stored
LOADX R2 R8 R0 ; Load the address of the label
SL0I R0 2 ; Thus we multiply our label by 4 to get where it is stored
LOADX R2 R8 R0 ; Load the address of the label
;; We now have to calculate the distance and store the 2 bytes
SUB R2 R2 R12 ; First determine the difference between the current PC and the stored PC of the label
ADDUI R2 R2 4 ; Adjust for relative positioning
SUB R2 R2 R12 ; First determine the difference between the current PC and the stored PC of the label
ADDUI R2 R2 4 ; Adjust for relative positioning
;; Store Upper byte
COPY R0 R2
SARI R0 8 ; Drop the bottom 8 bits
AND R0 R0 R9 ; Mask out everything but bottom bits
LOADUI R1 0x1101 ; Write the byte
FPUTC ; To TAPE_02
SARI R0 8 ; Drop the bottom 8 bits
AND R0 R0 R9 ; Mask out everything but bottom bits
LOADUI R1 0x1101 ; Write the byte
FPUTC ; To TAPE_02
;; Store Lower byte
AND R0 R2 R9 ; Drop everything but the bottom 8 bits
FPUTC ; Write the byte to TAPE_02
AND R0 R2 R9 ; Drop everything but the bottom 8 bits
FPUTC ; Write the byte to TAPE_02
RET R13
;; Hex function
;; Returns hex value of ascii char
;; Or -1 if not a hex char
:hex
;; Deal with line comments starting with #
CMPUI R14 R0 35
@ -201,20 +206,21 @@
TRUE R0
RET R13
:ascii_comment
LOADUI R1 0x1100 ; Read from TAPE_01
FGETC ; Read another char
CMPUI R14 R0 10 ; Stop at the end of line
LOADUI R1 0 ; Write to TTY
FPUTC ; The char we just read
JUMP.NE R14 @ascii_comment ; Otherwise keep looping
LOADUI R1 0x1100 ; Read from TAPE_01
FGETC ; Read another char
CMPUI R14 R0 10 ; Stop at the end of line
LOADUI R1 0 ; Write to TTY
FPUTC ; The char we just read
JUMP.NE R14 @ascii_comment ; Otherwise keep looping
JUMP @ascii_other
:finish
LOADUI R0 0x1100 ; Close TAPE_01
LOADUI R0 0x1100 ; Close TAPE_01
FCLOSE
LOADUI R0 0x1101 ; Close TAPE_02
LOADUI R0 0x1101 ; Close TAPE_02
FCLOSE
HALT
;; Where all of our pointers will be stored for our locations
;; Where all of our pointers will be stored for our locations
:table

View File

@ -1,7 +1,9 @@
:start
LOADUI R13 @table ; Where we are putting our table
LOADUI R13 @table ; Where we are putting our table
;; We will be using R14 for our condition codes
LOADUI R15 0x7FFF ; We will be using R15 for our stack
LOADUI R15 0x7FFF ; We will be using R15 for our stack
;; Main program functionality
;; Reads in Tape_01 and writes out results onto Tape_02
;; Accepts no arguments and HALTS when done
@ -11,9 +13,9 @@
FOPEN_READ
;; Intialize environment
FALSE R12 ; Set holder to zero
FALSE R11 ; Set PC counter to zero
LOADUI R10 1 ; Our toggle
FALSE R12 ; Set holder to zero
FALSE R11 ; Set PC counter to zero
LOADUI R10 1 ; Our toggle
;; Perform first pass
CALLI R15 @first_pass
@ -23,11 +25,11 @@
REWIND
;; Reintialize environment
FALSE R12 ; Set holder to zero
FALSE R11 ; Set PC counter to zero
LOADUI R10 1 ; Our toggle
LOADUI R9 0xFF ; Byte mask
LOADUI R8 0x0F ; nybble mask
FALSE R12 ; Set holder to zero
FALSE R11 ; Set PC counter to zero
LOADUI R10 1 ; Our toggle
LOADUI R9 0xFF ; Byte mask
LOADUI R8 0x0F ; nybble mask
;; Prep TAPE_02
LOADUI R0 0x1101
@ -36,19 +38,20 @@
CALLI R15 @second_pass
;; Close up as we are done
LOADUI R0 0x1100 ; Close TAPE_01
LOADUI R0 0x1100 ; Close TAPE_01
FCLOSE
LOADUI R0 0x1101 ; Close TAPE_02
LOADUI R0 0x1101 ; Close TAPE_02
FCLOSE
HALT
;; First pass function
;; Reads Tape_01 and creates our label table
;; Will Overwrite R0 R10 R11
;; Returns to Main function when done
:first_pass
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
;; Check for EOF
CMPSKIP.GE R0 0
@ -72,31 +75,32 @@
JUMP @ThrowAwayAddress
;; Otherwise attempt to process
CALLI R15 @hex ; Convert it
CMPSKIP.GE R0 0 ; Don't record, nonhex values
JUMP @first_pass ; Move onto Next char
CALLI R15 @hex ; Convert it
CMPSKIP.GE R0 0 ; Don't record, nonhex values
JUMP @first_pass ; Move onto Next char
;; Determine if we got a full byte
JUMP.Z R10 @first_pass_0 ; Jump if toggled
JUMP.Z R10 @first_pass_0 ; Jump if toggled
;; Deal with case of first half of byte
FALSE R10 ; Flip the toggle
FALSE R10 ; Flip the toggle
JUMP @first_pass
:first_pass_0
;; Deal with case of second half of byte
TRUE R10 ; Flip the toggle
ADDUI R11 R11 1 ; increment PC now that that we have a full byte
TRUE R10 ; Flip the toggle
ADDUI R11 R11 1 ; increment PC now that that we have a full byte
JUMP @first_pass
;; Second pass function
;; Reads from Tape_01 and uses the values in the table
;; To write desired contents onto Tape_02
;; Will Overwrite R0 R10 R11
;; Returns to Main function when done
:second_pass
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
;; Check for EOF
CMPSKIP.GE R0 0
@ -107,58 +111,59 @@
JUMP @ThrowAwayLabel
;; Check for and deal with Pointers to labels
CMPSKIP.NE R0 64 ; @ for relative
CMPSKIP.NE R0 64 ; @ for relative
JUMP @StoreRelativePointer
CMPSKIP.NE R0 36 ; $ for absolute
CMPSKIP.NE R0 36 ; $ for absolute
JUMP @StoreAbsolutePointer
CMPSKIP.NE R0 38 ; & for address
CMPSKIP.NE R0 38 ; & for address
JUMP @StoreAbsoluteAddress
;; Process everything else
CALLI R15 @hex ; Attempt to Convert it
CMPSKIP.GE R0 0 ; Don't record, nonhex values
JUMP @second_pass ; Move onto Next char
CALLI R15 @hex ; Attempt to Convert it
CMPSKIP.GE R0 0 ; Don't record, nonhex values
JUMP @second_pass ; Move onto Next char
;; Determine if we got a full byte
JUMP.Z R10 @second_pass_0 ; Jump if toggled
JUMP.Z R10 @second_pass_0 ; Jump if toggled
;; Deal with case of first half of byte
AND R12 R0 R8 ; Store our first nibble
FALSE R10 ; Flip the toggle
AND R12 R0 R8 ; Store our first nibble
FALSE R10 ; Flip the toggle
JUMP @second_pass
:second_pass_0
;; Deal with case of second half of byte
SL0I R12 4 ; Shift our first nybble
AND R0 R0 R8 ; Mask out top
ADD R0 R0 R12 ; Combine nybbles
TRUE R10 ; Flip the toggle
LOADUI R1 0x1101 ; Write the combined byte
FPUTC ; To TAPE_02
ADDUI R11 R11 1 ; increment PC now that that we have a full byte
SL0I R12 4 ; Shift our first nybble
AND R0 R0 R8 ; Mask out top
ADD R0 R0 R12 ; Combine nybbles
TRUE R10 ; Flip the toggle
LOADUI R1 0x1101 ; Write the combined byte
FPUTC ; To TAPE_02
ADDUI R11 R11 1 ; increment PC now that that we have a full byte
JUMP @second_pass
;; Store Label function
;; Writes out the token and the current PC value
;; Its static variable for storing the next index to be used
;; Will overwrite R0
;; Returns to first pass when done
:storeLabel
LOADR R0 @current_index ; Get address of first open index
CMPSKIP.NE R0 0 ; If zero intialize from R13
LOADR R0 @current_index ; Get address of first open index
CMPSKIP.NE R0 0 ; If zero intialize from R13
COPY R0 R13
;; Store the PC of the label
STORE32 R11 R0 0
;; Store the name of the Label
ADDUI R0 R0 4 ; Increment the offset of the index
ADDUI R0 R0 4 ; Increment the offset of the index
CALLI R15 @writeout_token
;; Update our index
ADDUI R0 R0 60 ; Hopefully our tokens are less than 60 bytes long
ADDUI R0 R0 60 ; Hopefully our tokens are less than 60 bytes long
STORER R0 @current_index
;; And be done
JUMP @first_pass
@ -167,6 +172,7 @@
:current_index
NOP
;; StoreRelativepointer function
;; Deals with the special case of relative pointers
;; Clears Temp
@ -177,17 +183,18 @@
;; Jumps back into Pass2
:StoreRelativePointer
;; Correct the PC to reflect the size of the pointer
ADDUI R11 R11 2 ; Exactly 2 bytes
LOADUI R0 $Temp ; Set where we want to shove our string
CALLI R15 @Clear_string ; Clear it
CALLI R15 @writeout_token ; Write it
CALLI R15 @Match_string ; Find the Match
LOAD32 R0 R0 -4 ; Get the value we care about
SUB R0 R0 R11 ; Determine the difference
ADDUI R0 R0 4 ; Adjust for relative positioning
ADDUI R11 R11 2 ; Exactly 2 bytes
LOADUI R0 $Temp ; Set where we want to shove our string
CALLI R15 @Clear_string ; Clear it
CALLI R15 @writeout_token ; Write it
CALLI R15 @Match_string ; Find the Match
LOAD32 R0 R0 -4 ; Get the value we care about
SUB R0 R0 R11 ; Determine the difference
ADDUI R0 R0 4 ; Adjust for relative positioning
CALLI R15 @ProcessImmediate ; Write out the value
JUMP @second_pass
;; StoreAbsolutepointer function
;; Deals with the special case of absolute pointers
;; Clears Temp
@ -198,15 +205,16 @@
;; Jumps back into Pass2
:StoreAbsolutePointer
;; Correct the PC to reflect the size of the pointer
ADDUI R11 R11 2 ; Exactly 2 bytes
LOADUI R0 $Temp ; Set where we want to shove our string
CALLI R15 @Clear_string ; Clear it
CALLI R15 @writeout_token ; Write it
CALLI R15 @Match_string ; Find the Match
LOAD32 R0 R0 -4 ; Get the value we care about
ADDUI R11 R11 2 ; Exactly 2 bytes
LOADUI R0 $Temp ; Set where we want to shove our string
CALLI R15 @Clear_string ; Clear it
CALLI R15 @writeout_token ; Write it
CALLI R15 @Match_string ; Find the Match
LOAD32 R0 R0 -4 ; Get the value we care about
CALLI R15 @ProcessImmediate ; Write out the value
JUMP @second_pass
;; StoreAbsoluteAddress function
;; Deal with the special case of absolute Addresses
;; Clear Temp
@ -217,22 +225,23 @@
;; Jumpbacs back into Pass2
:StoreAbsoluteAddress
;; COrrect the PC to reflect the size of the address
ADDUI R11 R11 4 ; 4 Bytes on 32bit machines
LOADUI R0 $Temp ; Set where we ant to shove our string
CALLI R15 @Clear_string ; Clear it
CALLI R15 @writeout_token ; Write it
CALLI R15 @Match_string ; Find the Match
PUSHR R14 R15 ; Get a temp storage place
LOAD32 R14 R0 -4 ; Get the value we care about
COPY R0 R14 ; We need to print the top 2 bytes first
SARI R0 16 ; Drop bottom 16 bits
ADDUI R11 R11 4 ; 4 Bytes on 32bit machines
LOADUI R0 $Temp ; Set where we ant to shove our string
CALLI R15 @Clear_string ; Clear it
CALLI R15 @writeout_token ; Write it
CALLI R15 @Match_string ; Find the Match
PUSHR R14 R15 ; Get a temp storage place
LOAD32 R14 R0 -4 ; Get the value we care about
COPY R0 R14 ; We need to print the top 2 bytes first
SARI R0 16 ; Drop bottom 16 bits
CALLI R15 @ProcessImmediate ; Write out top 2 bytes
LOADUI R0 0xFFFF ; Provide mask to keep bottom 2 bytes
AND R0 R0 R14 ; Drop top 16 bits
POPR R14 R15 ; Restore R14
LOADUI R0 0xFFFF ; Provide mask to keep bottom 2 bytes
AND R0 R0 R14 ; Drop top 16 bits
POPR R14 R15 ; Restore R14
CALLI R15 @ProcessImmediate ; Write out bottom 2 bytes
JUMP @second_pass
;; Writeout Token Function
;; Writes the Token [minus first char] to the address
;; It recieves in R0 until it reaches a delimiter
@ -245,25 +254,25 @@
PUSHR R2 R15
;; Initialize
MOVE R2 R0 ; Set R2 as our index
LOADUI R1 0x1100 ; Read from tape_01
MOVE R2 R0 ; Set R2 as our index
LOADUI R1 0x1100 ; Read from tape_01
;; Our core loop
:writeout_token_0
FGETC ; Get another byte
FGETC ; Get another byte
;; Deal with termination cases
CMPSKIP.NE R0 32 ; Finished if space
CMPSKIP.NE R0 32 ; Finished if space
JUMP @writeout_token_done
CMPSKIP.NE R0 9 ; Finished if tab
CMPSKIP.NE R0 9 ; Finished if tab
JUMP @writeout_token_done
CMPSKIP.NE R0 10 ; Finished if newline
CMPSKIP.NE R0 10 ; Finished if newline
JUMP @writeout_token_done
;; Deal with valid input
STORE8 R0 R2 0 ; Write out the byte
ADDUI R2 R2 1 ; Increment
JUMP @writeout_token_0 ; Keep looping
STORE8 R0 R2 0 ; Write out the byte
ADDUI R2 R2 1 ; Increment
JUMP @writeout_token_0 ; Keep looping
;; Clean up now that we are done
:writeout_token_done
@ -274,6 +283,7 @@
;; And be done
RET R15
;; Clear string function
;; Clears string pointed at by the value of R0
;; Until a null character is reached
@ -290,10 +300,10 @@
LOADUI R2 0
LOADUI R3 0
:clear_byte
LOADXU8 R0 R1 R2 ; Get the byte
STOREX8 R3 R1 R2 ; Overwrite with a Zero
ADDUI R2 R2 1 ; Prep for next loop
JUMP.NZ R0 @clear_byte ; Stop if byte is NULL
LOADXU8 R0 R1 R2 ; Get the byte
STOREX8 R3 R1 R2 ; Overwrite with a Zero
ADDUI R2 R2 1 ; Prep for next loop
JUMP.NZ R0 @clear_byte ; Stop if byte is NULL
;; Done
;; Restore registers
POPR R3 R15
@ -302,6 +312,7 @@
POPR R0 R15
RET R15
;; Match string function
;; Walks down table until match is found
;; Then returns address of matching string in R0
@ -312,19 +323,19 @@
PUSHR R2 R15
;; Initialize for Loop
LOADUI R1 $Temp ; We always compare against Temp
LOADUI R2 $table ; Begin at start of table
ADDUI R2 R2 4 ; Where the string is located
LOADUI R1 $Temp ; We always compare against Temp
LOADUI R2 $table ; Begin at start of table
ADDUI R2 R2 4 ; Where the string is located
;; Loop until we find a match
:Match_string_0
COPY R0 R2 ; Set R0 to our current string
COPY R0 R2 ; Set R0 to our current string
CALLI R15 @strcmp
JUMP.E R0 @Match_string_1 ; It is a match!
JUMP.E R0 @Match_string_1 ; It is a match!
;; Prepare for next loop
LOADUI R1 $Temp ; That function clears R1
ADDUI R2 R2 64 ; Each Index is 64 bytes
JUMP @Match_string_0 ; Keep looping
LOADUI R1 $Temp ; That function clears R1
ADDUI R2 R2 64 ; Each Index is 64 bytes
JUMP @Match_string_0 ; Keep looping
:Match_string_1
;; Store the correct answer
@ -334,6 +345,7 @@
POPR R1 R15
RET R15
;; Our simple string compare function
;; Recieves two pointers in R0 and R1
;; Returns the difference between the strings in R0
@ -349,20 +361,21 @@
MOVE R3 R1
LOADUI R4 0
:cmpbyte
LOADXU8 R0 R2 R4 ; Get a byte of our first string
LOADXU8 R1 R3 R4 ; Get a byte of our second string
ADDUI R4 R4 1 ; Prep for next loop
CMP R1 R0 R1 ; Compare the bytes
CMPSKIP.E R0 0 ; Stop if byte is NULL
JUMP.E R1 @cmpbyte ; Loop if bytes are equal
LOADXU8 R0 R2 R4 ; Get a byte of our first string
LOADXU8 R1 R3 R4 ; Get a byte of our second string
ADDUI R4 R4 1 ; Prep for next loop
CMP R1 R0 R1 ; Compare the bytes
CMPSKIP.E R0 0 ; Stop if byte is NULL
JUMP.E R1 @cmpbyte ; Loop if bytes are equal
;; Done
MOVE R0 R1 ; Prepare for return
MOVE R0 R1 ; Prepare for return
;; Restore registers
POPR R4 R15
POPR R3 R15
POPR R2 R15
RET R15
;; Processimmediate Function
;; Recieves an integer value in R0
;; Writes out the values to Tape_02
@ -374,17 +387,17 @@
PUSHR R1 R15
PUSHR R2 R15
;; Break up Immediate
AND R2 R0 R9 ; Put lower byte in R2
SARI R0 8 ; Drop Bottom byte from R0
AND R0 R0 R9 ; Maskout everything outside of top byte
AND R2 R0 R9 ; Put lower byte in R2
SARI R0 8 ; Drop Bottom byte from R0
AND R0 R0 R9 ; Maskout everything outside of top byte
;; Write out Top Byte
LOADUI R1 0x1101 ; Write the byte
FPUTC ; To TAPE_02
LOADUI R1 0x1101 ; Write the byte
FPUTC ; To TAPE_02
;; Write out bottom Byte
MOVE R0 R2 ; Put Lower byte in R0
LOADUI R1 0x1101 ; Write the byte
FPUTC ; To TAPE_02
MOVE R0 R2 ; Put Lower byte in R0
LOADUI R1 0x1101 ; Write the byte
FPUTC ; To TAPE_02
;; Restore registers
POPR R2 R15
@ -393,15 +406,17 @@
;; Be Done
RET R15
;; ThrowAwaypointer function
;; Handle the special case of a generic problem
;; for Pass1, Will update R11 and modify R0
;; Will return to the start of first_pass
;; Never call this function, only jump to it
:ThrowAwayPointer
ADDUI R11 R11 2 ; Pointers always take up 2 bytes
CALLI R15 @throwAwayToken ; Get rid of rest of token
JUMP @first_pass ; Then return to the proper place
ADDUI R11 R11 2 ; Pointers always take up 2 bytes
CALLI R15 @throwAwayToken ; Get rid of rest of token
JUMP @first_pass ; Then return to the proper place
;; ThrowAwayAddress function
;; Handle the case of a 32bit absolute address storage
@ -409,9 +424,10 @@
;; Will return to the start of first_pass
;; Never call this function, conly jump to it
:ThrowAwayAddress
ADDUI R11 R11 4 ; Addresses on 32bit systems take up 4 bytes
CALLI R15 @throwAwayToken ; Get rid of rest of token
JUMP @first_pass ; Then return to the proper place
ADDUI R11 R11 4 ; Addresses on 32bit systems take up 4 bytes
CALLI R15 @throwAwayToken ; Get rid of rest of token
JUMP @first_pass ; Then return to the proper place
;; ThrowAwaylabel function
;; Handle the special case of a generic problem
@ -419,7 +435,7 @@
;; Will return to the start of second_pass
;; Never call this function, only jump to it
:ThrowAwayLabel
CALLI R15 @throwAwayToken ; Get rid of rest of token
CALLI R15 @throwAwayToken ; Get rid of rest of token
JUMP @second_pass
;; Throw away token function
@ -428,8 +444,8 @@
;; This Will alter the values of R0 R1
;; Returns back to whatever called it
:throwAwayToken
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char
;; Stop looping if space
CMPSKIP.NE R0 32
@ -495,12 +511,13 @@
TRUE R0
RET R15
:ascii_comment
LOADUI R1 0x1100 ; Read from TAPE_01
FGETC ; Read another char
CMPUI R14 R0 10 ; Stop at the end of line
JUMP.NE R14 @ascii_comment ; Otherwise keep looping
LOADUI R1 0x1100 ; Read from TAPE_01
FGETC ; Read another char
CMPUI R14 R0 10 ; Stop at the end of line
JUMP.NE R14 @ascii_comment ; Otherwise keep looping
JUMP @ascii_other
;; Where we are storing our Temp
:Temp
NOP
@ -519,5 +536,7 @@
NOP
NOP
NOP
;; Where we will putting our Table
:table