~grimmware/lc3

3d5d8a66df3c9916e534cb24a2e4399afc12e4db — glenda 1 year, 2 months ago 77a9b54
String literals for .STRINGZ
2 files changed, 43 insertions(+), 14 deletions(-)

M asm.c
M tests/hello.asm
M asm.c => asm.c +41 -13
@@ 69,7 69,6 @@ syntaxerr(char* msg)
		lineno,
		msg
	);
	free(msg);
	exits("Syntax error");
}



@@ 360,21 359,50 @@ parseline(char *l)
				}
				return 1;
			} else if(strcmp(t,".STRINGZ") == 0) {
				// FIXME very hex
				l+=2;
				gettoken(&t, l);
				for(int i = 0; i<=strlen(t); i+=2){
					u16int p = 0;
					p |= t[i];
					if(t[i] != 0)
						p|= t[i+1] << 8;
					inst = newinst(OP_DATA);
					inst->p_arg[0] = basenparse(16, 16, (char *) &p);
					inst->argc++;
					pc++;
				l++;
				if(l[0] == 'x'){
					l++;
					gettoken(&t, l);
					for(int i = 0; i<=strlen(t); i+=2){
						u16int p = 0;
						p |= t[i];
						if(t[i] != 0)
							p|= t[i+1] << 8;
						inst = newinst(OP_DATA);
						inst->p_arg[0] = basenparse(16, 16, (char *) &p);
						inst->argc++;
						pc++;
					}
				} else if (l[0] == '"') {
					gettoken(&t, l);
					if(t[strlen(t) - 1] != '"')
						syntaxerr("No closing quote");
					t++;
					for(int i = 0; i<(strlen(t) - 1); i++) {
						inst = newinst(OP_DATA);
						if(t[i] == '\\'){
							i++;
							switch(t[i]){
							case 'n':
								inst->p_arg[0] = '\n';
								break;
							case 't':
								inst->p_arg[0] = '\t';
								break;
							}
						} else {
							inst->p_arg[0] = (u16int) t[i];
						}
						inst->argc++;
						pc++;
					}
				} else {
					syntaxerr(".STRINGZ only supports hex and string literals");
				}
				goto Nop;
				return 1;
			} else if(strcmp(t,"NOP") == 0) {
	Nop:
				inst = newinst(OP_BR);
				inst->arg[0] = "x0";
				inst->argc++;

M tests/hello.asm => tests/hello.asm +2 -1
@@ 29,5 29,6 @@ O	.FILL	x6f
	BRnp	x0
	BR	x0
	HALT
WRLD	.STRINGZ	x20776f726c64210a
HXWRLD	.STRINGZ	x20776f726c64210a
WRLD	.STRINGZ	" world\n"
	.END