X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=jonesforth.S;h=d0e933ea1ff94a3e8174e1011fcf8b7be9839498;hb=456b467a3ab7761e4f105305bf110c7c1abfb31b;hp=82de8dc0b41dea5dea543956937ba3a7a82cf8f2;hpb=7e551bcfa63742c26ea1e72ace4c459899412b4c;p=rrq%2Fjonesforth.git diff --git a/jonesforth.S b/jonesforth.S index 82de8dc..d0e933e 100644 --- a/jonesforth.S +++ b/jonesforth.S @@ -1,11 +1,11 @@ /* A sometimes minimal FORTH compiler and tutorial for Linux / i386 systems. -*- asm -*- By Richard W.M. Jones http://annexia.org/forth This is PUBLIC DOMAIN (see public domain release statement below). - $Id: jonesforth.S,v 1.34 2007-09-26 22:50:19 rich Exp $ + $Id: jonesforth.S,v 1.39 2007-09-29 16:05:10 rich Exp $ gcc -m32 -nostdlib -static -Wl,-Ttext,0 -o jonesforth jonesforth.S */ - .set JONES_VERSION,33 + .set JONES_VERSION,39 /* INTRODUCTION ---------------------------------------------------------------------- @@ -757,6 +757,14 @@ code_\label : // assembler code follows push %ecx NEXT + defcode "?DUP",4,,QDUP // duplicate top of stack if non-zero + pop %eax + test %eax,%eax + jz 1f + push %eax +1: push %eax + NEXT + defcode "1+",2,,INCR incl (%esp) // increment top of stack NEXT @@ -792,7 +800,8 @@ code_\label : // assembler code follows /* In this FORTH, only /MOD is primitive. Later we will define the / and MOD words in - terms of the primitive /MOD. + terms of the primitive /MOD. The design of the i386 assembly instruction idiv which + leaves both quotient and remainder makes this obvious choice. */ defcode "/MOD",4,,DIVMOD @@ -1283,7 +1292,7 @@ _EMIT: What it does in detail is that it first skips any blanks (spaces, tabs, newlines and so on). Then it calls KEY to read characters into an internal buffer until it hits a blank. Then it calculates the length of the word it read and returns the address and the length as - two words on the stack (with address at the top). + two words on the stack (with the length at the top of stack). Notice that WORD has a single internal buffer which it overwrites each time (rather like a static C string). Also notice that WORD's internal buffer is just 32 bytes long and @@ -1306,8 +1315,8 @@ _EMIT: defcode "WORD",4,,WORD call _WORD - push %ecx // push length push %edi // push base address + push %ecx // push length NEXT _WORD: @@ -1400,10 +1409,10 @@ _SNUMBER: */ defcode "FIND",4,,FIND - pop %edi // %edi = address pop %ecx // %ecx = length + pop %edi // %edi = address call _FIND - push %eax + push %eax // %eax = address of dictionary entry (or NULL) NEXT _FIND: @@ -1923,25 +1932,28 @@ _COMMA: NEXT /* - PRINTING STRINGS ---------------------------------------------------------------------- + LITERAL STRINGS ---------------------------------------------------------------------- + + LITSTRING is a primitive used to implement the ." and S" operators (which are written in + FORTH). See the definition of those operators later. - LITSTRING and EMITSTRING are primitives used to implement the ." and S" operators - (which are written in FORTH). See the definition of those operators below. + TELL just prints a string. It's more efficient to define this in assembly because we + can make it a single Linux syscall. */ defcode "LITSTRING",9,,LITSTRING lodsl // get the length of the string - push %eax // push it on the stack push %esi // push the address of the start of the string + push %eax // push it on the stack addl %eax,%esi // skip past the string addl $3,%esi // but round up to next 4 byte boundary andl $~3,%esi NEXT - defcode "EMITSTRING",10,,EMITSTRING + defcode "TELL",4,,TELL mov $1,%ebx // 1st param: stdout - pop %ecx // 2nd param: address of string pop %edx // 3rd param: length of string + pop %ecx // 2nd param: address of string mov $__NR_write,%eax // write syscall int $0x80 NEXT