Skip to content

Commit f344179

Browse files
committed
Added lexing, still working on byte splitting
1 parent c0340e9 commit f344179

8 files changed

Lines changed: 223 additions & 8 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SRC=as4.c
22
EXE=as4
3-
EXTRACFLAGS=-lm
4-
EXTRALDFLAGS=-lm
3+
EXTRACFLAGS=
4+
EXTRALDFLAGS=
55
export SRC
66
export EXE
77
export EXTRACFLAGS

Makefile.fast.gcc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CC=gcc
2-
FASTFLAGS=-O3 -s -march=native -fomit-frame-pointer -pipe -Wdisabled-optimization
2+
FASTFLAGS=-Os -s -static -march=native -fomit-frame-pointer -pipe -Wdisabled-optimization
33
CFLAGS=$(FASTFLAGS) $(EXTRACFLAGS) -c
44
LDFLAGS=$(FASTFLAGS) $(EXTRALDFLAGS)
55
OBJ=$(SRC:.c=.o)

TestCodeNibbleV2.asm

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
; This program reads in integers and adds them together
2+
; until a negative number is read in. Then it outputs
3+
; the sum (not including the last number).
4+
5+
;Start: read ; read n -> acc
6+
; jmpn Done ; jump to Done if n < 0.
7+
; add sum ; add sum to the acc
8+
; store sum ; store the new sum
9+
; jump Start ; go back & read in next number
10+
;Done: load sum ; load the final sum
11+
; write ; write the final sum
12+
; stop ; stop
13+
;
14+
;sum: .data 2 0 ; 2-byte location where sum is stored
15+
16+
;; START NIbble-Knowledge code ;;
17+
18+
;; Instruction Section ;;
19+
Start:
20+
LOD n15
21+
ADD n2
22+
NND n15
23+
NOP
24+
CXA
25+
JMP Jump
26+
STR sum
27+
28+
Jump: HLT
29+
30+
31+
;; Data Section ;;
32+
n0: .data 1 0 ; b0000
33+
n1: .data 1 1 ; b0001
34+
n2: .data 1 2 ; b0010
35+
n3: .data 1 3 ; b0011
36+
n4: .data 1 4 ; b0100
37+
n5: .data 1 5 ; b0101
38+
n6: .data 1 6 ; b0110
39+
n7: .data 1 7 ; b0111
40+
n8: .data 1 8 ; b1000
41+
n9: .data 1 9 ; b1001
42+
n10: .data 1 10 ; b1010
43+
n11: .data 1 11 ; b1011
44+
n12: .data 1 12 ; b1100
45+
n13: .data 1 13 ; b1101
46+
n14: .data 1 14 ; b1110
47+
n15: .data 1 15 ; b1111
48+
49+
sum: .data 1 0
50+
51+

as4.c

Lines changed: 137 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,162 @@ int main(int argc, char **argv)
1414
char *line = NULL;
1515
ssize_t linelen = -1;
1616
size_t len = 0;
17+
size_t outputsize = 0;
18+
const char *delims = " \t";
19+
char *outbuf = NULL;
20+
struct stat st;
21+
size_t bufsize = 0;
22+
unsigned int i = 0;
23+
unsigned long long bits = 0;
24+
unsigned long long bytes = 0;
1725

18-
input = fopen(argv[1], "r");
26+
input = fopen(argv[1], "rb");
1927
if(input == NULL)
2028
{
2129
perror("Could not open input file");
2230
return 1;
2331
}
2432

25-
output = fopen(argv[2], "w");
33+
output = fopen(argv[2], "wb");
2634
if(output == NULL)
2735
{
2836
perror("Could not open output file");
2937
return 2;
3038
}
3139

40+
stat(argv[1], &st);
41+
bufsize = (size_t)st.st_size * 2;
42+
outbuf = calloc(bufsize, 1);
43+
if(outbuf == NULL)
44+
{
45+
perror("Could not allocate output buffer");
46+
}
3247
linelen = getline(&line, &len, input);
3348
while(linelen > -1)
3449
{
35-
puts(line);
50+
char *tokens;
51+
52+
if(strlen(line) >= 2)
53+
{
54+
tokens = strtok(line, delims);
55+
if(tokens != NULL && tokens[0] != ';' && tokens[0] != '\n' && tokens[0] != '\r')
56+
{
57+
58+
while(tokens != NULL)
59+
{
60+
if(tokens[0] == ';')
61+
{
62+
break;
63+
}
64+
else if(tokens[0] != '\n' && tokens[0] != '\r')
65+
{
66+
size_t tokenlen = strlen(tokens);
67+
int doneline = 0;
68+
if(tokens[tokenlen - 1] == '\n')
69+
{
70+
tokens[tokenlen - 1] = '\0';
71+
}
72+
73+
if(!strncmp(tokens, "HLT", 3))
74+
{
75+
printf("0000 0000 0000 0000 0000");
76+
doneline = 1;
77+
}
78+
else if(!strncmp(tokens, "LOD", 3))
79+
{
80+
printf("0001|");
81+
}
82+
else if(!strncmp(tokens, "STR", 3))
83+
{
84+
printf("0010|");
85+
}
86+
else if(!strncmp(tokens, "ADD", 3))
87+
{
88+
printf("0011|");
89+
}
90+
else if(!strncmp(tokens, "NOP", 3))
91+
{
92+
printf("0100 0000 0000 0000 0000");
93+
doneline = 1;
94+
}
95+
else if(!strncmp(tokens, "NND", 3))
96+
{
97+
printf("0101|");
98+
}
99+
else if(!strncmp(tokens, "JMP", 3))
100+
{
101+
printf("0110|");
102+
}
103+
else if(!strncmp(tokens, "CXA", 3))
104+
{
105+
printf("0111 0000 0000 0000 0000");
106+
doneline = 1;
107+
if(bits % 8 != 0)
108+
{
109+
outbuf[bytes] |= 0x70;
110+
bytes++;
111+
bits += 4;
112+
outbuf[bytes] = 0;
113+
bytes++;
114+
bits += 8;
115+
outbuf[bytes] = 0;
116+
bytes++;
117+
bits += 8;
118+
}
119+
else
120+
{
121+
outbuf[bytes] = 7;
122+
bytes++;
123+
bits += 8;
124+
outbuf[bytes] = 0;
125+
bytes++;
126+
bits += 8;
127+
outbuf[bytes] &= 0xF0;
128+
bits += 4;
129+
}
130+
}
131+
else
132+
{
133+
printf("%s|", tokens);
134+
}
135+
136+
if(doneline)
137+
{
138+
break;
139+
}
140+
}
141+
tokens = strtok(NULL, delims);
142+
}
143+
puts("");
144+
}
145+
}
36146
linelen = getline(&line, &len, input);
37147
}
38-
148+
for(i = 0; i < bufsize; i++)
149+
{
150+
char temp = outbuf[i];
151+
char j;
152+
for(j = 0; j < 8; j++)
153+
{
154+
if(temp & 1)
155+
{
156+
printf("1");
157+
}
158+
else
159+
{
160+
printf("0");
161+
}
162+
if((j+1)%4 == 0)
163+
{
164+
printf(" ");
165+
}
166+
temp >>= 1;
167+
}
168+
}
169+
printf("\n");
170+
free(outbuf);
39171
}
40-
172+
return 0;
41173
}
42174

43175
void help(void)

as4.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#define _GNU_SOURCE
22
#include <stdio.h>
33
#include <stdlib.h>
4+
#include <string.h>
5+
#include <sys/stat.h>
46

57
void help(void);

out

Whitespace-only changes.

sumshit.asm

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
LOD addressY ; Put Y into A
2+
NND n15 ; Negate Y
3+
ADD n1 ; Adds 1 to Negated Y (Finished Two’s Compliment)
4+
ADD addressX ; Add X to Negated Y (X-Y)
5+
CXA ; Copies Carry and XORb to A
6+
NND n8
7+
NND n15 ; ANDs with 1000 to remove carry bit and leave XORb
8+
JMP TAG ; Jumps to tag if X >= Y else continue
9+
LOD addressY ; Put Y into A
10+
NND n15 ; Negate Y
11+
ADD n1 ; Add 1 to Negated Y (Finished Two’s Compliment)
12+
ADD addressX ; Add X to Negated Y (X-Y)
13+
NND addressY ; NAND with Y
14+
NND n15 ; Negate A = Z
15+
NND n15 ; X = X’
16+
STR addressX’ ; Store X’ in Mem
17+
LOD addressY ; Load Y
18+
NND n15 ; Y = Y’
19+
NND addressX’ ; Y’ NAND X’ = Z
20+
STR addressX ; Store X into Mem
21+
NND addressY ; X NAND Y = Inter1
22+
STR addressInter1 ; Store Intermediate 1 in Mem
23+
NND addressX ; Inter1 NAND X = Inter2
24+
STR addressInter2 ; Store Intermediate 2 in Mem
25+
LOD addressInter1 ; Load Intermediate 1 into Mem
26+
NND addressY ; Inter1 NAND Y =Inter3
27+
NND addressInter2 ; Inter3 NAND Inter2 = Z

test.asm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXA
2+
CXA
3+
CXA

0 commit comments

Comments
 (0)