📄 common.c
字号:
icomment_list = malloc(sizeof(struct comment));
if (icomment_list == NULL)
{
printf("\nNo memory for comment struct");
exit(MEM_ERROR);
}
cmt_ptr = icomment_list;
}
else // add comment to list
{
cmt_ptr = icomment_list;
while (cmt_ptr->next) // find end of list
cmt_ptr = cmt_ptr->next;
cmt_ptr->next = malloc(sizeof(struct comment));
if (cmt_ptr->next == NULL)
{
printf("\nNo memory for comment struct");
exit(MEM_ERROR);
}
cmt_ptr = cmt_ptr->next;
}
cmt_ptr->adrs = adrs;
ctext = malloc(strlen(str) + 3);
if (ctext == NULL)
{
printf("\nNo memory for comment string");
exit(MEM_ERROR);
}
cmt_ptr->str = ctext;
strcpy(ctext, "; ");
strcat(ctext, str);
cmt_ptr->next = NULL;
}
//
// Add patch string to linked list in memory
//
void add_patch(int adrs, char *str)
{
int len;
char *ctext;
COMMENT_PTR patch_ptr;
len = strlen(str) - 1;
if (len >= 0 && (str[len] == '\n' || str[len] == '\r'))
str[len] = '\0'; // get rid of newline
if (!patch_list) // first patch
{
patch_list = malloc(sizeof(struct comment));
if (patch_list == NULL)
{
printf("\nNo memory for patch struct");
exit(MEM_ERROR);
}
patch_ptr = patch_list;
}
else // add patch to list
{
patch_ptr = patch_list;
while (patch_ptr->next) // find end of list
patch_ptr = patch_ptr->next;
patch_ptr->next = malloc(sizeof(struct comment));
if (patch_ptr->next == NULL)
{
printf("\nNo memory for patch struct");
exit(MEM_ERROR);
}
patch_ptr = patch_ptr->next;
}
patch_ptr->adrs = adrs;
ctext = malloc(strlen(str) + 3);
if (ctext == NULL)
{
printf("\nNo memory for patch string");
exit(MEM_ERROR);
}
patch_ptr->str = ctext;
if (len >= 0)
strcpy(ctext, str);
else
strcpy(ctext, ";");
patch_ptr->next = NULL;
}
//
// Output hexadecimal operand
//
void puthex(int j)
{
j &= WORD_MASK;
if (j < 10)
{
if (upperflag)
kcnt += fprintf(fp, "%X", j);
else
kcnt += fprintf(fp, "%x", j);
}
else if (j < 16)
{
if (upperflag)
kcnt += fprintf(fp, "0%XH", j);
else
kcnt += fprintf(fp, "0%xh", j);
}
else if (j < 0xa0)
{
if (upperflag)
kcnt += fprintf(fp, "%XH", j);
else
kcnt += fprintf(fp, "%xh", j);
}
else if (j < 0x100)
{
if (upperflag)
kcnt += fprintf(fp, "0%XH", j);
else
kcnt += fprintf(fp, "0%xh", j);
}
else if (j < 0xa00)
{
if (upperflag)
kcnt += fprintf(fp, "%XH", j);
else
kcnt += fprintf(fp, "%xh", j);
}
else if (j < 0x1000)
{
if (upperflag)
kcnt += fprintf(fp, "0%XH", j);
else
kcnt += fprintf(fp, "0%xh", j);
}
else if (j < 0xa000)
{
if (upperflag)
kcnt += fprintf(fp, "%XH", j);
else
kcnt += fprintf(fp, "%xh", j);
}
else
{
if (upperflag)
kcnt += fprintf(fp, "0%XH", j);
else
kcnt += fprintf(fp, "0%xh", j);
}
}
//
// Convert code to printable ascii
//
int ascii(int i)
{
i = i & 0x7f;
if (i == 0x7f)
return ('.');
else if (i < 0x20)
return ('.');
else
return (i);
}
//
// Check if data is printable ascii other than the string delimiter
//
int is_ascii(byte data)
{
if (data < ' ' || data > 0x7e || data == '\'')
return(0);
return(1);
}
//
// Convert ascii hex to hexadecimal for X option
//
int atox(char *str)
{
char c;
int i;
i = 0;
c = (char) toupper(*str++);
while (c)
{
if (isxdigit((int) c))
{
c = (c > '9') ? c - 0x37 : c & 0xf;
i = (i << 4) | c;
}
else
break;
c = (char) toupper(*str++);
}
return(i);
}
//
// Check for reference to address in middle of
// code and flag split reference if true
//
void splitcheck(int i)
{
if (!(pgmflags[i] & PF_CLREF)) // ignore if not referenced
pgmflags[i] |= PF_SPLIT; // else flag split ref
}
//
// Add label to output line if current location marked as referenced
//
void chk_ref(int i)
{
int cnt;
char *cptr;
if ((pgmflags[i] & (PF_REF | PF_CLREF)) == PF_REF)
{
pgmflags[i] |= PF_LABGEN;
cptr = find_entry(i, label_count, lab_val_index); // see if label exists
if (cptr == NULL) // if not, output hex value
{
if (upperflag)
cnt = fprintf(fp, "\nX%04X:", i);
else
cnt = fprintf(fp, "\nX%04x:", i);
}
else
cnt = fprintf(fp, "\n%s:", cptr); // else output label text
if (cnt > 8)
fprintf(fp, "\n\t");
else
fprintf(fp, "\n\t");
}
else
fprintf(fp, "\n\t");
}
//
// Add label to output line if current location marked as referenced
// For PF_WORD and PF_ADRS data
//
void chk_label(int i)
{
char *cptr;
if ((pgmflags[i] & (PF_REF | PF_CLREF)) == PF_REF)
{
pgmflags[i] |= PF_LABGEN;
cptr = find_entry(i, label_count, lab_val_index); // see if label exists
if (cptr == NULL) // if not, output hex value
{
if (upperflag)
fprintf(fp, "\nX%04X:", i);
else
fprintf(fp, "\nX%04x:", i);
}
else
fprintf(fp, "\n%s:", cptr); // else output label text
fprintf(fp, "\n\t");
}
else
fprintf(fp, "\n\t");
}
//
// Output opcode for current code
//
void doopcode(char *mnem)
{
char c;
c = *mnem++;
if (upperflag)
c = toupper(c);
while (c) // output text from opcode table
{
if (c == ' ') // convert spaces to tabs
{
putc('\t', fp);
kcnt = (kcnt + 8) & 0x78;
}
else
{
putc(c, fp);
kcnt++;
}
c = *mnem++;
if (upperflag)
c = toupper(c);
}
}
//
// Output ascii data accumulated in buffer
//
void dump_ascii(int adrs)
{
int padrs, off, cnt;
char *cptr;
padrs = adrs - asc_cnt; // print address for comment field
adrs = padrs; // address in program array
cnt = off = 0; // cnt = char count, off = buffer offset
while (asc_cnt) // while data in ascii buffer...
{
if (pgmflags[adrs] & PF_REF) // if addresss is referenced...
{
if (cnt)
{
putc('\'', fp); // terminate line
kcnt++;
if (hexflag) // if comment field requested...
{
do // show hex address
{
putc('\t', fp); // but tab out to field first
kcnt = (kcnt + 8) & 0x78;
} while (kcnt < XSTOP);
fprintf(fp, "; %04x", padrs);
}
padrs += cnt; // update print address
cnt = 0; // clear char count for this line
}
pgmflags[adrs] |= PF_LABGEN;
cptr = find_entry(adrs, label_count, lab_val_index);
// see if label exists for this adrs
if (cptr == NULL) // if not, show address in hex
fprintf(fp, "\nX%04x:\t%s\t'", adrs, ascistr);
else // else show label name
fprintf(fp, "\n%s:\t%s\t'", cptr, ascistr);
kcnt = 17;
}
else if (!cnt)
{
fprintf(fp, "\n\t%s\t'", ascistr);
kcnt = 17;
}
putc(string[off], fp); // output data in ascii
kcnt++; // character position in this line
cnt++; // increment char in this line
off++; // increment offset into asci buffer
adrs++; // offset into program memory
if (cnt >= ASCLINE) // if max characters per line...
{
putc('\'', fp); // terminate line
kcnt++;
if (hexflag) // if comment field requested
{
do // show hex address
{
putc('\t', fp);
kcnt = (kcnt + 8) & 0x78;
} while (kcnt < XSTOP);
fprintf(fp, "; %04x", padrs);
}
padrs += cnt; // update print address
cnt = 0;
}
--asc_cnt;
}
putc('\'', fp); // terminate line
kcnt++;
if (hexflag && cnt) // if comment field requested...
{
do // show address
{
putc('\t', fp);
kcnt = (kcnt + 8) & 0x78;
} while (kcnt < XSTOP);
fprintf(fp, "; %04x", padrs);
}
dump = 1;
}
//
// Output binary data accumulated in buffer
//
void dump_bytes(int adrs)
{
int padrs, bcnt, off, k;
char *cptr, chr;
padrs = adrs - byte_cnt; // compute adrs to print in ascii part
adrs = padrs;
bcnt = off = 0; // no bytes output yet
while (byte_cnt) // while data in binary buffer...
{
if (pgmflags[adrs] & PF_REF) // if data adrs is referenced, or header comment...
{
if (off && hexflag) // dump any remaining ascii first
{
do
{
putc('\t', fp);
kcnt = (kcnt + 8) & 0x78;
} while (kcnt < XSTOP);
fprintf(fp, "; %04x ", padrs);
for (k=0; k<off; k++)
putc(ascii(pgmmem[padrs + k]), fp);
padrs += k; // update print address
off = 0;
}
if (pgmflags[adrs] & PF_REF)
{
pgmflags[adrs] |= PF_LABGEN;
cptr = find_entry(adrs, label_count, lab_val_index); // then do a label
if (cptr == NULL)
{
if (upperflag)
fprintf(fp, "\nX%04X:\t%s\t", adrs, defbstr);
else
fprintf(fp, "\nX%04x:\t%s\t", adrs, defbstr);
}
else
fprintf(fp, "\n%s:\t%s\t", cptr, defbstr);
}
kcnt = 16;
bcnt = 0;
}
else if (!bcnt) // else if first byte...
{
kcnt = 16;
fprintf(fp, "\n\t%s\t", defbstr);
}
else
{
putc(',', fp); // else separate bytes
kcnt++;
}
if ((pgmflags[adrs] & (PF_LABEL | PF_SYMBOL)) == (PF_LABEL | PF_SYMBOL))
cptr = NULL;
else
cptr = find_entry(pgmmem[adrs], symbol_count, sym_val_index);
if (cptr)
kcnt += fprintf(fp, "%s", cptr);
else
{
if (!(pgmflags[adrs] & PF_ASCII))
puthex(pgmmem[adrs] & 0xff);
else // user defined this as ascii text
{ // even though it's not; let's try
chr = pgmmem[adrs]; // to give him what he wants.
if (chr & 0x80) // if flagged binary byte because
{ // high bit is set...
chr &= 0x7f;
if (chr >= ' ' && chr <= 'z') // would it be ascii
{ // without bit 7 set?
kcnt += fprintf(fp, "'%c'+80h", chr); // yes
bcnt += 3;
}
else // else do as binary and remove
{ // ascii flag
puthex(pgmmem[adrs] & 0xff);
pgmflags[adrs] &= ~PF_ASCII;
}
}
else // high bit not set, so is
{ // really binary, not ascii
puthex(pgmmem[adrs] & 0xff);
pgmflags[adrs] &= ~PF_ASCII;
}
}
}
bcnt++;
if (bcnt >= BYTELINE || pgmflags[adrs] & PF_ICMT) // if max bytes per line...
{
bcnt = 0;
if (hexflag) // do ascii dump of previous bytes
{
do
{
putc('\t', fp);
kcnt = (kcnt + 8) & 0x78;
} while (kcnt < XSTOP);
fprintf(fp, "; %04x ", padrs);
for (k=0; k<=off; k++)
putc(ascii(pgmmem[padrs + k]), fp);
padrs += k;
off = 0;
}
}
else
off++;
if (pgmflags[adrs] & PF_ICMT)
output_icomment(adrs);
--byte_cnt;
adrs++;
}
if (off && hexflag) // generate comment line
{
do
{
putc('\t', fp);
kcnt = (kcnt + 8) & 0x78;
} while (kcnt < XSTOP);
fprintf(fp, "; %04x ", padrs); // show address and ascii for data
for (k=0; k<off; k++)
putc(ascii(pgmmem[padrs + k]), fp);
}
dump = 1;
}
// end of common.c
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -