println, gt
This commit is contained in:
parent
6a955679cd
commit
251d214f25
@ -1,17 +1,19 @@
|
||||
@include "arch.vc6asm"
|
||||
@include "macros.vc6asm"
|
||||
|
||||
@const STACK_BASE 0xfff
|
||||
@const HEAP_BASE 0x1000
|
||||
|
||||
start:
|
||||
mov bp, 0x1000
|
||||
mov bp, STACK_BASE
|
||||
mov sp, bp
|
||||
|
||||
mov r1, .splash
|
||||
mov r2, VCD_BEGIN + (0 * 40 + 0)
|
||||
call println
|
||||
|
||||
mov r0, 512
|
||||
mov r1, 1
|
||||
jr .l0_cond
|
||||
jmp .l0_cond
|
||||
.l0_body:
|
||||
store [DISK_BLOCK_ID], r1
|
||||
store [DISK_ADDR], r0
|
||||
@ -20,48 +22,68 @@ start:
|
||||
add r0, 512
|
||||
add r1, 1
|
||||
.l0_cond:
|
||||
lr r2, .l0_body
|
||||
clf
|
||||
cmp r1, BOOT_SIZE / 512 + 2
|
||||
and fl, FL_LT
|
||||
cmov ip, r2
|
||||
jlt .l0_body
|
||||
|
||||
|
||||
.spin:
|
||||
hlt
|
||||
jr .spin
|
||||
jmp .spin
|
||||
|
||||
.splash:
|
||||
dw "VC6 boot loader\0"
|
||||
dw "VC6 boot loader\n\0"
|
||||
|
||||
display:
|
||||
.x: dw 0
|
||||
.y: dw 0
|
||||
|
||||
println:
|
||||
push bp
|
||||
mov bp, sp
|
||||
sub sp, 2
|
||||
|
||||
store [sp], r1
|
||||
stea r0, r2, sp, 1
|
||||
|
||||
mov r1, 0
|
||||
jr .l0_cond
|
||||
mov r0, r1
|
||||
jmp .l0_cond
|
||||
.l0_body:
|
||||
ldea r3, sp, 1
|
||||
load r2, [r0]
|
||||
|
||||
cmp r2, '\n'
|
||||
jeq .newline
|
||||
|
||||
load r1, [display.x]
|
||||
load r3, [display.y]
|
||||
|
||||
add r3, r1
|
||||
load r2, [sp]
|
||||
add r2, r1
|
||||
load r2, [r2]
|
||||
add r3, VCD_BEGIN
|
||||
|
||||
store [r3], r2
|
||||
|
||||
add r1, 1
|
||||
|
||||
cmp r1, 40
|
||||
jlt .no_newline
|
||||
.newline:
|
||||
mov r1, 0
|
||||
|
||||
load r3, [display.y]
|
||||
add r3, 40
|
||||
store [display.y], r3
|
||||
.no_newline:
|
||||
|
||||
store [display.x], r1
|
||||
|
||||
add r0, 1
|
||||
.l0_cond:
|
||||
load r2, [sp]
|
||||
add r2, r1
|
||||
load r2, [r2]
|
||||
clf
|
||||
lr r3, .l0_body
|
||||
cmp r2, '\0'
|
||||
not fl
|
||||
and fl, FL_EQ
|
||||
cmov ip, r3
|
||||
load r1, [r0]
|
||||
cmp r1, '\0'
|
||||
jgt .l0_body
|
||||
|
||||
load r3, [display.y]
|
||||
add r3, 40
|
||||
store [display.y], r3
|
||||
|
||||
mov r1, 0
|
||||
store [display.x], r1
|
||||
|
||||
.leave:
|
||||
mov sp, bp
|
||||
|
||||
@ -2,10 +2,61 @@
|
||||
|
||||
@include "arch.vc6asm"
|
||||
|
||||
@macro nor 2
|
||||
or %0, %1
|
||||
not %0
|
||||
@end
|
||||
|
||||
@macro xnor 2
|
||||
xor %0, %1
|
||||
not %0
|
||||
@end
|
||||
|
||||
@macro nand 2
|
||||
and %0, %1
|
||||
not %0
|
||||
@end
|
||||
|
||||
@macro clf 0
|
||||
xor fl, fl
|
||||
@end
|
||||
|
||||
@macro maskeq 0
|
||||
; xxp
|
||||
and fl, FL_EQ ; 00p
|
||||
@end
|
||||
|
||||
@macro masklt 0
|
||||
; xpx
|
||||
and fl, FL_LT ; 0p0
|
||||
@end
|
||||
|
||||
|
||||
@macro maskgt 0
|
||||
; xpp
|
||||
and fl, FL_EQ | FL_LT ; 0pp
|
||||
cmp fl, 0 ; xxp
|
||||
and fl, FL_EQ ; 00p
|
||||
@end
|
||||
|
||||
@macro maskne 0
|
||||
maskeq ; 00p
|
||||
not fl ; 11q
|
||||
and fl, FL_EQ ; 00q
|
||||
@end
|
||||
|
||||
@macro maskgte 0
|
||||
masklt ; 0p0
|
||||
not fl ; 1q1
|
||||
and fl, FL_LT ; 0q0
|
||||
@end
|
||||
|
||||
@macro masklte 0
|
||||
maskgt ; 00p
|
||||
not fl ; 11q
|
||||
and fl, FL_EQ ; 00q
|
||||
@end
|
||||
|
||||
@macro push 1
|
||||
sub sp, 1
|
||||
store [sp], %0
|
||||
@ -55,18 +106,52 @@
|
||||
add ip, (%0) - $ - 2
|
||||
@end
|
||||
|
||||
@macro jmp 1
|
||||
mov ip, %0
|
||||
@end
|
||||
|
||||
@macro jeq 1
|
||||
maskeq
|
||||
cmov ip, %0
|
||||
@end
|
||||
|
||||
@macro jne 1
|
||||
maskne
|
||||
cmov ip, %0
|
||||
@end
|
||||
|
||||
@macro jlt 1
|
||||
masklt
|
||||
cmov ip, %0
|
||||
@end
|
||||
|
||||
@macro jlte 1
|
||||
masklte
|
||||
cmov ip, %0
|
||||
@end
|
||||
|
||||
@macro jgt 1
|
||||
maskgt
|
||||
cmov ip, %0
|
||||
@end
|
||||
|
||||
@macro jgte 1
|
||||
maskgte
|
||||
cmov ip, %0
|
||||
@end
|
||||
|
||||
@macro lea 3
|
||||
mov %0, %1
|
||||
add %0, %2
|
||||
@end
|
||||
|
||||
@macro ldea 3
|
||||
@macro ldo 3
|
||||
mov %0, %1
|
||||
add %0, %2
|
||||
load %0, [%0]
|
||||
@end
|
||||
|
||||
@macro stea 4
|
||||
@macro sto 4
|
||||
lea %0, %2, %3
|
||||
store [%0], %1
|
||||
@end
|
||||
|
||||
@ -47,12 +47,12 @@ println:
|
||||
sub sp, 2
|
||||
|
||||
store [sp], r1
|
||||
stea r0, r2, sp, 1
|
||||
sto r0, r2, sp, 1
|
||||
|
||||
mov r1, 0
|
||||
jr .l0_cond
|
||||
.l0_body:
|
||||
ldea r3, sp, 1
|
||||
ldo r3, sp, 1
|
||||
add r3, r1
|
||||
load r2, [sp]
|
||||
add r2, r1
|
||||
|
||||
@ -23,9 +23,8 @@ shl reg, reg/imm
|
||||
rshr reg, reg/imm
|
||||
rshl reg, reg/imm
|
||||
|
||||
adc reg, reg/imm
|
||||
sbb reg, reg/imm
|
||||
rsbb reg, reg/imm
|
||||
add reg, reg/imm
|
||||
sub reg, reg/imm
|
||||
|
||||
# registers
|
||||
|
||||
|
||||
47
src/asm.c
47
src/asm.c
@ -633,8 +633,7 @@ typedef struct {
|
||||
size_t output_size;
|
||||
size_t ip;
|
||||
Map syms;
|
||||
int labels_counter;
|
||||
int current_label;
|
||||
char *current_label;
|
||||
ARRAY_FIELD(IncludeExpansion, incl_expans);
|
||||
ARRAY_FIELD(Macro, macros);
|
||||
Map macro_map;
|
||||
@ -662,8 +661,7 @@ void assembler_init(Assembler *a, Lexer *lexer)
|
||||
.output_size = output_size,
|
||||
.ip = 0,
|
||||
.syms = { },
|
||||
.labels_counter = 0,
|
||||
.current_label = 0,
|
||||
.current_label = nullptr,
|
||||
ARRAY_FIELD_INIT(incl_expans),
|
||||
ARRAY_FIELD_INIT(macros),
|
||||
.macro_map = { },
|
||||
@ -696,8 +694,9 @@ void assembler_rewind(Assembler *a)
|
||||
|
||||
a->tok = lexer_next(a->lexer);
|
||||
a->ip = 0;
|
||||
a->labels_counter = 0;
|
||||
a->current_label = 0;
|
||||
if (a->current_label)
|
||||
free(a->current_label);
|
||||
a->current_label = nullptr;
|
||||
a->expan_ids = 0;
|
||||
a->org_offset = 0;
|
||||
a->second_pass = true;
|
||||
@ -709,6 +708,8 @@ void assembler_destroy(Assembler *a)
|
||||
{
|
||||
free(a->output);
|
||||
map_destroy(&a->syms);
|
||||
if (a->current_label)
|
||||
free(a->current_label);
|
||||
for (size_t i = 0; i < a->incl_expans_size; ++i) {
|
||||
free(a->incl_expans[i].filename);
|
||||
free(a->incl_expans[i].text);
|
||||
@ -912,11 +913,29 @@ static int64_t a_parse_operand(Assembler *a)
|
||||
if (a->tok.ty == Tok_Ident) {
|
||||
Tok ident_tok = a->tok;
|
||||
a_step(a);
|
||||
if (!a->second_pass)
|
||||
return 0;
|
||||
|
||||
char *ident = strndup(ident_tok.text, ident_tok.len);
|
||||
|
||||
if (a->tok.ty == '.') {
|
||||
a_step(a);
|
||||
if (a->tok.ty != Tok_Ident)
|
||||
print_error_exit(a->tok.loc, "expected identifier");
|
||||
Tok local_ident_tok = a->tok;
|
||||
a_step(a);
|
||||
char *local_ident
|
||||
= strndup(local_ident_tok.text, local_ident_tok.len);
|
||||
|
||||
char *combined = alloc_sprintf("%s.%s", ident, local_ident);
|
||||
free(ident);
|
||||
free(local_ident);
|
||||
ident = combined;
|
||||
}
|
||||
|
||||
if (!a->second_pass) {
|
||||
free(ident);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Sym *v = map_get(&a->syms, ident);
|
||||
if (!v)
|
||||
print_error_exit(ident_tok.loc, "undefined identifier '%s'", ident);
|
||||
@ -933,7 +952,7 @@ static int64_t a_parse_operand(Assembler *a)
|
||||
return 0;
|
||||
|
||||
char *ident = strndup(ident_tok.text, ident_tok.len);
|
||||
char *unique_ident = alloc_sprintf(".%s_%d", ident, a->current_label);
|
||||
char *unique_ident = alloc_sprintf("%s.%s", a->current_label, ident);
|
||||
|
||||
Sym *v = map_get(&a->syms, unique_ident);
|
||||
if (!v)
|
||||
@ -1228,8 +1247,6 @@ static void a_parse_directive_assert(Assembler *a)
|
||||
int64_t cond = a_parse_expr(a);
|
||||
a_assert_eol(a);
|
||||
|
||||
printf("cond = %ld\n", cond);
|
||||
|
||||
if (!cond && a->second_pass)
|
||||
print_error_exit(cond_loc, "assertion failed");
|
||||
}
|
||||
@ -1556,10 +1573,10 @@ static void a_parse_label_or_ins(Assembler *a)
|
||||
|
||||
char *ident = strndup(ident_tok.text, ident_tok.len);
|
||||
a_sym_define(a, ident, ident_tok.loc, a->ip + a->org_offset);
|
||||
free(ident);
|
||||
|
||||
a->labels_counter += 1;
|
||||
a->current_label = a->labels_counter;
|
||||
if (a->current_label)
|
||||
free(a->current_label);
|
||||
a->current_label = ident;
|
||||
|
||||
assembler_parse_line(a);
|
||||
return;
|
||||
@ -1582,7 +1599,7 @@ static void a_parse_local_label(Assembler *a)
|
||||
a_step(a);
|
||||
|
||||
char *ident = strndup(ident_tok.text, ident_tok.len);
|
||||
char *unique_ident = alloc_sprintf(".%s_%d", ident, a->current_label);
|
||||
char *unique_ident = alloc_sprintf("%s.%s", a->current_label, ident);
|
||||
|
||||
a_sym_define(a, unique_ident, ident_tok.loc, a->ip + a->org_offset);
|
||||
free(ident);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user