Qwen3-0.6B inference (12 KB), bare-metal bootable OS (22 KB), and from-scratch Mini-GPT trainer with live Braille TUI (32 KB) in 100% pure x86-64 NASM assembly. Zero C, zero libc, zero dependencies.
os: add /more to continue a reply that was cut at max_tokens
sanbo9b841794c21b
diff --git a/02-baremetal-os/README.md b/02-baremetal-os/README.mdindex 16f189e..9405735 100644--- a/02-baremetal-os/README.md+++ b/02-baremetal-os/README.md@@ -154,6 +154,7 @@ Both PS/2 keyboard (VGA) and COM1 serial (`115200 8N1`) are polled simultaneousl| Command | Action ||---|---|| `/clear`, `/new`, `/reset` | Clear conversation history (`g_chat_pos = 0`, prints `ctx=0/2048`) |+| `/more` | Continue a reply that was cut at `max_tokens` (the cut leaves the assistant turn open; any other input closes it first). Prints a hint when a reply is cut; "nothing to continue" otherwise || `/think` | Enable Qwen3 `<think>...</think>` reasoning mode || `/nothink` | Disable reasoning mode (direct answers, default) || `/bench` | Reset KV-cache and run built-in latency/throughput benchmark prompt |diff --git a/02-baremetal-os/kernel.asm b/02-baremetal-os/kernel.asmindex 72b53a4..977d257 100644--- a/02-baremetal-os/kernel.asm+++ b/02-baremetal-os/kernel.asm@@ -390,7 +390,7 @@ kernel_main:lea rsi, [rel cmd_help]call str_eqtest eax, eax- jz .run_user_prompt+ jz .chk_moremov dil, VGA_ATTR_GREYcall vga_set_colormov edi, 1@@ -398,6 +398,35 @@ kernel_main:call write_cstrjmp .repl_loop+ ; /more - continue a reply that was cut at max_tokens+.chk_more:+ mov rdi, r12+ lea rsi, [rel cmd_more]+ call str_eq+ test eax, eax+ jz .run_user_prompt+ mov rax, [rel g_chat_pos]+ test rax, rax+ jz .more_none+ cmp rax, [rel g_trunc_pos]+ jne .more_none+ add rax, 8+ lea rdx, [rel g_ctx]+ cmp rax, [rdx + CTX_MAX_SEQ]+ jae .more_none+ mov dword [rel g_continue], 1+ xor edi, edi+ xor esi, esi+ call run_inference+ jmp .repl_loop+.more_none:+ mov dil, VGA_ATTR_YELLOW+ call vga_set_color+ mov edi, 1+ lea rsi, [rel msg_more_none]+ call write_cstr+ jmp .repl_loop+.run_user_prompt:mov rdi, r12xor esi, esi@@ -448,6 +477,46 @@ kernel_main:call tp_shutdowncall qemu_shutdown+; ============================================================================+; void close_trunc_turn(void)+; If the last reply was cut at max_tokens (KV still holds the open assistant turn),+; feed <|im_end|>\n so the next user turn lines up. No-op otherwise.+; Preserves rdi/rsi (run_inference's prompt pointer).+; ============================================================================+close_trunc_turn:+ push rbx+ push rdi ; run_inference keeps the user's prompt pointer in rdi+ push rsi ; (and rsi) across this call: preserve both+ mov rax, [rel g_chat_pos]+ test rax, rax+ jz .ct_ret+ cmp rax, [rel g_trunc_pos]+ jne .ct_ret+ mov qword [rel g_trunc_pos], 0+ cmp dword [rel opt_raw], 0 ; raw mode: upstream never writes <|im_end|>, so neither do we+ jne .ct_ret+ lea rcx, [rax + 2]+ lea rdx, [rel g_ctx]+ cmp rcx, [rdx + CTX_MAX_SEQ]+ jae .ct_ret+ mov rbx, rax+ mov edi, TOK_IM_END+ mov rsi, rbx+ xor edx, edx+ call transformer_forward+ inc rbx+ mov edi, TOK_NL+ mov rsi, rbx+ xor edx, edx+ call transformer_forward+ inc rbx+ mov [rel g_chat_pos], rbx+.ct_ret:+ pop rsi+ pop rdi+ pop rbx+ ret+; ============================================================================; void run_inference(const char *prompt in rdi, int show_boot_ttft in esi); Exact same Qwen3 BPE + prefill + streaming decode loop as Phase 1!@@ -464,6 +533,16 @@ run_inference:mov [rel cur_show_ttft], esilea rbp, [rel g_ctx]+ ; /more: keep decoding from the current KV state, no new prompt+ cmp dword [rel g_continue], 0+ je .not_continue+ mov dword [rel g_continue], 0+ xor r12d, r12d ; n_prompt = 0+ jmp .do_prefill+.not_continue:+ ; a previous reply cut at max_tokens is still open in the KV cache: close it now+ call close_trunc_turn+; Build prompt tokens via pure-asm BPE + Qwen3 chat templatemov rsi, [rbp + CTX_BUF_PROMPT]mov rdx, MAX_PROMPT_TOKENS@@ -600,15 +679,24 @@ run_inference:jne .reset_chat_after; If decode stopped at max_tokens, feed the last emitted token into KV-cache+ mov qword [rel g_trunc_pos], 0cmp dword [rsp], 0je .close_eotcmp r13, [rbp + CTX_MAX_SEQ]jae .save_chat_posmov rdi, r14mov rsi, r13- xor edx, edx ; compute_logits = 0+ mov edx, 1 ; compute_logits = 1 so /more can sample the next tokencall transformer_forwardinc r13+ ; Leave the assistant turn OPEN: /more continues it, any other input closes it first+ mov [rel g_trunc_pos], r13+ mov dil, VGA_ATTR_YELLOW+ call vga_set_color+ mov edi, 1+ lea rsi, [rel msg_cut_hint]+ call write_cstr+ jmp .save_chat_pos.close_eot:; Feed <|im_end|> (151645) and \n (198) so next turn's <|im_start|>user\n aligns exact@@ -1443,10 +1531,12 @@ ban_mod_lyr: db ", ", 0ban_mod_ctx: db "L, ctx=", 0msg_prompt_lbl: db "Prompt: ", 0-msg_repl_help: db "Interactive Bare-Metal Chat (/clear, /think, /nothink, /bench, /halt, /reboot, /help)", 10+msg_repl_help: db "Interactive Bare-Metal Chat (/more, /clear, /think, /nothink, /bench, /halt, /reboot, /help)", 10db "Ctrl-R or a serial BREAK reboots at any time, even mid-generation", 10, 0msg_repl_prompt: db 10, "asm-llm> ", 0msg_chat_cleared: db "[chat] history cleared (ctx=0/", 0+msg_cut_hint: db "[cut at max_tokens - type /more to continue this reply]", 10, 0+msg_more_none: db "[chat] nothing to continue (only a reply cut at max_tokens, as the last turn)", 10, 0msg_ctx_full: db "[chat] context full, resetting KV-cache", 10, 0msg_think_on: db "[mode] think=1 (Qwen3 reasoning enabled)", 10, 0msg_think_off: db "[mode] think=0 (direct answer mode)", 10, 0@@ -1484,6 +1574,7 @@ cmd_think: db "/think", 0cmd_nothink: db "/nothink", 0cmd_bench: db "/bench", 0cmd_help: db "/help", 0+cmd_more: db "/more", 0key_prompt: db "prompt", 0key_max_tokens: db "max_tokens", 0@@ -1521,6 +1612,8 @@ g_mod_end: resq 1g_mod_size: resq 1g_active_cores: resq 1g_chat_pos: resq 1+g_trunc_pos: resq 1+g_continue: resd 1opt_max_tok: resq 1opt_ctx_len: resq 1