@@ 78,6 78,8 @@ global _start
%endmacro
%macro pushs4 1
+ dup
+ mov tos4, %1
%endmacro
@@ 214,8 216,7 @@ section .text
; push an address constant
%macro aconst 1
fcreate %1
- dup
- mov tos4, %1
+ pushs4 %1
ret
%endmacro
@@ 281,7 282,7 @@ fcreate syscall3
; exit fox, param: exit code
%macro exit 1
pushs %1 ; param1: return code
- pushs 60 ; #syscall exit
+ pushs4 60 ; #syscall exit
tail syscall1
%endmacro
@@ 290,12 291,12 @@ fcreate bye
; ( size buf fd -- size|error )
fcreate read
- pushs 0
+ pushs4 0
tail syscall3
; ( size buf fd -- size|error )
fcreate write
- pushs 1
+ pushs4 1
tail syscall3
section .rodata
@@ 303,75 304,87 @@ section .rodata
; lendigits db $ - digits
section .text
+; r9: base to convert to
+; r10: address of digits string, used in finddigit
; al: char
; return: zf=1: found, else not found
-; results in rcx, rdi are used by caller
+; result in rdi
finddigit:
- lea rdi, [digits]
- mov rcx, [base]
+ mov rdi, r10
+ mov rcx, r9
inc rcx
repne scasb
ret
; in: eax(al): char [byte]
+; r10: address of digits string, used in finddigit
; out: zf==1 => no, zf==0 => yes
-; if digit, then eax holds the digit, otherwise eax is not defined
+; if digit, then rsi holds the digit
; destroys: rcx, rdi
digit?:
call finddigit
; compute digit
dec rdi ; rdi points to the digit that was found
- sub rdi, digits
- mov tos, rdi
+ sub rdi, r10
; set zf again, depending on outcome if the search
test rcx, rcx
ret
+; rax: current char to convert, used in finddigit
+; rbx: address of current char, init to end of string
+; rcx: counter, used in finddigit
+; rdx: start of string data
+; rbp: result number
+; rdi: used in digit?, returns digit
+; r8: current power to multiply current digit with
+; r9: base to convert to
+; r10: address of digits string, used in finddigit
-; ( saddr -- 0|[-1 n])
-; todo: negative numbers
+; ( *str -- 0|[n -1])
fcreate number
- mpush rcx, rbp, rbx, rdx
-
- ; init base^n value with base^0
- mov rcx, 1
- ; sign at front?
- mov bpl, [tos+ptrsize]
- cmp bpl, '-'
- jnz .positive
+ mpush rbx, rcx, rdx, rbp, rdi
+ push r8
+ push r9
+ push r10
-.positive:
- ; load len
- mov rbp, [tos]
- ; current char
- ; skip len, ptrsize-1 only because ebp has len, which
- ; is one more then we have to add to get go the end
- ; of the word
- lea rbx, [tos+ptrsize-1]
- ; rdx will save the result
- xor rdx, rdx
+ mov r10, digits
+ mov r9, [base]
+ mov r8, 1
+ xor rbp, rbp
+ lea rdx, [tos+ptrsize]
+ mov tos, [tos] ; fetch len of string
+ ; set rbx to the last char, so -1, otherwise we would be
+ ; at the first byte after the string
+ lea rbx, [rdx+tos-1]
+ movzx tos, byte [rdx]
+ dec rdx ; set rdx one byte *before* the string data as exit condition
+ cmp tos, '-'
+ jnz .nextchar
+ ; negative number
+ mov r8, -1
+ inc rdx
.nextchar:
- ; start at last char
- movzx tos, byte [rbx+rbp]
- mpush rdi, rcx
+ movzx tos, byte [rbx]
call digit?
- mpop
jz .nan
- ; digit is in tos
- mov rdi, tos
- imul rdi, rcx
- add rdx, rdi
- imul rcx, [base]
- dec rbp
- jnz .nextchar
- mov tos, rdx
+ ; digit is in rdi
+ imul rdi, r8
+ add rbp, rdi
+ imul r8, r9
+ dec rbx
+ cmp rbx, rdx
+ jne .nextchar
+ mov tos, rbp
pushs -1
- jmp leave
+ jmp .done
.nan:
xor tos, tos
-leave:
+.done:
+ pop r10
+ pop r9
+ pop r8
mpop
- ret
+ ret
; ( number base -- saddr )
; be aware: ".s" uses itoa, so it will overwrite the result in repl mode !
@@ 490,18 503,17 @@ isws:
; push stack pointer, for debugging
; ( -- n )
fcreate rsp
- dup
- mov tos, rsp
+ pushs rsp
ret
; ( -- [c])
fcreate getc
- pushs [bufcurr]
+ pushs4 [bufcurr]
.retry:
- cmp tos, [buflen]
+ cmp tos4, [buflen]
je .readmore
; lea tos, [buf] ; need this step because of rpi relative addressing
- movzx tos, byte [buf+tos]; fetch char from bufcurr
+ movzx tos4, byte [buf+tos]; fetch char from bufcurr
inc ptr [bufcurr]
ret
.readmore:
@@ 117,11 117,9 @@ m: % ^ / d08948 ,3 ; : % % ; ( mov ra
m: 0 ^ dup c031 ,2 ; ( xor eax, eax )
( alters flags )
-m: -1 ^ 0 #
-m: 1- c8ff48 ,3 ; : -1 -1 ; : 1- 1- ; ( dec rax )
+m: 1- c8ff48 ,3 ; : 1- 1- ; ( dec rax )
( alters flags )
-m: 1 ^ 0 #
m: 1+ c0ff48 ,3 ; : 1+ 1+ ; ( inc rax )
m: 2* e0d148 ,3 ; : 2* 2* ; ( shl rax, 1 )
m: 2/ f8d148 ,3 ; : 2/ 2/ ; ( sar rax, 1 )