all repos — nand2tetris @ c52dc71dd10d530e038cbed1aa273c206fbd3326

my nand2tetris progress

projects/06/assembler2/assembler2.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DBGLOG printf

#define valid_symbol_char(c) (('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') \
                           || ('a' <= c && c <= 'z') || c == '_' || c == '.'   \
		                                     || c == '$' || c == ':')

#define error(...) (fprintf(stderr, __VA_ARGS__), -1)

#define MAX_LINE_LEN            256 // TODO: in/excludes NULL terminator?
#define MAX_SYMBOL_LEN          MAX_LINE_LEN - 2 + 1 // + 1 for NULL terminator
#define MAX_SYMBOLS             32768 // TODO remove me

char *g_asm_line;                 // currently-read line for convenience
size_t g_asm_line_number;         // current line number
size_t g_instruction_number = 0;  // instruction offset

struct symbol_t {
	char *symbolstr;
	uint16_t value;
};

char rom[32768];
size_t rom_index = 0;

char     symbol_strs[32768 + 23][MAX_SYMBOL_LEN]; // TODO: double-check 2D array
uint16_t symbol_vals[32768];
uint16_t symbol_index;

#define RESERVED_LABEL_NUM      23 // TODO: remove me

// label and variable symbols will effectively be treated the same; the only
// difference is in their use (in a hack program)
struct symbol_t *g_symbol_list[32768 + RESERVED_LABEL_NUM];
size_t g_symbol_list_len = 0;
size_t g_current_variable_address = 16; // address for next variable symbol


void print_binary_word16(uint16_t w)
{
        unsigned char i, msb;
        char binary_string[17];
        binary_string[16] = '\0';

        for (i = 0; i < 16; ++i) {
                msb = (char)(w >> 15);

                if (msb != 0) binary_string[i] = '1';
                else          binary_string[i] = '0';
                w <<= 1;
        }
        printf("%s", binary_string);
}

static uint32_t myatoi(const char *str)
{
	size_t i;
	uint32_t ret = 0;

	for (i = 0; i < 5 && '0' <= str[i] && str[i] <= '9'; ++i) {
		ret = (ret * 10) + (str[i] - 0x30);
	}

	return ret;
}

size_t skip_whitespace(const char *line, size_t n) {
	size_t i = 0;
	char c = line[i];

	for (i; (c == ' ' || c == '\t') && c != '\0' && i < n; ++i);
	return i; // TODO double-check above loop logic
}

// TODO: will still need to handle multi-line-comments
bool is_single_line_comment(const char *line) {
	size_t i;

	i = skip_whitespace(line, MAX_LINE_LEN);
	if (line[i] == '/')
		if (line[i + 1] == '/' && !(i >= MAX_LINE_LEN))
			return true;
	return false;
}

// TODO FIX!
void debug_dump_all_symbols()
{
	size_t i;

	DBGLOG("-------- DEBUG SYMBOL DUMP --------\n");
	for (i = 0; i < symbol_index; ++i) {
		DBGLOG("symbol (%s, %hu)\n", symbol_strs[i], symbol_vals[i]);
	}
	DBGLOG("-------- END SYMBOL DUMP --------\n");
}

bool lookup_symbol(const char *str, uint16_t *val)
{
	size_t i;

	for (i = 0; i < sizeof(symbol_strs) / sizeof(symbol_strs[0]); ++i) {
		if (strncmp(symbol_strs[i], str, MAX_SYMBOL_LEN) == 0) {
			*val = symbol_vals[i];
			return true;
		}
	}

	return false;
}

bool add_symbol(const char *str, uint16_t val)
{
	size_t i;
	uint16_t tmp;

	if (symbol_index > 32767) {
		// TODO: print appropriate error message
		return false;
	}

	if (lookup_symbol(str, &tmp)) {
		// TODO: print appropriate error message
		return false;
	}

	strncpy(symbol_strs[symbol_index], str, MAX_SYMBOL_LEN);
	symbol_vals[symbol_index] = val;
	++symbol_index;

	return true;
}

// pre-fill symbol lists with 'reserved' symbols and values
bool init_symbol_list()
{
	size_t i;

	char *reserved_strs[] = {"R0", "R1", "R2", "R3", "R4", "R5",
	                         "R6", "R7", "R8", "R9", "R10", "R11",
	                         "R12", "R13", "R14", "R15", "SP", "LCL",
                                 "ARG", "THIS", "THAT", "SCREEN", "KBD"};
	uint16_t reserved_vals[] = {0, 1, 2, 3, 4, 5,
	                            6, 7, 8, 9, 10, 11,
	                            12, 13, 14, 15, 0, 1,
	                            2, 3, 4, 0x4000, 0x6000};

	for (i = 0; i < sizeof(reserved_strs) / sizeof(reserved_strs[0]); ++i) {
		if (!add_symbol(reserved_strs[i], reserved_vals[i]))
			return false;
	}

	return true;
}

bool pass2(FILE *in_file)
{
	bool result = false; 
	uint16_t instruction;
	char in_line[MAX_LINE_LEN];
	size_t i, file_line, in_line_len;

	file_line = 1;
	while (fgets(in_line, MAX_LINE_LEN, in_file) != NULL) {  // parse loop
		for (i = 0; in_line[i] != '\0'; ++i) {      // remove newlines
			if (in_line[i] == '\n' || in_line[i] == '\r') {
				in_line[i] = '\0';
				break;
			}
			++in_line_len;
		}

		if (in_line_len == 0 || in_line_len == 1)
			continue;

		g_asm_line = in_line;
		g_asm_line_number = file_line;

		printf("line %lu: %s\n", file_line, in_line);
		result = true;
		/*
		result = parse_line(in_line, &instruction);

		if (result) {
			print_binary_word16(instruction);
			putchar('\n');
			//TODO: put instruction in ROM
		}
		*/

		++file_line;
	}

	return result;
}

bool pass1(FILE *in_file)
{
	bool result = false; 
	uint16_t instruction;
	char in_line[MAX_LINE_LEN];
	size_t i, file_line, in_line_len;

	if (!init_symbol_list()) {
		return false;
	}

	file_line = 1;
	// first pass to read labels and associate with values
	while (fgets(in_line, MAX_LINE_LEN, in_file) != NULL) {  // parse loop
		for (i = 0; in_line[i] != '\0'; ++i) {      // remove newlines
			if (in_line[i] == '\n' || in_line[i] == '\r') {
				in_line[i] = '\0';
				break;
			}
			++in_line_len;
		}

		if (in_line_len == 0 || in_line_len == 1)
			continue;

		if (in_line_len >= 2)
			if (in_line[0] == '/' && in_line[1] == '/')
				continue; // comment found

		g_asm_line = in_line;
		g_asm_line_number = file_line;
		printf("line %lu. If this was real, it would call "
		       "parse_line_for_label()\n", file_line);
		result = true;
		////result = parse_line_for_label(in_line);
		//if (result)
		//	DBGLOG("DEBUG: label found in line %s\n", in_line);

		++file_line;
	}

	debug_dump_all_symbols();

	return result;
}


char *usage_msg = "Usage: assembler1 [path/to/file.asm]\n";

int main(int argc, char *argv[])
{
	FILE *in_file, *out_file;
	char *in_file_path, *out_file_path;
	if (argc != 3)                    // requires 2 arguments
		return error(usage_msg);  // TODO: eventually support STDOUT

	in_file_path = argv[1];
	out_file_path = argv[2];

	in_file = fopen(in_file_path, "r");
	if (in_file == NULL)
		return error("failed to open assembly file for reading\n");

	if(!pass1(in_file))
		return error("failed to parse labels/variables in file\n");

	if (fseek(in_file, 0, SEEK_SET))
		return error("failed to re-read file from beginning\n");

	if(!pass2(in_file))
		return error("failed to parse assembly in file\n");

	out_file = fopen(out_file_path, "wb");
	if (out_file == NULL)
		return error("failed to open output file for writing\n");

	//fwrite(assembled, sizeof(assembled), 1, out_fp);
	if (fclose(in_file))
		return error("failed to close assembly file\n");
	if (fclose(out_file))
		return error("failed to close output file\n");

	return 0;
}