Skip to content

Commit 57af670

Browse files
U-UC\harvey1U-UC\harvey1
authored andcommitted
Added N_START
1 parent 9a5b6fa commit 57af670

3 files changed

Lines changed: 46 additions & 5 deletions

File tree

src/as4.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* This is the main file */
44

55
unsigned long long FILELINE = 0;
6+
uint16_t N_START = 0xFFFF;
67

78
/* Let's get ready to party */
89
int main(int argc, char **argv)
@@ -306,6 +307,22 @@ int main(int argc, char **argv)
306307
addinst(outbuf, NOP, address, &bits, &bytes);
307308

308309
}
310+
/* Find where the N_ section starts */
311+
else if(!strncmp(tokens, "NSTART", 7))
312+
{
313+
tokens = strtok(NULL, delims);
314+
/* Check firstly that there is a valid label or address after the instruction. */
315+
if(tokens == NULL || tokens[0] == '\n' || tokens[0] == '\r' || tokens[0] == '\0' || tokens[0] == ' ')
316+
{
317+
fprintf(stderr, "Line %llu: A memory address must succeed a DSEC instruction.\n", FILELINE);
318+
exit(34);
319+
}
320+
/* Get the address location. */
321+
/* If it's just a number after the instruction, that will be returned with the base address added to it. */
322+
/* If it's a yet undeclared label, 65535 (UNKNOWNADDR) is returned. The instruction will be modified when the label is declared. */
323+
/* If it's an already declared label return the address relative to the base address. */
324+
N_START = findlabel(&unknownlabels, &labels, tokens, numlabels, &numunknownlabels, bits, INST);
325+
}
309326
/* Each group of same size data sections should be recorded with the pair DNUM DSIZE */
310327
else if(!strncmp(tokens, "DNUM", 5))
311328
{
@@ -357,9 +374,22 @@ int main(int argc, char **argv)
357374
doneline = 1;
358375
/* Just in case someone forgot EPINF... */
359376
inpinf = 0;
360-
/* Add it to the buffer. */
377+
/* Prevent the address of EINF from getting written over... */
378+
if(unknownlabels != NULL)
379+
{
380+
for(i = 0; i < numunknownlabels; i++)
381+
{
382+
if((unknownlabels[i].str != NULL) && (!strcmp(unknownlabels[i].str, "N_")) && (unknownlabels[i].addr == (bits/4)))
383+
{
384+
char *emptystr = calloc(1, 2);
385+
memcpy(emptystr, "", 2);
386+
unknownlabels[i].str = emptystr;
387+
unknownlabels[i].addr = 0xFFFF;
388+
}
389+
}
390+
}
391+
/* Add to output buffer. */
361392
addinst(outbuf, NOP, 0xFFFF, &bits, &bytes);
362-
363393
}
364394
/* Nand instruction. */
365395
else if(!strncmp(tokens, "NND", 4))
@@ -593,7 +623,8 @@ int main(int argc, char **argv)
593623
if(labels[j].str != NULL)
594624
{
595625
/* If we find it, set found to true. */
596-
if(!strcmp(labels[j].str, unknownlabels[i].str))
626+
/* if both str is "" and addr is 0xFFFF, this was an unknown label we were meant to ignore. */
627+
if(!strcmp(labels[j].str, unknownlabels[i].str) || (!strcmp("", unknownlabels[i].str) && (unknownlabels[i].addr == 0xFFFF)))
597628
{
598629
foundunknown = 1;
599630
}

src/as4.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@
4040
/* When we are using labels references labels, we need account for the 4 nibble size instead of 5 for an instruction */
4141
#define INST 0
4242
#define LABEL 1
43+
#define ADDROF 2
4344

44-
/* The singular global variable - what line of the assembly file we are on. Helps with error messages */
45+
/* What line of the assembly file we are on. Helps with error messages */
4546
extern unsigned long long FILELINE;
47+
/* Where the N_ section starts. Used for address-of operations. */
48+
extern uint16_t N_START;
49+
4650

4751
/* This is the data structure used to identify labels */
4852
typedef struct _label
@@ -51,6 +55,7 @@ typedef struct _label
5155
char *str;
5256
uint16_t offset;
5357
uint8_t type;
58+
uint16_t addroffset;
5459
} label;
5560

5661
/* Help() prints the help. More useful in large programs */

src/label.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ void addlabel(char *outbuf, label **labels, label **unknownlabels, unsigned long
7878
/* Assign the current location of the output buffer to as the address, because then the label will point to the next instruction or data element added, which is what we want. */
7979
/* Also add the base address, which is important. */
8080
(*labels)[(*numlabels) - 1].addr = ((bits/4) + baseaddr);
81+
/* If the label is for N_, then don't add anything to the output buffer but set N_START. */
82+
if(!(strcmp(tempstr, "N_")))
83+
{
84+
N_START = ((bits/4) + baseaddr);
85+
}
8186
/* Now we've saved the declared label, we can check for it being used before it was declared. */
8287
/* But if there is no output buffer, we can't do much. */
8388
if(outbuf == NULL)
@@ -94,7 +99,7 @@ void addlabel(char *outbuf, label **labels, label **unknownlabels, unsigned long
9499
for(i = 0; i < numunknownlabels; i++)
95100
{
96101
/* If the unknown label name exits (otherwise it would be hard to identify it)... */
97-
if((*unknownlabels)[i].str != NULL)
102+
if((*unknownlabels)[i].str != NULL && strcmp((*unknownlabels)[i].str, ""))
98103
{
99104
/* Check if the name of the unknown label is the same as the label that we just had declared. */
100105
if(!strcmp((*unknownlabels)[i].str, tempstr))

0 commit comments

Comments
 (0)