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.

os: word-wrap the VGA console instead of splitting words at the edge

Life-Buoy2aad268091ab
diff --git a/02-baremetal-os/drivers.asm b/02-baremetal-os/drivers.asm
index 56251a6..d9efdfb 100644
--- a/02-baremetal-os/drivers.asm
+++ b/02-baremetal-os/drivers.asm
@@ -173,6 +173,104 @@ vga_scroll:
pop rdi
ret
+; Internal: move the text cursor to column 0 of the next row (scrolling if
+; needed). Used by the word-wrap path.
+vga_next_line:
+ mov dword [rel vga_col], 0
+ mov eax, [rel vga_row]
+ inc eax
+ cmp eax, VGA_ROWS
+ jb .store
+ call vga_scroll
+ ret
+.store:
+ mov [rel vga_row], eax
+ ret
+
+; Internal: word-wrap helper, called just before drawing a non-space glyph in
+; the last usable column (VGA_COLS - 2). If a word is already in progress on
+; this row it is copied out, blanked and redrawn on the next row, so words are
+; never split across the right edge. Spaces still break lines normally, and a
+; word wider than the row falls back to a hard wrap. Preserves al.
+vga_word_wrap:
+ push rax
+ push rbx
+ push rcx
+ push rdx
+ push rsi
+ push rdi
+ push r8
+ push r9
+
+ mov r9d, [rel vga_col] ; column the incoming glyph would use
+ test r9d, r9d
+ jz .done
+ lea r8d, [r9d - 1] ; last column already drawn
+
+ mov eax, [rel vga_row]
+ imul eax, eax, VGA_COLS
+ mov rdi, VGA_MEM
+ lea rsi, [rdi + rax*2]
+ movzx edx, word [rsi + r8*2]
+ cmp dl, ' '
+ je .start_new_word
+
+ ; Walk back to the first character of the word in progress.
+ mov ecx, r8d
+.find_start:
+ test ecx, ecx
+ jz .done ; word fills the row: let it hard-wrap
+ dec ecx
+ movzx edx, word [rsi + rcx*2]
+ cmp dl, ' '
+ jne .find_start
+ inc ecx ; first column of the word
+
+ ; Copy the word out of video memory and blank it on the current row.
+ lea rdi, [rel vga_wrap_buf]
+ xor ebx, ebx
+.copy:
+ movzx edx, word [rsi + rcx*2]
+ mov [rdi + rbx], dl
+ mov dl, ' '
+ mov dh, [rel vga_attr]
+ mov [rsi + rcx*2], dx
+ inc ebx
+ inc ecx
+ cmp ecx, r9d
+ jb .copy
+
+ ; Drop to the next row and redraw the whole word there.
+ call vga_next_line
+ xor ecx, ecx
+.reemit:
+ cmp ecx, ebx
+ jae .done
+ movzx eax, byte [rdi + rcx]
+ push rcx
+ push rdi
+ call vga_emit_ascii
+ pop rdi
+ pop rcx
+ inc ecx
+ jmp .reemit
+
+.start_new_word:
+ ; Previous cell is a space, so the incoming glyph begins a new word.
+ ; Start that word on a fresh row instead of stranding one letter here.
+ call vga_next_line
+
+.done:
+ pop r9
+ pop r8
+ pop rdi
+ pop rsi
+ pop rdx
+ pop rcx
+ pop rbx
+ pop rax
+ ret
+
; Internal: emit a single ASCII byte (al) to VGA text buffer
vga_emit_ascii:
cmp al, 10 ; '\n'
@@ -190,6 +288,16 @@ vga_emit_ascii:
cmp al, 0x7E
ja .ret
+ ; Word wrap: if the next non-space glyph would land in the last usable
+ ; column, move the word it belongs to down as a whole instead of leaving
+ ; a fragment at the right edge ("crops" must not become "c" + "rops").
+ cmp al, ' '
+ je .no_word_wrap
+ cmp dword [rel vga_col], VGA_COLS - 2
+ jne .no_word_wrap
+ call vga_word_wrap
+.no_word_wrap:
+
mov ecx, [rel vga_row]
imul ecx, ecx, VGA_COLS
add ecx, [rel vga_col]
@@ -765,5 +873,6 @@ vga_col: resd 1
utf8_cp: resd 1
utf8_rem: resd 1
vga_attr: resb 1
+vga_wrap_buf: resb VGA_COLS
ps2_shift: resb 1
last_was_cr: resb 1