From 726d27c7b775f34bd587f740c1f2da5850ad763a Mon Sep 17 00:00:00 2001 From: org Date: Fri, 15 Sep 2023 11:43:55 +0300 Subject: [PATCH 1/3] assemble_text --- Tools/Breakasm/asm.cpp | 117 ++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 65 deletions(-) diff --git a/Tools/Breakasm/asm.cpp b/Tools/Breakasm/asm.cpp index 3f98cf7..40d7b2c 100644 --- a/Tools/Breakasm/asm.cpp +++ b/Tools/Breakasm/asm.cpp @@ -504,9 +504,21 @@ static void parse_line (char **text, line& ln) // **************************************************************** +int emit_mode = 1; // 1: output byte stream; 0: just change origin + +void set_emit_mode(int mode) +{ + emit_mode = mode; +} + void emit (uint8_t b) { - PRG[org++] = b; + if (emit_mode) { + PRG[org++] = b; + } + else { + org++; + } } static oplink optab[] = { @@ -577,9 +589,45 @@ static void cleanup (void) } } -int assemble (char *text, char* source_name, uint8_t *prg) +static void assemble_text(char* text) { + oplink* opl; line l; + + while (1) { + if (*text == 0) break; + parse_line(&text, l); + + //printf ( "%s: \'%s\' \'%s\'\n", l.label, l.cmd, l.op ); + + // Add label + if (strlen(l.label) > 1) { + add_label(l.label, org); + } + + // Execute command + if (strlen(l.cmd) > 1) { + opl = optab; + while (1) { + if (opl->name == NULL) { + printf("ERROR(%s,%i): Unknown command %s\n", get_source_name().c_str(), get_linenum(), l.cmd); + break; + } + else if (!_stricmp(opl->name, l.cmd)) { + _strupr(l.cmd); + opl->handler(l.cmd, l.op); + break; + } + opl++; + } + if (stop) break; + } + nextline(); + } +} + +int assemble (char *text, char* source_name, uint8_t *prg) +{ oplink *opl; PRG = prg; org = 0; @@ -603,36 +651,7 @@ int assemble (char *text, char* source_name, uint8_t *prg) } nextline(); - while (1) { - if (*text == 0) break; - parse_line (&text, l); - - //printf ( "%s: \'%s\' \'%s\'\n", l.label, l.cmd, l.op ); - - // Add label - if ( strlen (l.label) > 1 ) { - add_label ( l.label, org ); - } - - // Execute command - if ( strlen (l.cmd) > 1 ) { - opl = optab; - while (1) { - if ( opl->name == NULL ) { - printf ("ERROR(%s,%i): Unknown command %s\n", get_source_name().c_str(), get_linenum(), l.cmd); - break; - } - else if ( !_stricmp (opl->name, l.cmd) ) { - _strupr (l.cmd); - opl->handler (l.cmd, l.op); - break; - } - opl++; - } - if (stop) break; - } - nextline(); - } + assemble_text(text); // Patch jump/branch offsets. do_expr_labels(); @@ -656,42 +675,10 @@ int assemble (char *text, char* source_name, uint8_t *prg) /// Source file name void assemble_include(char* text, char* source_name) { - line l; - oplink* opl; - source_name_stack.push_back(source_name); linenum_stack.push_back(1); - while (1) { - if (*text == 0) break; - parse_line(&text, l); - - //printf ( "%s: \'%s\' \'%s\'\n", l.label, l.cmd, l.op ); - - // Add label - if (strlen(l.label) > 1) { - add_label(l.label, org); - } - - // Execute command - if (strlen(l.cmd) > 1) { - opl = optab; - while (1) { - if (opl->name == NULL) { - printf("ERROR(%s,%i): Unknown command %s\n", get_source_name().c_str(), get_linenum(), l.cmd); - break; - } - else if (!_stricmp(opl->name, l.cmd)) { - _strupr(l.cmd); - opl->handler(l.cmd, l.op); - break; - } - opl++; - } - if (stop) break; - } - nextline(); - } + assemble_text(text); source_name_stack.pop_back(); linenum_stack.pop_back(); From 92c442a4eb1b789aca18d268a621b16073b0876e Mon Sep 17 00:00:00 2001 From: org Date: Fri, 15 Sep 2023 12:34:13 +0300 Subject: [PATCH 2/3] listing --- Tools/Breakasm/asm.cpp | 17 +++++++++++++++-- Tools/Breakasm/asmexpr.cpp | 6 ++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Tools/Breakasm/asm.cpp b/Tools/Breakasm/asm.cpp index 40d7b2c..347d6b0 100644 --- a/Tools/Breakasm/asm.cpp +++ b/Tools/Breakasm/asm.cpp @@ -593,18 +593,19 @@ static void assemble_text(char* text) { oplink* opl; line l; + int listing = 0; while (1) { if (*text == 0) break; parse_line(&text, l); - //printf ( "%s: \'%s\' \'%s\'\n", l.label, l.cmd, l.op ); - // Add label if (strlen(l.label) > 1) { add_label(l.label, org); } + long org_before = org; + // Execute command if (strlen(l.cmd) > 1) { opl = optab; @@ -622,6 +623,18 @@ static void assemble_text(char* text) } if (stop) break; } + + // Listing + if (listing) { + printf("0x%08X: ", org_before); + int num_bytes = org - org_before; + for (int i = 0; i < num_bytes; i++) { + printf("%02X ", PRG[org_before + i]); + } + printf("%s: \'%s\' \'%s\'\n", l.label, l.cmd, l.op); + printf("\n"); + } + nextline(); } } diff --git a/Tools/Breakasm/asmexpr.cpp b/Tools/Breakasm/asmexpr.cpp index ad1b9a8..569d793 100644 --- a/Tools/Breakasm/asmexpr.cpp +++ b/Tools/Breakasm/asmexpr.cpp @@ -413,7 +413,7 @@ static node_t * evaluate (std::list& tree, node_t * expr, long *lvalue, } else { label_s* label = label_lookup(token->string); - if (label) { + if (label && label->orig != UNDEF) { mvalue = label->orig; } else { @@ -583,7 +583,9 @@ long eval_expr(char* text, bool debug, bool quiet) // Execute the tree long result = 0; - evaluate(tree.nodes, root->rvalue->rvalue, &result, quiet); + if (root) { + evaluate(tree.nodes, root->rvalue->rvalue, &result, quiet); + } if (debug) printf("Source expression: %s, result: %d (0x%08X)\n", text, result, result); From 108972f1b7b352b596cc491849cae5c37b4771e9 Mon Sep 17 00:00:00 2001 From: org Date: Fri, 15 Sep 2023 13:42:45 +0300 Subject: [PATCH 3/3] Update asmexpr.cpp --- Tools/Breakasm/asmexpr.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Tools/Breakasm/asmexpr.cpp b/Tools/Breakasm/asmexpr.cpp index 569d793..7f797ed 100644 --- a/Tools/Breakasm/asmexpr.cpp +++ b/Tools/Breakasm/asmexpr.cpp @@ -359,7 +359,7 @@ static node_t* addnode(std::list& tree, token_t* token, int depth) } // executing the tree (semantic analysis) -static node_t * evaluate (std::list& tree, node_t * expr, long *lvalue, bool quiet) +static node_t * evaluate (std::list& tree, node_t * expr, long *lvalue, bool quiet, bool& syntax_error) { node_t * curr; long rvalue = 0, mvalue; @@ -379,6 +379,7 @@ static node_t * evaluate (std::list& tree, node_t * expr, long *lvalue, token = curr->token; if ( token->type == TOKEN_OP && isunary(token->op) ) { if (curr->rvalue == NULL) { + syntax_error = true; if (!quiet) { printf("Missing identifier\n"); errors++; @@ -401,7 +402,7 @@ static node_t * evaluate (std::list& tree, node_t * expr, long *lvalue, if ( curr->depth > expr->depth ) { // nested expression if (debug) printf ( "SUBEVAL " ); - curr = evaluate (tree, curr, &mvalue, quiet); + curr = evaluate (tree, curr, &mvalue, quiet, syntax_error); //printf ( "SUB LVALUE : %i\n", mvalue.num.value ); } else if ( token->type == TOKEN_IDENT) { @@ -413,10 +414,11 @@ static node_t * evaluate (std::list& tree, node_t * expr, long *lvalue, } else { label_s* label = label_lookup(token->string); - if (label && label->orig != UNDEF) { + if (label && label->orig != UNDEF && !label->composite) { mvalue = label->orig; } else { + syntax_error = true; if (!quiet) { printf("Undefined identifier: %s", token->string); errors++; @@ -468,6 +470,7 @@ static node_t * evaluate (std::list& tree, node_t * expr, long *lvalue, else rvalue = mvalue; } else { + syntax_error = true; if (!quiet) { printf("Identifier required\n"); errors++; @@ -583,8 +586,12 @@ long eval_expr(char* text, bool debug, bool quiet) // Execute the tree long result = 0; + bool syntax_error = false; if (root) { - evaluate(tree.nodes, root->rvalue->rvalue, &result, quiet); + evaluate(tree.nodes, root->rvalue->rvalue, &result, quiet, syntax_error); + } + if (syntax_error) { + result = 0; } if (debug)