added strlen, strncpy and realloc
[rrq/rrqforth.git] / wordlists.asm
index 44656e85fb81a22003e175b0019875cedd971cbc..7daa4289c964b501314d40dbb21f4609d92fc480 100644 (file)
@@ -38,27 +38,67 @@ p_words_END:
        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