Skip to content

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.

train: keep the last 4096 prompt bytes in gpt_sample, not the first 4096

Leo Camusd193f9825096
diff --git a/03-minigpt-train/model.asm b/03-minigpt-train/model.asm
index bd1522e..56378fe 100644
--- a/03-minigpt-train/model.asm
+++ b/03-minigpt-train/model.asm
@@ -1527,7 +1527,7 @@ gpt_sample:
vmovss [rsp + 20], xmm1
lea rbx, [rel g_trn]
- lea r12, [rel sample_tok_buf] ; uint32[4096]
+ lea r12, [rel sample_tok_buf] ; uint32[8192], window of 4096
xor r13d, r13d ; tok_len = 0
; Encode prompt_cstr (supporting literal "\n" -> 0x0A) and echo to stdout
@@ -1563,13 +1563,30 @@ gpt_sample:
; Map byte -> token ID via byte_to_id[byte]
BYTE_TO_TOKEN eax, r15
- cmp r13, 4096
- jae .p_lp
+ ; Keep the last 4096 prompt tokens: when the 8192 buffer is full, move
+ ; its last 4096 tokens to the front in one pass instead of per byte
+ cmp r13, 8192
+ jb .p_store
+ lea rsi, [r12 + 4096*4]
+ mov rdi, r12
+ mov ecx, 4096
+ rep movsd
+ mov r13d, 4096
+.p_store:
mov [r12 + r13*4], eax
inc r13
jmp .p_lp
.chk_empty:
+ ; Move the last 4096 prompt tokens to the front for the generation loop
+ cmp r13, 4096
+ jbe .p_fits
+ lea rsi, [r12 + r13*4 - 4096*4]
+ mov rdi, r12
+ mov ecx, 4096
+ rep movsd
+ mov r13d, 4096
+.p_fits:
test r13, r13
jnz .gen_start
mov dword [r12], 0 ; seed with token 0 ('\n')
@@ -1687,4 +1704,4 @@ alignb 64
g_trn: resb TRN_SIZE
alignb 64
-sample_tok_buf: resd 4096
+sample_tok_buf: resd 8192