I am getting a segmentation fault in a function and am not sure if I have correctly pinpointed the location. And if I have I don't have a clue of how to resolve the issue.
Here is the gdb report I get:
Code:
Program received signal SIGSEGV, Segmentation fault.
0x00000000004023c7 in breakup (
sourceline=0x504c60 ".CHECK getsource removal of initial blank lines",
components=
{lbl = 0x7fbffff9d0 "\001", opc = 0x402399 "\213EôÉÃ\220\220UH\211åH\201ìp\002", opr1 = 0x5a <Address 0x5a out of bounds>, opr2 = 0x509b10 "", ni = 0xa0d0c0b0920, xbpe = 0x1a00000000}) at breakup.c:19
19 int xbpe = *components.xbpe;
(gdb) backtrace
#0 0x00000000004023c7 in breakup (
sourceline=0x504c60 ".CHECK getsource removal of initial blank lines",
components=
{lbl = 0x7fbffff9d0 "\001", opc = 0x402399 "\213EôÉÃ\220\220UH\211åH\201ìp\002", opr1 = 0x5a <Address 0x5a out of bounds>, opr2 = 0x509b10 "", ni = 0xa0d0c0b0920, xbpe = 0x1a00000000}) at breakup.c:19
#1 0x0000000000401e05 in pass1 () at pass1.c:47
#2 0x0000000000400e81 in main ()
From what I can tell the problem seems to exist in the following code:
Code:
int xbpe = *components.xbpe;
Which is part of the following function:
Code:
/* Pre-Processor Directives */
#include <string.h>
struct toklist
{
char *lbl;
char *opc;
char *opr1;
char *opr2;
int *ni;
int *xbpe;
} ;
void breakup(char sourceline[], struct toklist components)
{
char workingstring[81], temp[81], *ptr;
char label[81], opcode[81], operand1[81], operand2[81];
int ni = 3, bp = 0;
int xbpe = *components.xbpe;
/* Remove x and e bits */
if(xbpe > 7)
xbpe -= 8;
if(xbpe > 3)
{
bp += 4;
xbpe -= 4;
}
if(xbpe > 1)
{
bp += 2;
xbpe -= 2;
}
xbpe = 0;
strcpy(workingstring, sourceline);
strtok(workingstring, "\n");
label[0] = '\0';
opcode[0] = '\0';
operand1[0] = '\0';
operand2[0] = '\0';
/* Check for a label */
if(workingstring[0] == ' ' || workingstring[0] == '\t')
{/* No label, tokenize opcode */
if((ptr = strtok(workingstring, " \t")) != NULL)
{
if(ptr[0] == '*')
{
ptr = ptr + 1;
ni = 0;
}
if(ptr[0] == '+')
{
ptr = ptr + 1;
xbpe += 1;
}
strcpy(opcode, ptr);
}
}
else
{/* Tokenize label and opcode */
if((ptr = strtok(workingstring, " \t")) != NULL)
strcpy(label, ptr);
if((ptr = strtok(NULL, " \t")) != NULL)
{
if(ptr[0] == '*')
{
ptr = ptr + 1;
ni = 0;
}
if(ptr[0] == '+')
{
ptr = ptr + 1;
xbpe += 1;
}
strcpy(opcode, ptr);
}
}
/* Remove White Space */
if((ptr = strtok(NULL, "")) != NULL)
{
while(ptr[0] == ' ' || ptr[0] == '\t')
ptr = ptr + 1;
strcpy(workingstring, ptr);
}
/* Set n bit for indirect addressing */
if(workingstring[0] == '@')
{
strcpy(workingstring, workingstring + 1);
ni = 2;
}
/* Set i bit for immediate addressing */
if(workingstring[0] == '#')
{
strcpy(workingstring, workingstring + 1);
ni = 1;
}
/* Check for single quote */
if(workingstring[1] == '\'' || workingstring[2] == '\'')
{
ptr = strtok(workingstring, "\'");
strcpy(temp, ptr);
strcat(temp, "\'");
ptr = strtok(NULL, "\'");
strcat(temp, ptr);
strcat(temp, "\'");
strcpy(operand1, temp);
}
else
/* Tokenize operand1 */
if((ptr = strtok(workingstring, ", \t")) != NULL)
strcpy(operand1, ptr);
/* Tokenize operand2 */
if((ptr = strtok(NULL, ", \t")) != NULL)
strcpy(operand2, ptr);
/* If there is an operand2 set x bit */
if(strncmp(operand2, "\0", 1) != 0)
xbpe += 8;
/* Set values in the components */
strcpy(components.lbl, label);
strcpy(components.opc, opcode);
strcpy(components.opr1, operand1);
strcpy(components.opr2, operand2);
*components.ni = ni;
*components.xbpe = xbpe + bp;
}