static int parse_align(struct _asm_context *asm_context) { int num; if (eval_expression(asm_context, &num) == -1 || (num != 16 && num != 32)) { print_error("align expects 16 or 32", asm_context); return -1; } if (num == 16) { if ((asm_context->address&1) != 0) { memory_write_inc(asm_context, 0, DL_DATA); } } else if (num == 32) { if ((asm_context->address&3) != 0) { int n; for (n = (asm_context->address&3); n < 4; n++) { memory_write_inc(asm_context, 0, DL_DATA); } } } return 0; }
int parse_dw(struct _asm_context *asm_context) { char token[TOKENLEN]; int token_type; int data32; unsigned short int data16; if (asm_context->segment==SEGMENT_BSS) { printf("Error: .bss segment doesn't support initialized data at %s:%d\n", asm_context->filename, asm_context->line); return -1; } while(1) { // if the user has a comma at the end, but no data, this is okay token_type=get_token(asm_context, token, TOKENLEN); if (token_type==TOKEN_EOL || token_type==TOKEN_EOF) break; pushback(asm_context, token, token_type); if (eval_expression(asm_context, &data32)==-1) { eat_operand(asm_context); } data16=(unsigned short)data32; if (asm_context->memory.endian==ENDIAN_LITTLE) { memory_write_inc(asm_context, data16&255, DL_DATA); memory_write_inc(asm_context, data16>>8, DL_DATA); } else {
int parse_dc16(struct _asm_context *asm_context) { char token[TOKENLEN]; int token_type; int data32; uint16_t data16; if (asm_context->segment == SEGMENT_BSS) { printf("Error: .bss segment doesn't support initialized data at %s:%d\n", asm_context->filename, asm_context->line); return -1; } while(1) { // if the user has a comma at the end, but no data, this is okay token_type = tokens_get(asm_context, token, TOKENLEN); if (token_type == TOKEN_EOL || token_type == TOKEN_EOF) break; tokens_push(asm_context, token, token_type); if (eval_expression(asm_context, &data32) != 0) { if (asm_context->pass == 2) { return -1; } eat_operand(asm_context); data32 = 0; } if (data32 < -32768 || data32 > 0xffff) { print_error_range("dc16", -32768, 0xffff, asm_context); return -1; } data16 = (uint16_t)data32; if (asm_context->memory.endian == ENDIAN_LITTLE) { memory_write_inc(asm_context, data16 & 255, DL_DATA); memory_write_inc(asm_context, data16 >> 8, DL_DATA); } else {
int parse_binfile(struct _asm_context *asm_context) { FILE *in; char token[TOKENLEN]; unsigned char buffer[8192]; //int token_type; int len; int n; if (asm_context->segment == SEGMENT_BSS) { printf("Error: .bss segment doesn't support initialized data at %s:%d\n", asm_context->filename, asm_context->line); return -1; } get_token(asm_context, token, TOKENLEN); #ifdef DEBUG printf("binfile file %s.\n", token); #endif in = fopen(token, "rb"); if (in == NULL) { printf("Cannot open binfile file '%s' at %s:%d\n", token, asm_context->filename, asm_context->line); return -1; } while(1) { len = fread(buffer, 1, 8192, in); if (len <= 0) break; for (n = 0; n < len; n++) { memory_write_inc(asm_context, buffer[n], DL_DATA); } asm_context->data_count += len; } fclose(in); return 0; }
int parse_db(struct _asm_context *asm_context, int null_term_flag) { char token[TOKENLEN]; int token_type; int data32; if (asm_context->segment == SEGMENT_BSS) { printf("Error: .bss segment doesn't support initialized data at %s:%d\n", asm_context->filename, asm_context->line); return -1; } while(1) { token_type = tokens_get(asm_context, token, TOKENLEN); if (token_type == TOKEN_EOL || token_type == TOKEN_EOF) break; if (token_type == TOKEN_QUOTED) { uint8_t *s = (uint8_t *)token; while(*s != 0) { if (*s == '\\') { int e = tokens_escape_char(asm_context, s); if (e == 0) { return -1; } s = s + e; } memory_write_inc(asm_context, *s, DL_DATA); asm_context->data_count++; s++; } if (null_term_flag == 1) { memory_write_inc(asm_context, 0, DL_DATA); asm_context->data_count++; } } else { tokens_push(asm_context, token, token_type); if (eval_expression(asm_context, &data32) != 0) { if (asm_context->pass == 2) { return -1; } eat_operand(asm_context); data32 = 0; } if (data32 < -128 || data32 > 0xff) { print_error_range("db", -128, 0xff, asm_context); return -1; } memory_write_inc(asm_context, (uint8_t)data32, DL_DATA); asm_context->data_count++; } token_type = tokens_get(asm_context, token, TOKENLEN); if (token_type == TOKEN_EOL || token_type == TOKEN_EOF) { break; } if (IS_NOT_TOKEN(token,',')) { print_error_expecting(",", token, asm_context); return -1; } } asm_context->line++; return 0; }
int parse_db(struct _asm_context *asm_context, int null_term_flag) { char token[TOKENLEN]; int token_type; int data32; if (asm_context->segment==SEGMENT_BSS) { printf("Error: .bss segment doesn't support initialized data at %s:%d\n", asm_context->filename, asm_context->line); return -1; } while(1) { token_type=get_token(asm_context, token, TOKENLEN); if (token_type==TOKEN_EOL || token_type==TOKEN_EOF) break; if (token_type==TOKEN_QUOTED) { unsigned char *s=(unsigned char *)token; while(*s!=0) { if (*s=='\\') { int e=escape_char(asm_context, s); if (e==0) { return -1; } s=s+e; } memory_write_inc(asm_context, *s, DL_DATA); asm_context->data_count++; s++; } if (null_term_flag==1) { memory_write_inc(asm_context, 0, DL_DATA); asm_context->data_count++; } } else { pushback(asm_context, token, token_type); if (eval_expression(asm_context, &data32)==-1) { eat_operand(asm_context); } memory_write_inc(asm_context, (unsigned char)data32, DL_DATA); asm_context->data_count++; } token_type=get_token(asm_context, token, TOKENLEN); if (token_type==TOKEN_EOL || token_type==TOKEN_EOF) break; if (IS_NOT_TOKEN(token,',')) { printf("Parse error: expecting a ',' on line %d.\n", asm_context->line); return -1; } } asm_context->line++; return 0; }