println, gt

This commit is contained in:
sfja 2026-11-12 22:05:24 +01:00
parent 6a955679cd
commit 251d214f25
6 changed files with 173 additions and 50 deletions

View File

@ -1,17 +1,19 @@
@include "arch.vc6asm" @include "arch.vc6asm"
@include "macros.vc6asm" @include "macros.vc6asm"
@const STACK_BASE 0xfff
@const HEAP_BASE 0x1000
start: start:
mov bp, 0x1000 mov bp, STACK_BASE
mov sp, bp mov sp, bp
mov r1, .splash mov r1, .splash
mov r2, VCD_BEGIN + (0 * 40 + 0)
call println call println
mov r0, 512 mov r0, 512
mov r1, 1 mov r1, 1
jr .l0_cond jmp .l0_cond
.l0_body: .l0_body:
store [DISK_BLOCK_ID], r1 store [DISK_BLOCK_ID], r1
store [DISK_ADDR], r0 store [DISK_ADDR], r0
@ -20,48 +22,68 @@ start:
add r0, 512 add r0, 512
add r1, 1 add r1, 1
.l0_cond: .l0_cond:
lr r2, .l0_body
clf
cmp r1, BOOT_SIZE / 512 + 2 cmp r1, BOOT_SIZE / 512 + 2
and fl, FL_LT jlt .l0_body
cmov ip, r2
.spin: .spin:
hlt hlt
jr .spin jmp .spin
.splash: .splash:
dw "VC6 boot loader\0" dw "VC6 boot loader\n\0"
display:
.x: dw 0
.y: dw 0
println: println:
push bp push bp
mov bp, sp mov bp, sp
sub sp, 2 sub sp, 2
store [sp], r1 mov r0, r1
stea r0, r2, sp, 1 jmp .l0_cond
mov r1, 0
jr .l0_cond
.l0_body: .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 add r3, r1
load r2, [sp] add r3, VCD_BEGIN
add r2, r1
load r2, [r2]
store [r3], r2 store [r3], r2
add r1, 1 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: .l0_cond:
load r2, [sp] load r1, [r0]
add r2, r1 cmp r1, '\0'
load r2, [r2] jgt .l0_body
clf
lr r3, .l0_body load r3, [display.y]
cmp r2, '\0' add r3, 40
not fl store [display.y], r3
and fl, FL_EQ
cmov ip, r3 mov r1, 0
store [display.x], r1
.leave: .leave:
mov sp, bp mov sp, bp

View File

@ -2,10 +2,61 @@
@include "arch.vc6asm" @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 @macro clf 0
xor fl, fl xor fl, fl
@end @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 @macro push 1
sub sp, 1 sub sp, 1
store [sp], %0 store [sp], %0
@ -55,18 +106,52 @@
add ip, (%0) - $ - 2 add ip, (%0) - $ - 2
@end @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 @macro lea 3
mov %0, %1 mov %0, %1
add %0, %2 add %0, %2
@end @end
@macro ldea 3 @macro ldo 3
mov %0, %1 mov %0, %1
add %0, %2 add %0, %2
load %0, [%0] load %0, [%0]
@end @end
@macro stea 4 @macro sto 4
lea %0, %2, %3 lea %0, %2, %3
store [%0], %1 store [%0], %1
@end @end

View File

@ -47,12 +47,12 @@ println:
sub sp, 2 sub sp, 2
store [sp], r1 store [sp], r1
stea r0, r2, sp, 1 sto r0, r2, sp, 1
mov r1, 0 mov r1, 0
jr .l0_cond jr .l0_cond
.l0_body: .l0_body:
ldea r3, sp, 1 ldo r3, sp, 1
add r3, r1 add r3, r1
load r2, [sp] load r2, [sp]
add r2, r1 add r2, r1

View File

@ -23,9 +23,8 @@ shl reg, reg/imm
rshr reg, reg/imm rshr reg, reg/imm
rshl reg, reg/imm rshl reg, reg/imm
adc reg, reg/imm add reg, reg/imm
sbb reg, reg/imm sub reg, reg/imm
rsbb reg, reg/imm
# registers # registers

View File

@ -633,8 +633,7 @@ typedef struct {
size_t output_size; size_t output_size;
size_t ip; size_t ip;
Map syms; Map syms;
int labels_counter; char *current_label;
int current_label;
ARRAY_FIELD(IncludeExpansion, incl_expans); ARRAY_FIELD(IncludeExpansion, incl_expans);
ARRAY_FIELD(Macro, macros); ARRAY_FIELD(Macro, macros);
Map macro_map; Map macro_map;
@ -662,8 +661,7 @@ void assembler_init(Assembler *a, Lexer *lexer)
.output_size = output_size, .output_size = output_size,
.ip = 0, .ip = 0,
.syms = { }, .syms = { },
.labels_counter = 0, .current_label = nullptr,
.current_label = 0,
ARRAY_FIELD_INIT(incl_expans), ARRAY_FIELD_INIT(incl_expans),
ARRAY_FIELD_INIT(macros), ARRAY_FIELD_INIT(macros),
.macro_map = { }, .macro_map = { },
@ -696,8 +694,9 @@ void assembler_rewind(Assembler *a)
a->tok = lexer_next(a->lexer); a->tok = lexer_next(a->lexer);
a->ip = 0; a->ip = 0;
a->labels_counter = 0; if (a->current_label)
a->current_label = 0; free(a->current_label);
a->current_label = nullptr;
a->expan_ids = 0; a->expan_ids = 0;
a->org_offset = 0; a->org_offset = 0;
a->second_pass = true; a->second_pass = true;
@ -709,6 +708,8 @@ void assembler_destroy(Assembler *a)
{ {
free(a->output); free(a->output);
map_destroy(&a->syms); map_destroy(&a->syms);
if (a->current_label)
free(a->current_label);
for (size_t i = 0; i < a->incl_expans_size; ++i) { for (size_t i = 0; i < a->incl_expans_size; ++i) {
free(a->incl_expans[i].filename); free(a->incl_expans[i].filename);
free(a->incl_expans[i].text); free(a->incl_expans[i].text);
@ -912,11 +913,29 @@ static int64_t a_parse_operand(Assembler *a)
if (a->tok.ty == Tok_Ident) { if (a->tok.ty == Tok_Ident) {
Tok ident_tok = a->tok; Tok ident_tok = a->tok;
a_step(a); a_step(a);
if (!a->second_pass)
return 0;
char *ident = strndup(ident_tok.text, ident_tok.len); 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); Sym *v = map_get(&a->syms, ident);
if (!v) if (!v)
print_error_exit(ident_tok.loc, "undefined identifier '%s'", ident); print_error_exit(ident_tok.loc, "undefined identifier '%s'", ident);
@ -933,7 +952,7 @@ static int64_t a_parse_operand(Assembler *a)
return 0; return 0;
char *ident = strndup(ident_tok.text, ident_tok.len); 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); Sym *v = map_get(&a->syms, unique_ident);
if (!v) if (!v)
@ -1228,8 +1247,6 @@ static void a_parse_directive_assert(Assembler *a)
int64_t cond = a_parse_expr(a); int64_t cond = a_parse_expr(a);
a_assert_eol(a); a_assert_eol(a);
printf("cond = %ld\n", cond);
if (!cond && a->second_pass) if (!cond && a->second_pass)
print_error_exit(cond_loc, "assertion failed"); 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); char *ident = strndup(ident_tok.text, ident_tok.len);
a_sym_define(a, ident, ident_tok.loc, a->ip + a->org_offset); a_sym_define(a, ident, ident_tok.loc, a->ip + a->org_offset);
free(ident);
a->labels_counter += 1; if (a->current_label)
a->current_label = a->labels_counter; free(a->current_label);
a->current_label = ident;
assembler_parse_line(a); assembler_parse_line(a);
return; return;
@ -1582,7 +1599,7 @@ static void a_parse_local_label(Assembler *a)
a_step(a); a_step(a);
char *ident = strndup(ident_tok.text, ident_tok.len); 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); a_sym_define(a, unique_ident, ident_tok.loc, a->ip + a->org_offset);
free(ident); free(ident);

View File

@ -490,7 +490,7 @@ int main(int argc, char *argv[])
case SDL_EVENT_QUIT: case SDL_EVENT_QUIT:
goto stop_running; goto stop_running;
case SDL_EVENT_KEY_DOWN: case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP: // case SDL_EVENT_KEY_UP:
if (event.key.key == SDLK_ESCAPE) if (event.key.key == SDLK_ESCAPE)
goto stop_running; goto stop_running;
vm_handle_keyboard_event(&vm, vm_handle_keyboard_event(&vm,