summaryrefslogtreecommitdiff
path: root/asmmon.c
diff options
context:
space:
mode:
Diffstat (limited to 'asmmon.c')
-rw-r--r--asmmon.c92
1 files changed, 64 insertions, 28 deletions
diff --git a/asmmon.c b/asmmon.c
index 198254c..dcffb67 100644
--- a/asmmon.c
+++ b/asmmon.c
@@ -9,6 +9,14 @@ uint8_t defined = 0;
uint8_t isfixup = 0;
+char lexeme[MAX_TOK];
+char *string[MAX_TOK];
+char *comment[MAX_TOK];
+uint16_t incl[MAX_TOK];
+struct line tokline[MAX_TOK];
+struct line tln[MAX_TOK];
+struct symbol *symbols[MAX_TOK];
+struct fixup *fixups[MAX_TOK];
static char tstr[2048];
@@ -319,8 +327,9 @@ void list(struct line *l, uint16_t start, uint16_t end, uint8_t all, uint8_t ln,
}
}
-uint64_t assemble(struct line *line, uint8_t dbg) {
- uint64_t bytecount = 0;
+void assemble(struct line *line, bytecount *bc, uint8_t dbg) {
+ bc->progsize = 0;
+ bc->datasize = 0;
uint64_t tmpaddr;
uint64_t value;
uint16_t flags = 0;
@@ -406,14 +415,11 @@ uint64_t assemble(struct line *line, uint8_t dbg) {
}
opsize = 0;
skip = 0;
- if ((flags & 0x53) == 0x42) {
+ if ((flags & 0x53) == 0x42 || (flags & 0x51) == 0x41) {
value = symbols[symid]->val;
} else {
value = op;
}
- if ((flags & 0x51) == 0x41) {
- value = symbols[symid]->val;
- }
if (flags & 0x220) {
switch (cm) {
case 0: value += aop; break;
@@ -449,15 +455,17 @@ uint64_t assemble(struct line *line, uint8_t dbg) {
c = string[str][k];
break;
}
+ bc->datasize++;
addr[tmpaddr++] = c;
}
addr[tmpaddr] = '\0';
- if (dbg) {
- printf("assemble(): ");
- printf("Placed string \"%s\"", string[str]);
- printf(", at address(es) $%"PRIX64"-$%"PRIX64".\n", address, tmpaddr);
- }
+ if (dbg) {
+ printf("assemble(): ");
+ printf("Placed string \"%s\"", string[str]);
+ printf(", at address(es) $%"PRIX64"-$%"PRIX64".\n", address, tmpaddr);
+ }
} else {
+ bc->datasize++;
addr[tmpaddr++] = value & 0xFF;
}
break;
@@ -467,14 +475,17 @@ uint64_t assemble(struct line *line, uint8_t dbg) {
addr[tmpaddr+5] = value >> 0x28;
addr[tmpaddr+4] = value >> 0x20;
tmp+=4;
+ bc->datasize+=4;
case DIR_DWORD:
addr[tmpaddr+3] = value >> 0x18;
addr[tmpaddr+2] = value >> 0x10;
tmp+=2;
+ bc->datasize+=2;
case DIR_WORD:
addr[tmpaddr+1] = value >> 0x08;
addr[tmpaddr ] = value & 0xFF;
tmp+=2;
+ bc->datasize+=2;
break;
case DIR_INCLUDE:
incl[inc_file++] = line[i].incl;
@@ -502,7 +513,7 @@ uint64_t assemble(struct line *line, uint8_t dbg) {
} else {
am = IMPL;
addr[tmpaddr++] = opcodes[ins][IMM];
- bytecount++;
+ bc->progsize++;
if (dbg) {
printf("assemble(): The instruction that is being used is, %s.\n", mne[ins]);
printf("assemble(): The addressing mode that this instruction is using is, %s.\n", adrmode[IMM]);
@@ -531,7 +542,7 @@ uint64_t assemble(struct line *line, uint8_t dbg) {
puts("Prefix byte detected.");
}
addr[tmpaddr++] = prefix;
- bytecount++;
+ bc->progsize++;
if (dbg) {
uint8_t addrsize = (prefix & 0x0C) >> 2;
uint8_t regsize = (prefix & 0x30) >> 4;
@@ -544,7 +555,7 @@ uint64_t assemble(struct line *line, uint8_t dbg) {
}
if (opcodes[ins][am] != 0xFF) {
addr[tmpaddr++] = opcodes[ins][am];
- bytecount++;
+ bc->progsize++;
if (dbg) {
printf("assemble(): The instruction that is being used is, %s.\n", mne[ins]);
printf("assemble(): The addressing mode that this instruction is using is, %s.\n", adrmode[am]);
@@ -591,23 +602,27 @@ uint64_t assemble(struct line *line, uint8_t dbg) {
break;
}
tmpaddr += tmp;
- bytecount += tmp;
+ bc->progsize += tmp;
tmp = 0;
}
}
if (dbg) {
printf("assemble(): The address that this line starts at is, $%"PRIX64".\n", address);
printf("assemble(): The address that this line ends on is, $%"PRIX64".\n", tmpaddr);
+
printf("assemble(): The program size is now at");
- printf(", %"PRIu64" bytes in decimal", bytecount);
- printf(", and $%"PRIX64" bytes in hex.\n", bytecount);
+ printf(", %"PRIu64" bytes in decimal", bc->progsize);
+ printf(", and $%"PRIX64" bytes in hex.\n", bc->progsize);
+
+ printf("assemble(): The data size is now at");
+ printf(", %"PRIu64" bytes in decimal", bc->datasize);
+ printf(", and $%"PRIX64" bytes in hex.\n", bc->datasize);
}
}
- return bytecount;
}
int asmmon(const char *fn) {
- FILE *fp;
+ FILE *fp = NULL;
FILE *fp2 = NULL;
char *path = malloc(0x400);
if (strcasecmp(fn, "stdin")) {
@@ -631,7 +646,9 @@ int asmmon(const char *fn) {
uint8_t done = 0;
uint8_t use_lexer = 1;
uint64_t address = 0;
- uint64_t bytecount = 0;
+ bytecount bc;
+ uint64_t progsize = 0;
+ uint64_t datasize = 0;
uint8_t dbg = 0;
uint8_t isinclude = 0;
uint16_t tmp_lineidx = 0;
@@ -680,7 +697,9 @@ int asmmon(const char *fn) {
switch (cmds) {
case 0x01:
free(path);
- fclose(fp);
+ if (fp != NULL) {
+ fclose(fp);
+ }
if (fp2 != NULL) {
fclose(fp2);
}
@@ -764,17 +783,29 @@ int asmmon(const char *fn) {
break;
case 0x08:
if (!isinclude) {
- puts("Assembling program.");
- bytecount = assemble(tokline, dbg);
+ printf("Assembling %s\n", (strcasecmp(fn, "stdin")) ? fn : "typed in program.");
+ assemble(tokline, &bc, dbg);
+ progsize = bc.progsize;
+ datasize = bc.datasize;
+ printf("Finished assembling %s\n", (strcasecmp(fn, "stdin")) ? fn : "typed in program.");
+ printf("%"PRIu64"/$%"PRIX64" bytes of program code.\n", progsize, progsize);
+ printf("%"PRIu64"/$%"PRIX64" bytes of data.\n", datasize, datasize);
+ putchar('\n');
} else {
- bytecount += assemble(tln, dbg);
+ printf("Assembling %s\n", string[incl[inc_count]]);
+ assemble(tln, &bc, dbg);
+ progsize += bc.progsize;
+ datasize += bc.datasize;
+ printf("Finished assembling %s\n", string[incl[inc_count]]);
+ printf("%"PRIu64"/$%"PRIX64" bytes of program code.\n", bc.progsize, bc.progsize);
+ printf("%"PRIu64"/$%"PRIX64" bytes of data.\n", bc.datasize, bc.datasize);
+ putchar('\n');
}
isinclude = (inc_file != 0);
if (inc_file) {
size = strlen(path)+strlen(string[incl[inc_count]])+1;
char *fn2 = malloc(size+1);
sprintf(fn2, "%s/%s", path, string[incl[inc_count]]);
- printf("%s\n", fn2);
if (!tmp_lineidx) {
tmp_lineidx = lineidx;
}
@@ -795,8 +826,9 @@ int asmmon(const char *fn) {
lineidx = tmp_lineidx;
}
if (!isinclude) {
- puts("Finished assembling program.");
- printf("Total Assembled Program Size: %"PRIu64"/$%"PRIX64" bytes.\n", bytecount, bytecount);
+ puts("Finished assembling.");
+ printf("Total Assembled Program Size: %"PRIu64"/$%"PRIX64" bytes.\n", progsize, progsize);
+ printf("Total Assembled Data Size: %"PRIu64"/$%"PRIX64" bytes.\n", datasize, datasize);
}
break;
case 0x10:
@@ -850,7 +882,11 @@ int asmmon(const char *fn) {
}
}
free(path);
- fclose(fp);
+
+ if (fp != NULL) {
+ fclose(fp);
+ }
+
if (fp2 != NULL) {
fclose(fp2);
}