Skip to content

Commit

Permalink
Merge pull request #472 from ogamespec/main
Browse files Browse the repository at this point in the history
Breakasm workout
  • Loading branch information
ogamespec authored Sep 21, 2023
2 parents 55a2ed3 + 108972f commit 9355fac
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 69 deletions.
130 changes: 65 additions & 65 deletions Tools/Breakasm/asm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand Down Expand Up @@ -577,9 +589,58 @@ static void cleanup (void)
}
}

int assemble (char *text, char* source_name, uint8_t *prg)
static void assemble_text(char* text)
{
oplink* opl;
line l;
int listing = 0;

while (1) {
if (*text == 0) break;
parse_line(&text, l);

// 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;
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;
}

// 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();
}
}

int assemble (char *text, char* source_name, uint8_t *prg)
{
oplink *opl;
PRG = prg;
org = 0;
Expand All @@ -603,36 +664,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();
Expand All @@ -656,42 +688,10 @@ int assemble (char *text, char* source_name, uint8_t *prg)
/// <param name="source_name">Source file name</param>
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();
Expand Down
17 changes: 13 additions & 4 deletions Tools/Breakasm/asmexpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ static node_t* addnode(std::list<node_t*>& tree, token_t* token, int depth)
}

// executing the tree (semantic analysis)
static node_t * evaluate (std::list<node_t*>& tree, node_t * expr, long *lvalue, bool quiet)
static node_t * evaluate (std::list<node_t*>& tree, node_t * expr, long *lvalue, bool quiet, bool& syntax_error)
{
node_t * curr;
long rvalue = 0, mvalue;
Expand All @@ -379,6 +379,7 @@ static node_t * evaluate (std::list<node_t*>& 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++;
Expand All @@ -401,7 +402,7 @@ static node_t * evaluate (std::list<node_t*>& 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) {
Expand All @@ -413,10 +414,11 @@ static node_t * evaluate (std::list<node_t*>& tree, node_t * expr, long *lvalue,
}
else {
label_s* label = label_lookup(token->string);
if (label) {
if (label && label->orig != UNDEF && !label->composite) {
mvalue = label->orig;
}
else {
syntax_error = true;
if (!quiet) {
printf("Undefined identifier: %s", token->string);
errors++;
Expand Down Expand Up @@ -468,6 +470,7 @@ static node_t * evaluate (std::list<node_t*>& tree, node_t * expr, long *lvalue,
else rvalue = mvalue;
}
else {
syntax_error = true;
if (!quiet) {
printf("Identifier required\n");
errors++;
Expand Down Expand Up @@ -583,7 +586,13 @@ long eval_expr(char* text, bool debug, bool quiet)
// Execute the tree

long result = 0;
evaluate(tree.nodes, root->rvalue->rvalue, &result, quiet);
bool syntax_error = false;
if (root) {
evaluate(tree.nodes, root->rvalue->rvalue, &result, quiet, syntax_error);
}
if (syntax_error) {
result = 0;
}

if (debug)
printf("Source expression: %s, result: %d (0x%08X)\n", text, result, result);
Expand Down

0 comments on commit 9355fac

Please sign in to comment.