Skip to content

Commit 388cabd

Browse files
committed
Fixed crash
1 parent 65045a3 commit 388cabd

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

src/as4.c

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,16 @@ int main(int argc, char **argv)
172172
/* Address is optional - if it isn't there, just put zeros. */
173173
addinst(outbuf, HLT, NOADDR, &bits, &bytes);
174174
}
175-
/* Get the address location. */
176-
/* If it's just a number after the instruction, that will be returned with the base address added to it. */
177-
/* If it's a yet undeclared label, 65535 (UNKNOWNADDR) is returned. The instruction will be modified when the label is declared. */
178-
/* If it's an already declared label return the address relative to the base address. */
179-
address = findlabel(&unknownlabels, &labels, trimed, numlabels, &numunknownlabels, bits, INST);
180-
/* Add this to the output buffer. */
181-
addinst(outbuf, HLT, address, &bits, &bytes);
175+
else
176+
{
177+
/* Get the address location. */
178+
/* If it's just a number after the instruction, that will be returned with the base address added to it. */
179+
/* If it's a yet undeclared label, 65535 (UNKNOWNADDR) is returned. The instruction will be modified when the label is declared. */
180+
/* If it's an already declared label return the address relative to the base address. */
181+
address = findlabel(&unknownlabels, &labels, trimed, numlabels, &numunknownlabels, bits, INST);
182+
/* Add this to the output buffer. */
183+
addinst(outbuf, HLT, address, &bits, &bytes);
184+
}
182185

183186
}
184187
/* Load instruction */
@@ -258,13 +261,16 @@ int main(int argc, char **argv)
258261
/* Address is optional - if it isn't there, just put zeros. */
259262
addinst(outbuf, NOP, NOADDR, &bits, &bytes);
260263
}
261-
/* Get the address location. */
262-
/* If it's just a number after the instruction, that will be returned with the base address added to it. */
263-
/* If it's a yet undeclared label, 65535 (UNKNOWNADDR) is returned. The instruction will be modified when the label is declared. */
264-
/* If it's an already declared label return the address relative to the base address. */
265-
address = findlabel(&unknownlabels, &labels, trimed, numlabels, &numunknownlabels, bits, INST);
266-
/* Add this to the output buffer. */
267-
addinst(outbuf, NOP, address, &bits, &bytes);
264+
else
265+
{
266+
/* Get the address location. */
267+
/* If it's just a number after the instruction, that will be returned with the base address added to it. */
268+
/* If it's a yet undeclared label, 65535 (UNKNOWNADDR) is returned. The instruction will be modified when the label is declared. */
269+
/* If it's an already declared label return the address relative to the base address. */
270+
address = findlabel(&unknownlabels, &labels, trimed, numlabels, &numunknownlabels, bits, INST);
271+
/* Add this to the output buffer. */
272+
addinst(outbuf, NOP, address, &bits, &bytes);
273+
}
268274

269275
}
270276
/* Start of the information section. Save as NOP with address 0xFFFF so the processor isn't bothered but we can tell later. */
@@ -314,7 +320,7 @@ int main(int argc, char **argv)
314320
label *templabels = NULL;
315321
unsigned long long templabelnum = 0;
316322

317-
addlabel(outbuf, &templabels, &unknownlabels, &templabelnum, &numunknownlabels, labels[i].str, (labels[i].addr * 4), baseaddr);
323+
addlabel(outbuf, &templabels, &unknownlabels, &templabelnum, &numunknownlabels, labels[i].str, (labels[i].addr * 4UL), baseaddr);
318324
labels[i].addr = templabels[0].addr;
319325
}
320326
}
@@ -497,13 +503,16 @@ int main(int argc, char **argv)
497503
/* Address is optional - if it isn't there, just put zeros. */
498504
addinst(outbuf, CXA, NOADDR, &bits, &bytes);
499505
}
500-
/* Get the address location. */
501-
/* If it's just a number after the instruction, that will be returned with the base address added to it. */
502-
/* If it's a yet undeclared label, 65535 (UNKNOWNADDR) is returned. The instruction will be modified when the label is declared. */
503-
/* If it's an already declared label return the address relative to the base address. */
504-
address = findlabel(&unknownlabels, &labels, trimed, numlabels, &numunknownlabels, bits, INST);
505-
/* Add this to the output buffer. */
506-
addinst(outbuf, CXA, address, &bits, &bytes);
506+
else
507+
{
508+
/* Get the address location. */
509+
/* If it's just a number after the instruction, that will be returned with the base address added to it. */
510+
/* If it's a yet undeclared label, 65535 (UNKNOWNADDR) is returned. The instruction will be modified when the label is declared. */
511+
/* If it's an already declared label return the address relative to the base address. */
512+
address = findlabel(&unknownlabels, &labels, trimed, numlabels, &numunknownlabels, bits, INST);
513+
/* Add this to the output buffer. */
514+
addinst(outbuf, CXA, address, &bits, &bytes);
515+
}
507516
}
508517
/* .data arbitrary data section inputs */
509518
else if(!strncmp(trimed, ".data", 6))

src/label.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ void addlabel(char *outbuf, label **labels, label **unknownlabels, unsigned long
132132
char *nstr = calloc(1, 6);
133133
/* As this is getting a portion of the address of the label accessed, we get the label's address and bitwise and that section then shift it back. */
134134
sprintf(nstr, "N_[%X]", (labeladdr & (0xF << ((*unknownlabels)[i].addroffset * 4))) >> ((*unknownlabels)[i].addroffset * 4));
135-
labeladdr = findlabel(unknownlabels, labels, nstr, (*numlabels), numunknownlabels, instaddress * 4, ((*unknownlabels)[i].type) & 1);
135+
labeladdr = findlabel(unknownlabels, labels, nstr, (*numlabels), numunknownlabels, instaddress * 4UL, ((*unknownlabels)[i].type) & 1);
136136
continue;
137137
}
138138
else
139139
{
140-
labeladdr = (labeladdr & (0xF << ((*unknownlabels)[i].addroffset * 4))) >> ((*unknownlabels)[i].addroffset * 4) + N_START;
140+
labeladdr = ((labeladdr & (0xF << ((*unknownlabels)[i].addroffset * 4))) >> ((*unknownlabels)[i].addroffset * 4)) + N_START;
141141
}
142142
}
143143
/* The address it was referenced at is actually the opcode of the instruction, so go up one nibble to point to the address section. */
@@ -199,6 +199,11 @@ unsigned short int findlabel(label **unknownlabels, label **labels, char *labels
199199
fprintf(stderr, "findlabel: Type can only be 0 or 1\n");
200200
exit(33);
201201
}
202+
if(labelstr == NULL)
203+
{
204+
fprintf(stderr, "findlabel: labelstr cannot be null!\n");
205+
exit(34);
206+
}
202207
/* Set errno to 0 so we can check for errors from strtol, which will tell us if the token after the assembly instruction is a number or a label (if it's neither we find out a bit later, not in this function). */
203208
errno = 0;
204209
/* tempaddress is 0 if the labelstr is actually a label and not a value. */
@@ -259,9 +264,9 @@ unsigned short int findlabel(label **unknownlabels, label **labels, char *labels
259264
if(endptr[0] == ']' && endptr[1] == '\0')
260265
{
261266
/* However, the offset must be between 0 and 3 as addresses are 4 nibbles. */
262-
if(addroffset > 3 || addroffset < 0)
267+
if(addroffset > 3)
263268
{
264-
fprintf(stderr, "Line %llu: The offset of an address of operation must be between 0 and 4.\n", FILELINE);
269+
fprintf(stderr, "Line %llu: The offset of an address of operation must be between 0 and 3.\n", FILELINE);
265270
exit(51);
266271
}
267272
formatcorrect = 1;
@@ -287,7 +292,7 @@ unsigned short int findlabel(label **unknownlabels, label **labels, char *labels
287292
}
288293
}
289294
/* If the string has not passed all the trials, punish the user. */
290-
if(formatcorrect = 0)
295+
if(formatcorrect == 0)
291296
{
292297
fprintf(stderr, "Line %llu: Format of an address of operation must be &(LABEL[OFFSET])[ADDRESS_OFFSET].\n", FILELINE);
293298
exit(52);

0 commit comments

Comments
 (0)