Updated syntax of assembler to simplify task of identifying labels

This commit is contained in:
Jeremiah Orians 2016-07-01 17:14:14 -04:00
parent 321ee1d3e3
commit 9c19a41d2c
No known key found for this signature in database
GPG Key ID: 7457821534D2ACCD
4 changed files with 60 additions and 49 deletions

23
asm.c
View File

@ -11,20 +11,20 @@ uint32_t ip;
struct Token struct Token
{ {
char Text[max_string + 1]; struct Token* next;
char Expression[max_string + 1]; struct Token* prev;
uint32_t type; uint32_t type;
uint32_t size; uint32_t size;
uint32_t address; uint32_t address;
struct Token* next; char Text[max_string + 1];
struct Token* prev; char Expression[max_string + 1];
}; };
enum type enum type
{ {
EOL = 1, EOL = 1,
comment = (1 << 1), comment = (1 << 1),
address = (1 << 2) label = (1 << 2)
}; };
struct Token* newToken() struct Token* newToken()
@ -113,6 +113,11 @@ Restart:
p->type = p->type | comment; p->type = p->type | comment;
store[i] = (char)c; store[i] = (char)c;
} }
else if((!(p->type & comment )) && (58 == c))
{
p->type = p->type | label;
store[i] = (char)c;
}
else else
{ {
store[i] = (char)c; store[i] = (char)c;
@ -379,7 +384,7 @@ void assign_addresses(struct Token* p)
uint32_t get_address(struct Token* p, char match[]) uint32_t get_address(struct Token* p, char match[])
{ {
if(0 == strncmp(p->Text, match, max_string)) if((label & p->type) && (0 == strncmp(p->Text, match, max_string)))
{ {
return p->address; return p->address;
} }
@ -396,9 +401,13 @@ void update_jumps(struct Token* head, struct Token* p)
{ {
uint32_t dest = -1; uint32_t dest = -1;
if('@' == p->Text[0]) if('@' == p->Text[0])
{ {
dest = get_address(head, p->Text + 1); char temp[256] = {0};
strncpy(temp, p->Text, max_string);
temp[0] = ':';
dest = get_address(head, temp);
} }
if(-1 != (int32_t)dest) if(-1 != (int32_t)dest)

View File

@ -1,4 +1,4 @@
start :start
LOADUI R10 0x0F ; Byte mask LOADUI R10 0x0F ; Byte mask
LOADUI R11 1 ; Our toggle LOADUI R11 1 ; Our toggle
LOADUI R13 0x600 ; Where we are starting our Stack LOADUI R13 0x600 ; Where we are starting our Stack
@ -13,7 +13,7 @@ start
LOADUI R0 0x1101 LOADUI R0 0x1101
FOPEN_WRITE FOPEN_WRITE
loop :loop
LOADUI R1 0 ; Read from tty LOADUI R1 0 ; Read from tty
FGETC ; Read a Char FGETC ; Read a Char
@ -22,12 +22,12 @@ loop
JUMP.NE R14 @.L0 JUMP.NE R14 @.L0
CALLI R13 @finish CALLI R13 @finish
.L0 :.L0
;; Check for EOF ;; Check for EOF
JUMP.P R0 @.L1 JUMP.P R0 @.L1
CALLI R13 @finish CALLI R13 @finish
.L1 :.L1
LOADUI R1 0x1101 ; Write to TAPE_02 LOADUI R1 0x1101 ; Write to TAPE_02
FPUTC ; Print the Char FPUTC ; Print the Char
CALLI R13 @hex ; Convert it CALLI R13 @hex ; Convert it
@ -39,7 +39,7 @@ loop
FALSE R11 ; Flip the toggle FALSE R11 ; Flip the toggle
JUMP @loop JUMP @loop
.L99 :.L99
SL0I R15 4 ; Shift our first nibble SL0I R15 4 ; Shift our first nibble
AND R0 R0 R10 ; Mask out top AND R0 R0 R10 ; Mask out top
ADD R0 R0 R15 ; Combine nibbles ADD R0 R0 R15 ; Combine nibbles
@ -48,7 +48,7 @@ loop
FPUTC ; To TAPE_01 FPUTC ; To TAPE_01
JUMP @loop ; Try to get more bytes JUMP @loop ; Try to get more bytes
hex :hex
;; Deal with line comments starting with # ;; Deal with line comments starting with #
CMPUI R14 R0 35 CMPUI R14 R0 35
JUMP.E R14 @ascii_comment JUMP.E R14 @ascii_comment
@ -76,19 +76,19 @@ hex
;; Ignore the rest ;; Ignore the rest
JUMP @ascii_other JUMP @ascii_other
ascii_num :ascii_num
SUBUI R0 R0 48 SUBUI R0 R0 48
RET R13 RET R13
ascii_low :ascii_low
SUBUI R0 R0 87 SUBUI R0 R0 87
RET R13 RET R13
ascii_high :ascii_high
SUBUI R0 R0 55 SUBUI R0 R0 55
RET R13 RET R13
ascii_other :ascii_other
TRUE R0 TRUE R0
RET R13 RET R13
ascii_comment :ascii_comment
LOADUI R1 0 ; Read from tty LOADUI R1 0 ; Read from tty
FGETC ; Read another char FGETC ; Read another char
CMPUI R14 R0 10 ; Stop at the end of line CMPUI R14 R0 10 ; Stop at the end of line
@ -97,7 +97,7 @@ ascii_comment
JUMP.NE R14 @ascii_comment ; Otherwise keep looping JUMP.NE R14 @ascii_comment ; Otherwise keep looping
JUMP @ascii_other JUMP @ascii_other
finish :finish
LOADUI R0 0x1100 ; Close TAPE_01 LOADUI R0 0x1100 ; Close TAPE_01
FCLOSE FCLOSE
LOADUI R0 0x1101 ; Close TAPE_02 LOADUI R0 0x1101 ; Close TAPE_02

View File

@ -1,7 +1,9 @@
start :start
LOADUI R10 0x0F ; Byte mask LOADUI R10 0x0F ; Byte mask
LOADUI R11 1 ; Our toggle LOADUI R11 1 ; Our toggle
LOADUI R13 0x600 ; Where we are starting our Stack LOADUI R13 0x600 ; Where we are starting our Stack
;; R14 is storing our condition code
;; R15 is storing our nybble
;; Prep TAPE_01 ;; Prep TAPE_01
LOADUI R0 0x1100 LOADUI R0 0x1100
@ -11,7 +13,7 @@ start
LOADUI R0 0x1101 LOADUI R0 0x1101
FOPEN_WRITE FOPEN_WRITE
loop :loop
LOADUI R1 0x1100 ; Read from tape_01 LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char FGETC ; Read a Char
@ -20,7 +22,7 @@ loop
JUMP.GE R14 @.L1 JUMP.GE R14 @.L1
CALLI R13 @finish CALLI R13 @finish
.L1 :.L1
LOADUI R1 0 ; Write to Char to TTY LOADUI R1 0 ; Write to Char to TTY
FPUTC ; Print the Char FPUTC ; Print the Char
CALLI R13 @hex ; Convert it CALLI R13 @hex ; Convert it
@ -33,7 +35,7 @@ loop
FALSE R11 ; Flip the toggle FALSE R11 ; Flip the toggle
JUMP @loop JUMP @loop
.L99 :.L99
SL0I R15 4 ; Shift our first nibble SL0I R15 4 ; Shift our first nibble
AND R0 R0 R10 ; Mask out top AND R0 R0 R10 ; Mask out top
ADD R0 R0 R15 ; Combine nibbles ADD R0 R0 R15 ; Combine nibbles
@ -42,7 +44,7 @@ loop
FPUTC ; To TAPE_02 FPUTC ; To TAPE_02
JUMP @loop ; Try to get more bytes JUMP @loop ; Try to get more bytes
hex :hex
;; Deal with line comments starting with # ;; Deal with line comments starting with #
CMPUI R14 R0 35 CMPUI R14 R0 35
JUMP.E R14 @ascii_comment JUMP.E R14 @ascii_comment
@ -70,19 +72,19 @@ hex
;; Ignore the rest ;; Ignore the rest
JUMP @ascii_other JUMP @ascii_other
ascii_num :ascii_num
SUBUI R0 R0 48 SUBUI R0 R0 48
RET R13 RET R13
ascii_low :ascii_low
SUBUI R0 R0 87 SUBUI R0 R0 87
RET R13 RET R13
ascii_high :ascii_high
SUBUI R0 R0 55 SUBUI R0 R0 55
RET R13 RET R13
ascii_other :ascii_other
TRUE R0 TRUE R0
RET R13 RET R13
ascii_comment :ascii_comment
LOADUI R1 0x1100 ; Read from TAPE_01 LOADUI R1 0x1100 ; Read from TAPE_01
FGETC ; Read another char FGETC ; Read another char
CMPUI R14 R0 10 ; Stop at the end of line CMPUI R14 R0 10 ; Stop at the end of line
@ -91,7 +93,7 @@ ascii_comment
JUMP.NE R14 @ascii_comment ; Otherwise keep looping JUMP.NE R14 @ascii_comment ; Otherwise keep looping
JUMP @ascii_other JUMP @ascii_other
finish :finish
LOADUI R0 0x1100 ; Close TAPE_01 LOADUI R0 0x1100 ; Close TAPE_01
FCLOSE FCLOSE
LOADUI R0 0x1101 ; Close TAPE_02 LOADUI R0 0x1101 ; Close TAPE_02

View File

@ -1,4 +1,4 @@
start :start
LOADUI R8 @table ; Where we are putting our address pointers LOADUI R8 @table ; Where we are putting our address pointers
LOADUI R9 0xFF ; Byte mask LOADUI R9 0xFF ; Byte mask
LOADUI R10 0x0F ; nybble mask LOADUI R10 0x0F ; nybble mask
@ -17,7 +17,7 @@ start
FOPEN_WRITE FOPEN_WRITE
;; Function for collecting the address of all labels ;; Function for collecting the address of all labels
getLables :getLables
LOADUI R1 0x1100 ; Read from tape_01 LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char FGETC ; Read a Char
@ -40,7 +40,7 @@ getLables
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 JUMP @getLables
.L0 :.L0
;; Otherwise attempt to process ;; Otherwise attempt to process
CALLI R13 @hex ; Convert it CALLI R13 @hex ; Convert it
CMPSKIP.GE R0 0 ; Don't record, nonhex values CMPSKIP.GE R0 0 ; Don't record, nonhex values
@ -53,14 +53,14 @@ getLables
FALSE R11 ; Flip the toggle FALSE R11 ; Flip the toggle
JUMP @getLables JUMP @getLables
.L1 :.L1
;; Deal with case of second half of byte ;; Deal with case of second half of byte
TRUE R11 ; Flip the toggle TRUE R11 ; Flip the toggle
ADDUI R12 R12 1 ; increment PC now that we have a full byte ADDUI R12 R12 1 ; increment PC now that we have a full byte
JUMP @getLables JUMP @getLables
;; Function for storing the address of the label ;; Function for storing the address of the label
storeLabel :storeLabel
;; Get the char of the Label ;; Get the char of the Label
LOADUI R1 0x1100 ; Read from tape_01 LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char FGETC ; Read a Char
@ -75,7 +75,7 @@ storeLabel
RET R13 RET R13
;; Main Functionality ;; Main Functionality
stage2 :stage2
;; We first need to rewind tape_01 to perform our second pass ;; We first need to rewind tape_01 to perform our second pass
LOADUI R0 0x1100 LOADUI R0 0x1100
REWIND REWIND
@ -84,7 +84,7 @@ stage2
LOADUI R11 1 ; Our toggle LOADUI R11 1 ; Our toggle
FALSE R12 ; Our PC counter FALSE R12 ; Our PC counter
loop :loop
LOADUI R1 0x1100 ; Read from tape_01 LOADUI R1 0x1100 ; Read from tape_01
FGETC ; Read a Char FGETC ; Read a Char
@ -101,14 +101,14 @@ loop
FGETC ; Read a Char FGETC ; Read a Char
JUMP @loop JUMP @loop
.L97 :.L97
;; Check for Pointer ;; Check for Pointer
CMPUI R14 R0 64 ; If it is a pointer Deal with it CMPUI R14 R0 64 ; If it is a pointer Deal with it
JUMP.NE R14 @.L98 ; Otherwise attempt to process it JUMP.NE R14 @.L98 ; Otherwise attempt to process it
CALLI R13 @storePointer CALLI R13 @storePointer
JUMP @loop JUMP @loop
.L98 :.L98
;; Process Char ;; Process Char
LOADUI R1 0 ; Write to Char to TTY LOADUI R1 0 ; Write to Char to TTY
FPUTC ; Print the Char FPUTC ; Print the Char
@ -122,7 +122,7 @@ loop
FALSE R11 ; Flip the toggle FALSE R11 ; Flip the toggle
JUMP @loop JUMP @loop
.L99 :.L99
SL0I R15 4 ; Shift our first nibble SL0I R15 4 ; Shift our first nibble
AND R0 R0 R10 ; Mask out top AND R0 R0 R10 ; Mask out top
ADD R0 R0 R15 ; Combine nibbles ADD R0 R0 R15 ; Combine nibbles
@ -132,7 +132,7 @@ loop
ADDUI R12 R12 1 ; increment PC now that we have a full byte ADDUI R12 R12 1 ; increment PC now that we have a full byte
JUMP @loop ; Try to get more bytes JUMP @loop ; Try to get more bytes
storePointer :storePointer
;; Correct the PC to reflect the size of the pointer ;; Correct the PC to reflect the size of the pointer
ADDUI R12 R12 2 ; Exactly 2 bytes ADDUI R12 R12 2 ; Exactly 2 bytes
@ -160,7 +160,7 @@ storePointer
FPUTC ; Write the byte to TAPE_02 FPUTC ; Write the byte to TAPE_02
RET R13 RET R13
hex :hex
;; Deal with line comments starting with # ;; Deal with line comments starting with #
CMPUI R14 R0 35 CMPUI R14 R0 35
JUMP.E R14 @ascii_comment JUMP.E R14 @ascii_comment
@ -188,19 +188,19 @@ hex
;; Ignore the rest ;; Ignore the rest
JUMP @ascii_other JUMP @ascii_other
ascii_num :ascii_num
SUBUI R0 R0 48 SUBUI R0 R0 48
RET R13 RET R13
ascii_low :ascii_low
SUBUI R0 R0 87 SUBUI R0 R0 87
RET R13 RET R13
ascii_high :ascii_high
SUBUI R0 R0 55 SUBUI R0 R0 55
RET R13 RET R13
ascii_other :ascii_other
TRUE R0 TRUE R0
RET R13 RET R13
ascii_comment :ascii_comment
LOADUI R1 0x1100 ; Read from TAPE_01 LOADUI R1 0x1100 ; Read from TAPE_01
FGETC ; Read another char FGETC ; Read another char
CMPUI R14 R0 10 ; Stop at the end of line CMPUI R14 R0 10 ; Stop at the end of line
@ -209,7 +209,7 @@ ascii_comment
JUMP.NE R14 @ascii_comment ; Otherwise keep looping JUMP.NE R14 @ascii_comment ; Otherwise keep looping
JUMP @ascii_other JUMP @ascii_other
finish :finish
LOADUI R0 0x1100 ; Close TAPE_01 LOADUI R0 0x1100 ; Close TAPE_01
FCLOSE FCLOSE
LOADUI R0 0x1101 ; Close TAPE_02 LOADUI R0 0x1101 ; Close TAPE_02
@ -217,4 +217,4 @@ finish
HALT HALT
;; Where all of our pointers will be stored for our locations ;; Where all of our pointers will be stored for our locations
table :table