Learning Forth (part 0): a minimal base

When developing software it is useful to build on top of existing abstractions, but a poorly made abstraction is frustrating to use, especially if it does not function as expected, whether due a bug or a misunderstanding. Having to debug complex abstractions is not nice. Designing and implementing a good abstraction is a difficult task.

With the increasing popularity of LLMs for generating code there are correspondingly more abstractions used where it only serves to make the code convoluted and harder to understand. LLMs are also increasingly able to find vulnerabilities in abstractions that are considered trusted, even in code generated by LLMs.

What if we did not rely on pre-made abstractions? There are some subcultures focused on extreme minimalism. Forth is notable among these as many Forth programmers develop their own interpreters and even hardware. Forth also allows defining abstractions very easily, making it one of the few languages where you can seamlessly transition between low- and high-level code.

There is a "standard" for Forth (ANS Forth), but we will not focus on it. Chuck Moore, the inventor of Forth, believes one should write their own Forth so we will be doing exactly that.

Writing a Forth interpreter

To get started we need to write a small interpreter. For ease's sake we shall use a Linux-based OS. We also need an assembler. FASM 1.73 has minimal dependencies and gives a great amount of control over the binary generation, making it a good choice for a minimal interpreter.

The current interpreter consists of 300 lines of assembly and compiles down to a 1050 byte executable.

Click to show source code
; vim:	syntax=asm-x86 softtabstop=16 tabstop=16 shiftwidth=16 noexpandtab

use64

; r15 = stack pointer
; r14 = stack top

elf_header:
db 7fh, "ELF", 2, 1, 1, 3
rb 8
dw 2, 3eh
dd 1
dq start, elf_program_headers, 0
dd 0
dw 64, 38h, 1, 40h, 0, 0
assert $ = 64

elf_program_headers:
dd 1, 7
dq segment_code, segment_code_virt, 0, segment_len, (1 shl 21) - (64 + 38h), 1 shl 12
assert $ - elf_program_headers = 38h

statbuf.st_size	equ	48
PROT_READ	equ	1
MAP_PRIVATE	equ	2
SYS_OPEN	equ	2
SYS_FSTAT	equ	5
SYS_MMAP	equ	9

virtual at 0
dict_entry::	.func	dq ?
	.flags	db ?
	.name_len	db ?
	.sizeof:
end virtual
MODE_COMPILE	equ	1
ENTRY_IMMEDIATE	equ	1

macro	udb
{	db	0d6h
}
macro	_syscall	nr
{	push	nr
	pop	rax
	syscall
}

segment_code:
org $ + (1 shl 21)
segment_code_virt:

start:	mov	r15, rsp
	sub	rsp, 1024
	mov	edi, path_start_fth
	xor	esi, esi
	_syscall	SYS_OPEN
	mov	edi, eax
.file_len:	mov	rsi, rsp
	_syscall	SYS_FSTAT
.file_mmap:	mov	rsi, [rsp + statbuf.st_size]
	mov	edx, PROT_READ
	mov	r10, MAP_PRIVATE
	mov	r8, rdi
	xor	r9, r9
	_syscall	SYS_MMAP
	test	rax, rax
	jns	@f
.fail:	udb
@@:	mov	[word_ptr], rax
	add	rax, rcx
	mov	[word_end], rax
parse_src:
.loop:	call	read_word
	cmp	rsi, [word_end]
	je	.exit
	mov	ecx, edi
	sub	ecx, esi
	call	lookup_word
	test	rdx, rdx
	jz	.number
	test	byte [mode_flags], MODE_COMPILE
	jz	.run_now
	test	byte [rdi + dict_entry.flags], ENTRY_IMMEDIATE
	jnz	.run_now
	mov	rdi, [code_head]
	mov	eax, 350o
	mov	[rdi + 0], al
	add	rdi, 5
	sub	edx, edi
	mov	[rdi - 4], edx
	mov	[code_head], rdi
	jmp	.loop
.run_now:	call	rdx
	jmp	.loop
.number:	call	parse_int
	test	byte [mode_flags], MODE_COMPILE
	jnz	@f
	call	w_dup
	mov	r14, rax
	jmp	.loop
@@:	mov	rdi, [code_head]
	mov	byte [rdi + 8], 276o
	mov	[rdi + 9], rax
	mov	rax, 4937894d08ef8349h
	stosq
	add	rdi, 9
	mov	[code_head], rdi
	jmp	.loop
.exit:	ret

w_define:	call	w_interp_end
	call	read_word
	mov	ecx, edi
	sub	ecx, esi
	mov	rdi, [dict_head]
	mov	eax, ecx
	rep movsb
	mov	[rdi + dict_entry.name_len], al
	mov	rax, [code_head]
	mov	[rdi + dict_entry.func], rax
	add	rdi, dict_entry.sizeof
	mov	[dict_head], rdi
	ret
w_immediate:	mov	rdi, [dict_head]
	or	byte [rdi - dict_entry.sizeof + dict_entry.flags], ENTRY_IMMEDIATE
	ret
w_end_define:	call	w_dup
	mov	r14b, 303o
	call	w_code_byte
w_interp_begin:	and	byte [mode_flags], not MODE_COMPILE
	ret
w_interp_end:	or	byte [mode_flags], MODE_COMPILE
	ret
w_code_byte:	mov	rdi, [code_head]
	mov	rax, r14
	stosb
	mov	r14, [r15]
	add	r15, 8
	mov	[code_head], rdi
	ret
w_runstate_ptr:	call	w_dup
	lea	r14, [runstate]
	ret
w_read_char:	call	w_dup
	mov	r14, -1
	mov	rdi, [word_ptr]
	cmp	rdi, [word_end]
	je	.ret
	movzx	r14, byte [rdi]
	inc	rdi
	mov	[word_ptr], rdi
.ret:	ret
w_dup:	add	r15, -8
	mov	[r15], r14
	ret
w_lookup_word:	mov	rsi, [r15]
	add	r15, 8
	mov	rcx, r14
	call	lookup_word
	mov	r14, rdi
	ret
w_read_word:	add	r15, -16
	mov	[r15 + 8], r14
	call	read_word
	mov	[r15], rsi
	sub	edi, esi
	mov	r14, rdi
	ret

; rsi = start, ecx = len
; rax = int
; sigill if invalid integer
parse_int:	mov	r8, rsi
	mov	r9, rcx
	cmp	byte [rsi], '-'
	jne	parse_uint
	inc	rsi
	dec	ecx
	call	parse_uint
	neg	rax
	ret
parse_uint:	movzx	eax, byte [rsi + rcx - 1]
	mov	ebx, 10
	mov	edx, 2
	cmp	eax, 'b'
	cmove	ebx, edx
	mov	edx, 8
	cmp	eax, 'o'
	cmove	ebx, edx
	mov	edx, 16
	cmp	eax, 'h'
	cmove	ebx, edx
	lea	edx, [ecx - 1]
	cmp	ebx, 10
	cmovne	ecx, edx
	xor	eax, eax
.loop:	mul	rbx
	movzx	edi, byte [rsi]
	inc	rsi
	or	edi, 40o
	lea	edx, [edi - '0']
	sub	edi, 'a' - 10
	cmova	edx, edi
	cmp	edx, ebx
	jae	.invalid
	add	rax, rdx
	dec	ecx
	jnz	.loop
	ret
.invalid:	push	1
	pop	rax
	mov	edi, eax
	mov	rsi, r8
	mov	rdx, r9
	syscall
	push	1
	pop	rax
	mov	edx, eax
	push	10
	mov	rsi, rsp
	syscall
	udb

; rsi = start, ecx = len
; rdx = address of function (0 if not found), rdi = pointer to entry (if valid)
lookup_word:	mov	rbx, [dict_head]
	sub	rbx, dict_entry.sizeof
	mov	rdx, [rbx]
.loop:	movzx	eax, byte [rbx + dict_entry.name_len]
	sub	rbx, rax
	cmp	eax, ecx
	jne	.next
	push	rsi
	push	rcx
	mov	rdi, rbx
	rep	cmpsb
	pop	rcx
	pop	rsi
	je	.end
.next:	sub	rbx, dict_entry.sizeof
	mov	rdx, [rbx]
	test	rdx, rdx
	jne	.loop
	xor	edi, edi
.end:	ret

; rsi = start, rdi = end
read_word:	mov	rdi, [word_ptr]
.start:	mov	rsi, rdi
.loop:	cmp	rdi, [word_end]
	je	.end
	movzx	eax, byte [rdi]
	cmp	al, ' '
	je	.delim
	cmp	al, 10
	je	.delim
	cmp	al, 9
	je	.delim
	inc	rdi
	jmp	.loop
.delim:	cmp	rdi, rsi
	jne	.end
	inc	rdi
	jmp	.start
.end:	mov	[word_ptr], rdi
	ret

path_start_fth	db "_start.fth", 0
mode_flags	db 0
runstate:
code_head	dq code
dict_head	dq dict
word_ptr	dq 0
word_end	dq 0

macro	e	name,is_imm,func
{	local	a, b
a:	db	name
b:	dq	func
	db	(is_imm * ENTRY_IMMEDIATE), b - a
	
}
	e	"",	0, 0
	e	"runstate",	0, w_runstate_ptr
	e	"parse-src",	0, parse_src
	e	":",	0, w_define
	e	"immediate",	0, w_immediate
	e	"[",	1, w_interp_begin
	e	"!C+",	0, w_code_byte
	e	"]",	0, w_interp_end
	e	";",	1, w_end_define
	e	"read-char",	0, w_read_char
	e	"read-word",	0, w_read_word
	e	"lookup-word",  0, w_lookup_word
dict:

segment_len = $ - segment_code_virt
rb 100000
align 4096
code:

Going through it:

elf_header:
db 7fh, "ELF", 2, 1, 1, 3
rb 8
dw 2, 3eh
dd 1
dq start, elf_program_headers, 0
dd 0
dw 64, 38h, 1, 40h, 0, 0
assert $ = 64

elf_program_headers:
dd 1, 7
dq segment_code, segment_code_virt, 0, segment_len, (1 shl 21) - (64 + 38h), 1 shl 12
assert $ - elf_program_headers = 38h

This section defines a ELF header with a single 2MiB RWX segment. Linux will map the page containing the binary at address 200000h and zero-fill the remaining pages.

statbuf.st_size	equ	48
statbuf.sizeof	equ	144
PROT_READ	equ	1
MAP_PRIVATE	equ	2
SYS_OPEN	equ	2
SYS_FSTAT	equ	5
SYS_MMAP	equ	9

Constant extracted by running small C programs.

virtual at 0
dict_entry::	.func	dq ?
	.flags	db ?
	.name_len	db ?
	.sizeof:
end virtual
MODE_COMPILE	equ	1
ENTRY_IMMEDIATE	equ	1

Constants used internally by the interpreter.

macro	udb
{	db	0d6h
}
macro	_syscall	nr
{	push	nr
	pop	rax
	syscall
}

Micro-optimizations to save a few bytes. udb was recently defined and is not recognized by FASM. It is only 1 byte whereas ud2 needs 2 bytes. push nr and pop rax is 2 bytes shorter than mov eax, nr.

segment_code:
org $ + (1 shl 21)
segment_code_virt:

Define the start of the code segment. The origin ($) starts at 0 by default, but we want to avoid mapping any pages at the null address so adjust the origin to 200000h.

start:	mov	r15, rsp
	sub	rsp, 1024
	mov	edi, path_start_fth
	xor	esi, esi
	_syscall	SYS_OPEN
.file_len:	mov	edi, eax
	mov	rsi, rsp
	_syscall	SYS_FSTAT
.file_mmap:	mov	rsi, [rsp + statbuf.st_size]
	mov	edx, PROT_READ
	mov	r10, MAP_PRIVATE
	mov	r8, rdi
	xor	r9, r9
	_syscall	SYS_MMAP
	test	rax, rax
	jns	@f
	udb
@@:	mov	[word_ptr], rax
	add	rax, rcx
	mov	[word_end], rax
parse_src:

Map in the _start.fth file for evaluation. If any system call fails, we just execute udb to trigger SIGILL. This may not seem user-friendly, but it is easy to determine what is happening by running the program under strace:

$ ./_start.bin
Illegal instruction
$ strace ./_start.bin
execve("./_start.bin", ["./_start.bin"], 0x7ffd2adb44b0 /* 51 vars */) = 0
open("_start.fth", O_RDONLY)            = -1 ENOENT (No such file or directory)
fstat(-2, 0x7ffe1c84a7c0)               = -1 EBADF (Bad file descriptor)
mmap(0xfffffffe, 0, PROT_READ, MAP_PRIVATE, -2, 0) = -1 EBADF (Bad file descriptor)
--- SIGILL {si_signo=SIGILL, si_code=ILL_ILLOPN, si_addr=0x2000b9} ---
+++ killed by SIGILL +++
Illegal instruction

Note that we abuse the fact that ENOENT is interpreted as an invalid file descriptor by the Linux kernel. This saves a few bytes on branches.

At the end we fall through straight to the main interpreter loop:

parse_src:
.loop:	call	read_word
	cmp	rsi, [word_end]
	je	.exit
	mov	ecx, edi
	sub	ecx, esi
	call	lookup_word
	test	rdx, rdx
	jz	.number
	test	byte [mode_flags], MODE_COMPILE
	jz	.run_now
	test	byte [rdi + dict_entry.flags], ENTRY_IMMEDIATE
	jnz	.run_now
	mov	rdi, [code_head]
	mov	eax, 350o
	mov	[rdi + 0], al
	add	rdi, 5
	sub	edx, edi
	mov	[rdi - 4], edx
	mov	[code_head], rdi
	jmp	.loop
.run_now:	call	rdx
	jmp	.loop
.number:	call	parse_int
	test	byte [mode_flags], MODE_COMPILE
	jnz	@f
	call	w_dup
	mov	r14, rax
	jmp	.loop
@@:	mov	rdi, [code_head]
	mov	byte [rdi + 8], 276o
	mov	[rdi + 9], rax
	mov	rax, 4937894d08ef8349h
	stosq
	add	rdi, 9
	mov	[code_head], rdi
	jmp	.loop
.exit:	ret

This loop is quite compact but performs a lot of work:

  1. We extract a word. If we are at the end of the source file we exit (the loop can be nested, more on that in a later article).
  2. We look for a definition of the word. If none is found, we attempt to parse it as a number.
  3. If we are not in compile mode or the word is immediate we evaluate the definition immediately. Otherwise, we insert a call to the definition.
  4. If the word is a number, we push it on the stack if we are not in compile mode. Otherwise, we insert it in the current definition.
350o <rel32> encodes the call <rel32> instruction. 4937894d08ef8349h and 276o is 3 instructions:
  1. 49h 83h 0efh 08h is sub r15, 8
  2. 4dh 89h 37h is mov [r15], r14
  3. 49b 276o <imm64> is mov r14, <imm64>

w_define:	call	w_interp_end
	call	read_word
	mov	ecx, edi
	sub	ecx, esi
	mov	rdi, [dict_head]
	mov	eax, ecx
	rep movsb
	mov	[rdi + dict_entry.name_len], al
	mov	rax, [code_head]
	mov	[rdi + dict_entry.func], rax
	add	rdi, dict_entry.sizeof
	mov	[dict_head], rdi
	ret
w_immediate:	mov	rdi, [dict_head]
	or	byte [rdi - dict_entry.sizeof + dict_entry.flags], ENTRY_IMMEDIATE
	ret
w_end_define:	call	w_dup
	mov	r14b, 303o
	call	w_code_byte
w_interp_begin:	and	byte [mode_flags], not MODE_COMPILE
	ret
w_interp_end:	or	byte [mode_flags], MODE_COMPILE
	ret
w_code_byte:	mov	rdi, [code_head]
	mov	rax, r14
	stosb
	mov	r14, [r15]
	add	r15, 8
	mov	[code_head], rdi
	ret
w_runstate_ptr:	call	w_dup
	lea	r14, [runstate]
	ret
w_read_char:	call	w_dup
	mov	r14, -1
	mov	rdi, [word_ptr]
	cmp	rdi, [word_end]
	je	.ret
	movzx	r14, byte [rdi]
	inc	rdi
	mov	[word_ptr], rdi
.ret:	ret
w_dup:	add	r15, -8
	mov	[r15], r14
	ret
w_lookup_word:	mov	rsi, [r15]
	add	r15, 8
	mov	rcx, r14
	call	lookup_word
	mov	r14, rdi
	ret
w_read_word:	add	r15, -16
	mov	[r15 + 8], r14
	call	read_word
	mov	[r15], rsi
	sub	edi, esi
	mov	r14, rdi
	ret

We define a number of words that we need for running programs and extending the interpreter:

; rsi = start, ecx = len
; rax = int
; sigill if invalid integer
parse_int:	mov	r8, rsi
	mov	r9, rcx
	cmp	byte [rsi], '-'
	jne	parse_uint
	inc	rsi
	dec	ecx
	call	parse_uint
	neg	rax
	ret
parse_uint:	movzx	eax, byte [rsi + rcx - 1]
	mov	ebx, 10
	mov	edx, 2
	cmp	eax, 'b'
	cmove	ebx, edx
	mov	edx, 8
	cmp	eax, 'o'
	cmove	ebx, edx
	mov	edx, 16
	cmp	eax, 'h'
	cmove	ebx, edx
	lea	edx, [ecx - 1]
	cmp	ebx, 10
	cmovne	ecx, edx
	xor	eax, eax
.loop:	mul	rbx
	movzx	edi, byte [rsi]
	inc	rsi
	or	edi, 40o
	lea	edx, [edi - '0']
	sub	edi, 'a' - 10
	cmova	edx, edi
	cmp	edx, ebx
	jae	.invalid
	add	rax, rdx
	dec	ecx
	jnz	.loop
	ret
.invalid:	push	1
	pop	rax
	mov	edi, eax
	mov	rsi, r8
	mov	rdx, r9
	syscall
	push	1
	pop	rax
	mov	edx, eax
	push	10
	mov	rsi, rsp
	syscall
	udb

The integer parsing routine is able to handle positive and negative numbers in decimal, binary, octal and hexadecimal format. It first checks for a sign, then checks for a suffix to determine whether the number is positive or negative. It also checks for invalid digits. If the number fails to parse, it prints the word and executes udb.

; rsi = start, ecx = len
; rdx = address of function (0 if not found), rdi = pointer to entry (if valid)
lookup_word:	mov	rbx, [dict_head]
	sub	rbx, dict_entry.sizeof
	mov	rdx, [rbx]
.loop:	movzx	eax, byte [rbx + dict_entry.name_len]
	sub	rbx, rax
	cmp	eax, ecx
	jne	.next
	push	rsi
	push	rcx
	mov	rdi, rbx
	rep	cmpsb
	pop	rcx
	pop	rsi
	je	.end
.next:	sub	rbx, dict_entry.sizeof
	mov	rdx, [rbx]
	test	rdx, rdx
	jne	.loop
	xor	edi, edi
.end:	ret

The lookup routine starts scanning from the end of the dictionary. By appending to the dictionary and scanning starting from the end towards the start we can redefine words while keeping pointers to previous definitions.

; rsi = start, rdi = end
read_word:	mov	rdi, [word_ptr]
.start:	mov	rsi, rdi
.loop:	cmp	rdi, [word_end]
	je	.end
	movzx	eax, byte [rdi]
	cmp	al, ' '
	je	.delim
	cmp	al, 10
	je	.delim
	cmp	al, 9
	je	.delim
	inc	rdi
	jmp	.loop
.delim:	cmp	rdi, rsi
	jne	.end
	inc	rdi
	jmp	.start
.end:	mov	[word_ptr], rdi
	ret

The read_word routine scans for a delimiter (space, newline or tab) or until it hits the end of the source code, then updates the current word pointer and returns a span with the current word.

path_start_fth	db "_start.fth", 0
mode_flags	db 0
runstate:
code_head	dq code
dict_head	dq dict
word_ptr	dq 0
word_end	dq 0

Various variables used by the interpreter.

macro	e	name,is_imm,func
{	local	a, b
a:	db	name
b:	dq	func
	db	(is_imm * ENTRY_IMMEDIATE), b - a
	
}
	e	"",	0, 0
	e	"runstate",	0, w_runstate_ptr
	e	"parse-src",	0, parse_src
	e	":",	0, w_define
	e	"immediate",	0, w_immediate
	e	"[",	1, w_interp_begin
	e	"!C+",	0, w_code_byte
	e	"]",	0, w_interp_end
	e	";",	1, w_end_define
	e	"read-char",	0, w_read_char
	e	"read-word",	0, w_read_word
	e	"lookup-word",  0, w_lookup_word
dict:

The initial dictionary. Note the dict label at the end of the dictionary. The dictionary is delimited by a entry with a zeroed function pointer.

segment_len = $ - segment_code_virt
rb 100000
align 4096
code:

Reserve 100KB for additional dictionary entries. There is a risk the dictionary flows over into the code space, but in practice this will only happen if you have many thousands of definitions, which is unusual for typical Forth programs.


To wrap this article up, here is a simple program that displays the current time in the CLI:

: ax  0 ;  : cx  1 ;  : dx  2 ;  : bx  3 ;
: sp  4 ;  : bp  5 ;  : si  6 ;  : di  7 ;
: r8  0 ;  : r9  1 ;  : r10 2 ;  : r11 3 ;
: r12 4 ;  : r13 5 ;  : r14 6 ;  : r15 7 ;

: rex.b  41h !C+ ;  : rex.x  42h !C+ ;  : rex.r  44h !C+ ;  : rex.w   48h !C+ ;
: rex.wb 49h !C+ ;  : rex.wr 4ch !C+ ;  : rex.rb 45h !C+ ;  : rex.wrb 4dh !C+ ;

: ret 303o !C+ ;  : int3 0cch !C+ ;  : udb 0d6h !C+ ;

: add_r15,i8 rex.wb 203o !C+ 307o !C+ !C+ ;
: add_r15,8   8 add_r15,i8 ;
: add_r15,-8 -8 add_r15,i8 ;

: or_r14,r15@ rex.wrb 013o !C+ 067o !C+ ;
: add_r14,r15@ rex.wrb 003o !C+ 067o !C+ ;
: and_r14,r15@ rex.wrb 043o !C+ 067o !C+ ;
: sub_r14,r15@ rex.wrb 053o !C+ 067o !C+ ;
: inc_r14 rex.wb 377o !C+ 306o !C+ ;
: dec_r14 rex.wb 377o !C+ 316o !C+ ;
: not_r14 rex.wb 367o !C+ 326o !C+ ;
: neg_r14 rex.wb 367o !C+ 336o !C+ ;

: or  [ or_r14,r15@  add_r15,8 ;
: and [ and_r14,r15@ add_r15,8 ;
: not [ not_r14 ;
: neg [ neg_r14 ;
: +   [ add_r14,r15@ add_r15,8 ;
: swap- [ sub_r14,r15@ add_r15,8 ;
: 1+ [ inc_r14 ;       : 1- [ dec_r14 ;

: mode_flags runstate -1 + ;
: code_head  runstate      ;

: syscall 0fh !C+ 05h !C+ ;
: rax>r14 rex.wb 211o !C+ 306o !C+ ;
: rdx>r14 rex.wb 211o !C+ 326o !C+ ;
:  si>r32 211o !C+ 360o or !C+ ;
: r14>rdx rex.wr dx si>r32 ;
: r14>rdi rex.wr di si>r32 ;
:  sp@>r14   rex.wr  213o !C+ 064o !C+ 044o !C+ ;
: r15@>rax   rex.wb  213o !C+ 007o !C+ ;
: r15@>rcx   rex.wb  213o !C+ 017o !C+ ;
: r15@>rsi   rex.wb  213o !C+ 067o !C+ ;
: r14@>r14   rex.wrb 213o !C+ 066o !C+ ;
: r15@>r14   rex.wrb 213o !C+ 067o !C+ ;
: r14b@>r14  rex.wrb 0fh  !C+ 266o !C+ 066o !C+ ;
: r15(i8)@>r32 213o !C+ 107o or !C+ !C+ ;
: r15(i8)@>rdx rex.wb  20o r15(i8)@>r32 ;
: r15(i8)@>r10 rex.wrb 20o r15(i8)@>r32 ;
: r15(i8)@>r14 rex.wrb 60o r15(i8)@>r32 ;
:  al>!r14 rex.b   210o !C+ 006o !C+ ;
: eax>!r14 rex.b   211o !C+ 006o !C+ ;
: rax>!r14 rex.wb  211o !C+ 006o !C+ ;
: rax>!r15 rex.wb  211o !C+ 007o !C+ ;
: rdx>!r15 rex.wb  211o !C+ 027o !C+ ;
: r14>!r15 rex.wrb 211o !C+ 067o !C+ ;
: r64>push 120o or !C+ ;
: pop>r64  130o or !C+ ;
: r14>push rex.b r14 r64>push ;
: pop>r14  rex.b r14 pop>r64 ;
: xor_eax,eax 061o !C+ 300o !C+ ;
: xor_edx,edx 061o !C+ 322o !C+ ;
: xor_r14,r15@ rex.wrb 063o !C+ 067o !C+ ;
: test_r14,r14 rex.wrb 205o !C+ 366o !C+ ;
: cmp_rax,rdx   rex.w   071o !C+ 320o !C+ ;
: cmp_r14,r15@ rex.wrb 073o !C+ 067o !C+ ;
: jnz_[i8] 165o !C+ ;  : jns_[i8] 171o !C+ ;  : jge_[i8] 175o !C+ ;
: cdq 99h !C+ ;  : cqo rex.w cdq ;
: idiv_r32 367o !C+ 370o or !C+ ;
: imul_r14,r15@ rex.wrb 017o !C+ 257o !C+ 067o !C+ ;
: idiv_r14 rex.wb r14 idiv_r32 ;
: shl_r14,cl rex.wb 323o !C+ 346o !C+ ;
: shr_r14,cl rex.wb 323o !C+ 356o !C+ ;
: sete_al 0fh !C+ 224o !C+ 300o !C+ ;

: 2drop [ add_r15,8 r15@>r14 add_r15,8 ;

: b@ [ r14b@>r14 ;
:  @ [ r14@>r14 ;
: b! [ r15@>rax  al>!r14 ] 2drop ;
: w! [ r15@>rax eax>!r14 ] 2drop ;
:  ! [ r15@>rax rax>!r14 ] 2drop ;

: C code_head @ ;  : >C code_head ! ;

: w!C+ C ! C 4 + >C ;
: d!C+ C ! C 8 + >C ;

: [iNN]>rNN 270o or !C+ ;
: u32>r32 [iNN]>rNN w!C+ ;
: u64>r14 rex.wb r14 [iNN]>rNN d!C+ ;

: {drop} r15@>r14 add_r15,8 ;
: {dup} add_r15,-8 r14>!r15 ;
: drop [ {drop} ;  : dup [ {dup} ;
: swap [ r15@>rax r14>!r15 rax>r14 ;
: over [ {dup} 8 r15(i8)@>r14 ;
: 2dup over over ;
: >r r14>push {drop} ; immediate
: r> {dup} pop>r14   ; immediate
: r@ {dup} sp@>r14   ; immediate

: swap-sll [ r15@>rcx add_r15,8 shl_r14,cl ;
: sll swap swap-sll ;
: - swap swap- ;
: xor [ xor_r14,r15@ add_r15,8 ;
: * [ imul_r14,r15@ add_r15,8 ;
: = [ xor_eax,eax cmp_r14,r15@ sete_al add_r15,8 rax>r14 ;

: stub-rel8 -2 !C+ C ;
: set-rel8 dup C swap- swap 1- b! ;
: if0  test_r14,r14 jnz_[i8] stub-rel8 ; immediate
: if-  test_r14,r14 jns_[i8] stub-rel8 ; immediate
: _pre r15@>rax r14>rdx 8 r15(i8)@>r14 16 add_r15,i8 cmp_rax,rdx ;
:  <if _pre jge_[i8] stub-rel8 ; immediate
: ;; ret set-rel8 ; immediate

: lit {dup} u64>r14 ;
: postpone; ret 0 mode_flags b! ;
: cst : lit postpone; ;

: _moddiv [ r15@>rax cqo idiv_r14 ;
: /mod _moddiv [ rax>!r15 rdx>r14 ;
: mod/ _moddiv [ rdx>!r15 rax>r14 ;
: mod mod/ drop ;  : / /mod drop ;
: /posmod >r if0 r> drop 0 ;; r@ /mod dup r@ xor if- drop r> + >r 1- r> ;; r> 2drop ;
: posmod/ /posmod swap ;

: p4 16 r15(i8)@>r10 [ : p3 8 r15(i8)@>rdx [ : p2 r15@>rsi [ : p1 r14>rdi ;
: s : ax u32>r32 ;  : t syscall rax>r14 add_r15,i8 postpone; ;
: s1 s p1  0 t ;  : s2 s p2  8 t ;  : s3 s p3 16 t ;  : s4 s p4 24 t ;
  1 s3 sys:write
  3 s1 sys:close
 60 s1 sys:exit
228 s2 sys:clock_gettime
230 s4 sys:clock_nanosleep

0 cst CLOCK_REALTIME  1 cst CLOCK_MONOTONIC

2 20 sll cst &A  &A 8 + cst &B
: A &A @ ;  : >A &A ! ;  : A+ A + >A ;  : b!A+ A b! 1 A+ ;
: B &B @ ;  : >B &B ! ;  : B+ B + >B ;

12o cst '\n'  15o cst '\r'  55o cst '-'  60o cst '0'  72o cst ':'

: systime &A CLOCK_REALTIME sys:clock_gettime drop A ;
: sleep >B >A 0 &A 0 CLOCK_MONOTONIC sys:clock_nanosleep drop ;
: print 1 sys:write drop ;

365 4 * 1+ cst 4Y>days
4Y>days 25 * 1- cst 100Y>days
100Y>days 4 * 1+ cst 400Y>days
365 366 + 4Y>days 7 * + neg cst 1970-2000>days
31 cst jan  28 cst feb  31 cst mar  30 cst apr  31 cst may  30 cst jun
31 cst jul  31 cst aug  30 cst sep  31 cst oct  30 cst nov
: leap A 400 mod 0 =  A 100 mod 0 = not and  A 3 and 0 = and ;
: m 2dup <if r> 2drop B ;; - 1 B+ ;
: month 0 >B jan m feb leap + m mar m apr m
  may m jun m jul m aug m sep m oct m nov m B ;
: y >r posmod/ r> * A+ ;
: cvt-time 60 posmod/ 60 posmod/ 24 posmod/
  1970-2000>days + 0 >A
  400Y>days 400 y 100Y>days 100 y 4Y>days 4 y 365 1 y
  A 2000 + >r month r> ;
: f '0' + b!A+ ;  : g 10 mod/ f f ;
: a '-' b!A+ 1+ g ;  : b ':' b!A+ g ;
: fmt cvt-time &B >A '\r' b!A+ 1000 mod/ f 100 mod/ f g a a 32 b!A+ g b b ;
: run systime fmt 32 b!A+ A &B - &B print 0 100000000 sleep r> drop run ;
run 0 sys:exit

To try it, save the assembly as _start.s and the Forth as _start.fth, then run fasm _start.s && chmod +x _start.bin && ./_start.bin