--- /dev/null
+// stdio.asm: WORD p_realloc,'REALLOC',fasm
+
+anchor:p_realloc[]
+
+=== Word: reALLOC
+
+....
+Data stack: ( a m n -- a )
+....
+
+"REALLOC" is a word that reallocates memory using mremap of address a
+of size m to be size n bytes and returns the lowest address of the
+allocated block.
+
+Note that this makes new page allocations for the process from the
+kernel, and the granularity is in pages, i.e. a multiple of 4 kb.
+
+The memory is reampped using the MREMAP_MAYMOVE flag,
+
+See also <<p_malloc,MALLOC>>
--- /dev/null
+// wordlists.asm: WORD p_strlen,'STRLEN',fasm
+
+anchor:p_strlen[]
+
+=== Word: STRLEN
+....
+Data stack: ( s -- n )
+....
+
+"STRLEN" is a function words that counts how many bytes there are from
+s to the first NUL byte and returns that count, n, not including the
+NUL byte.
--- /dev/null
+// wordlists.asm: WORD p_strncpy,'STRNCPY',fasm
+
+anchor:p_strncpy[]
+
+=== Word: STRNCPY
+....
+Data stack: ( s1 s2 n -- )
+....
+
+"STRNCPY" is a function words that copies n bytes of byte sequence s1
+to s2.
= RRQFORTH Reference Documentation
:author: Ralph Ronnquist <ralph.ronnquist@gmail.com>
-== Compilation words
+== Compilation Words
// include::compile.adoc[]
include::adoc/p_allot.adoc[]
include::separator.adoc[]
include::separator.adoc[]
include::adoc/p_this_word.adoc[]
+== Execution Control Words
//include::control.adoc[]
include::adoc/p_0branch.adoc[]
include::separator.adoc[]
include::separator.adoc[]
include::adoc/p_then.adoc[]
-== Logic operation words
+== Logic Operation Words
//include::logic.adoc[]
include::adoc/p_0equal.adoc[]
include::separator.adoc[]
include::separator.adoc[]
include::adoc/p_xor.adoc[]
-== Math operation words
+== Math Operation Words
// include::math.adoc[]
include::adoc/p_abs.adoc[]
include::separator.adoc[]
include::separator.adoc[]
include::adoc/p_plus.adoc[]
-== RRQFORTH main words
+== RRQFORTH Main Words
//include::rrqforth.adoc[]
include::adoc/data_stack.adoc[]
include::separator.adoc[]
include::adoc/inline_code.adoc[]
include::separator.adoc[]
-include::adoc/p_0branch.adoc[]
-include::separator.adoc[]
include::adoc/p_args.adoc[]
include::separator.adoc[]
-include::adoc/p_branch.adoc[]
-include::separator.adoc[]
include::adoc/p_dodoes.adoc[]
include::separator.adoc[]
include::adoc/p_dofasm.adoc[]
include::separator.adoc[]
include::adoc/p_read_word.adoc[]
include::separator.adoc[]
+include::adoc/p_realloc.adoc[]
+include::separator.adoc[]
include::adoc/p_sp.adoc[]
include::separator.adoc[]
include::adoc/p_stream.adoc[]
include::separator.adoc[]
include::adoc/p_forth.adoc[]
include::separator.adoc[]
+include::adoc/p_strlen.adoc[]
+include::separator.adoc[]
include::adoc/p_strncmp.adoc[]
include::separator.adoc[]
+include::adoc/p_strncpy.adoc[]
+include::separator.adoc[]
include::adoc/p_words.adoc[]
== System calls
;;; Dynamic memory management. Allocated with MALLOC and released with
;;; MUNMAP (see below)
- ;; ( size -- addr )
- ;; Allocates memory (using brk)
WORD p_malloc,'MALLOC',fasm
+ ;; ( size -- addr )
+ ;; Allocates memory (using mmap)
pushr rsi ; pretend it's a FORTH word since it
; ends via sys_mmap_asm
pop rax
push qword 0 ; offset
jmp sys_mmap_asm ; exit via sys_mmap
+ WORD p_realloc,'REALLOC',fasm
+ ;; ( addr old new -- )
+ ;; Try remapping a given MMAP region of old size to a new size
+ ;; mremap(void *old_address, size_t old_size,
+ ;; size_t new_size, int flags, ... /* void *new_address */);
+ pushr rsi
+ push 1 ; MREMAP_MAYMOVE
+ jmp sys_mmap_asm ; exit via sys_mmap
+
;;; ========================================
;;; Input stream handling.
;;;
popr rsi
next
+ WORD p_strlen,'STRLEN',fasm
+ ;; ( chars -- n )
+ ;; Determine length of NUL terminated byte sequence
+ pushr rsi
+ mov rsi,qword [rsp]
+ xor rcx,rcx
+ dec rcx
+ cld
+p_strlen_LOOP:
+ inc rcx
+ lodsb
+ cmp al,0
+ jne p_strlen_LOOP
+ mov qword [rsp],rcx
+ popr rsi
+ next
+
+ WORD p_strncpy,'STRNCPY',fasm
+ ;; ( chars1 chars2 n -- )
+ ;; Copy n bytes from chars1 to chars2.
+ pushr rsi
+ pop rcx
+ pop rdi
+ pop rsi
+ cmp rcx,0
+ jle p_strncpy_END
+ cld
+p_strncpy_LOOP:
+ movsb
+ dec rcx
+ jg p_strncpy_LOOP
+p_strncpy_END:
+ popr rsi
+ next
+
WORD p_strncmp,'STRNCMP',fasm
;; ( chars1 chars2 n -- flag )
;; Compare bytes until one is NUL, return <0, =0 or >0 to
;; indicate that chars1 is lesser, they are equal, or chars2
;; is lesser in ascii ordering respectively.
- pop rdx ; count
- pop rbx ; chars2
- pop rax ; chars1
- xor rcx,rcx
+ pushr rsi
+ pop rcx ; count
+ pop rsi ; chars2
+ pop rdi ; chars1
+ xor rax,rax
+ cmp rcx,0
+ jle p_strncmp_end
;; rax = chars1, rbx = chars2, cl = byte acc, rdx = length
+ cld
p_strncmp_loop:
- dec rdx
- jl p_strncmp_end
- mov cl,[rax]
- inc rax
- inc rbx
- sub cl,[rbx-1]
- je p_strncmp_loop
- jmp p_strncmp_end
+ cmpsb
+ jne p_strncmp_diff
+ dec rcx
+ jg p_strncmp_loop
+p_strncmp_diff:
+ xor rax,rax
+ mov al,[rsi-1]
+ sub al,[rdi-1]
p_strncmp_end:
- push rcx
+ push rax
+ popr rsi
next
WORD p_find,'FIND',fasm