static int assem_pass1(void)
{
	for( int i = 0; i < line_num; i++ ) {
		if(token_parsing(i) < 0) {
			return -1;
		}
	}

	for( int i = 0; i < line_num; i ++ ) {
		token *curr_token = token_table[i];
		if( curr_token->operator == NULL ) {
			continue;
		}

		int instruction_number = search_opcode( curr_token->operator );
		if( instruction_number < 0 ) {
			if( is_assembly_directive(curr_token->operator) ) {
				printf("%s\t", curr_token->label);
				printf("%s\t", curr_token->operator);
				printf("%8s\t\n", curr_token->operand[0]);
			} else {
				// error!
			}
			continue;
		}

		int opcode = get_opcode_of_instruction(instruction_number);

		if( curr_token->label ) {
			printf("%s\t", curr_token->label);
		} else {
			printf("%6s\t", "");
		}

		printf("%s\t", curr_token->operator);
		printf("%8s\t", curr_token->operand[0]);
		printf("0x%02X\n", opcode);
	}

	return -1;
}
Ejemplo n.º 2
0
void get_token(Token *t)
{
    /* check if we need to free the previous string */
    if (t->token_string != NULL)
        {
            free(t->token_string);
            t->token_string = NULL;
        }

    enum Token_type a = peek_token_type();
    if (a != Tsymbol)
        {
            t->type = a;
            t->token_string = copy_token_string(token_index, token_index);
            token_index += 1;
            if (debug) fprintf(stderr, "next token: %s\n", t->token_string);
            return;
        }

    if (isalpha(input_buffer[token_index]))
        {
            int j;
            for (j = token_index; j < input_line_length; j++)
                {
                    if (isalpha(input_buffer[j])) continue;
                    if (isdigit(input_buffer[j])) continue;
                    if (input_buffer[j] == '_') continue;
                    if (input_buffer[j] == '.') continue;
                    /* not alpha, digit or underbar or period */
                    break;
                }
            /* symbol is from token_index to j-1 */
            t->token_string = copy_token_string(token_index, j-1);
            token_index = j;
            if (debug) fprintf(stderr, "next token: %s\n", t->token_string);

            /* search to see if this token is an opcode */
            opcode *op = search_opcode(t->token_string);
            if (op != NULL)
                {
                    t->type = Topcode;
                    t->op = op;
                    return;
                }

            /* search to see if this token is a known symbol */
            symbol *sy = search_symbol(t->token_string);
            if (sy != NULL)
                {
                    t->type = Tsymbol;
                    t->sy = sy;
                    t->value = sy->value;
                    return;
                }

            /* we have a symbol that is not currently defined */
            t->type = Tsymbol;
            t->sy = NULL;
            t->value = 0;
            return;
        }

    /* A numeric constant */
    else if (isdigit(input_buffer[token_index]) || (input_buffer[token_index] == '-'))
        {
            int value;
            if (parse_constant(t, &value))
                {
                    t->type = Tconstant;
                    t->value = value;
                    if (debug) fprintf(stderr, "next token: constant %d\n", t->value);
                    return;
                }
        }

    /* A character constant: '.'  */
    else if ((input_buffer[token_index] == '\'') && (input_buffer[token_index+2] == '\''))
        {
            t->type = Tconstant;
            t->value = input_buffer[token_index+1];
            t->token_string = malloc(4);
            t->token_string[0] = t->token_string[2] = '\'';
            t->token_string[1] = t->value;
            t->token_string[3] = '\0';
            token_index += 3;
            if (debug) fprintf(stderr, "next token: char constant %d (%c)\n", t->value, t->value);
            return;
        }

    /* and anything else is just illegal */
    /* should we advance the token_index here? */
    t->type = Tillegal;
    if (debug) fprintf(stderr, "next token: illegal\n");
    return;
}