Annotation of gforth/contrib/syscalls386.fs, revision 1.1

1.1     ! anton       1: \ syscalls386.fs
        !             2: \
        !             3: \ Selected system calls for gforth 0.6.x on 386 machines running Linux.
        !             4: \   
        !             5: \ Copyright (c) 2004 Krishna Myneni,
        !             6: \ Provided under the GNU General Public License
        !             7: \
        !             8: \ Notes:
        !             9: \
        !            10: \ 1)   System calls under Linux may be performed using a software
        !            11: \        interrupt, $80, and placing the parameters in appropriate
        !            12: \        registers. The corresponding C wrapper functions are another
        !            13: \        way to do this, but the $80 int method is direct. The Forth
        !            14: \        stack parameters are chosen to correspond to the C wrapper 
        !            15: \        function argument list.
        !            16: \ 
        !            17: \ 2)   There are about 221 system calls under Linux, but this file
        !            18: \        provides only a select few. The provided syscalls allow 
        !            19: \        communication with device drivers, e.g. serial port drivers. 
        !            20: \        Add others as needed following the examples below and using 
        !            21: \        the man pages for the C wrapper functions. System call numbers 
        !            22: \        are listed in /usr/include/asm/unistd.h 
        !            23: \
        !            24: \ 3)   Compatibility with low-level kForth words is maintained to allow 
        !            25: \        kForth code to be used under gforth, e.g. serial.fs, terminal.fs.
        !            26: \        Other driver interface examples from kForth should also
        !            27: \        work, e.g. the National Instruments GPIB interface nigpib.4th.
        !            28: \
        !            29: \ 4)   The code should be readily adaptable to other Forths running
        !            30: \        on the same platform (386/Linux). It also demonstrates why
        !            31: \        an assembler can be an important component of a Forth system.
        !            32: \
        !            33: \ Revisions:
        !            34: \      2004-09-16  created  KM 
        !            35: 
        !            36: \ syscall0  ( syscall_num -- retval | system call with no args )
        !            37:  
        !            38: code syscall0
        !            39:        .d  di )  ax   mov
        !            40:        .d  $80 #      int
        !            41:        .d  ax    di ) mov
        !            42:        next
        !            43: end-code
        !            44: 
        !            45: \ syscall1  ( arg syscall_num -- retval | system call with one arg )
        !            46: 
        !            47: code syscall1
        !            48:        .d  di )  ax   mov
        !            49:        .d   4 #  di   add
        !            50:        .d  di )  bx   mov
        !            51:        .d  $80 #      int
        !            52:        .d  ax    di ) mov
        !            53:        next
        !            54: end-code
        !            55: 
        !            56: \ syscall2  ( arg1 arg2 syscall_num -- retval | system call with 2 args )
        !            57: 
        !            58: code syscall2
        !            59:        .d  di )  ax   mov
        !            60:        .d   4 #  di   add
        !            61:        .d  di )  cx   mov
        !            62:        .d   4 #  di   add
        !            63:        .d  di )  bx   mov
        !            64:        .d  $80 #      int
        !            65:        .d  ax    di ) mov
        !            66:        next
        !            67: end-code
        !            68: 
        !            69: \ syscall3  ( arg1 arg2 arg3 syscall_num -- retval | system call with 3 args )
        !            70: 
        !            71: code syscall3
        !            72:        .d  di )  ax   mov
        !            73:        .d   4 #  di   add
        !            74:        .d  di )  dx   mov
        !            75:        .d   4 #  di   add
        !            76:        .d  di )  cx   mov
        !            77:        .d   4 #  di   add
        !            78:        .d  di )  bx   mov
        !            79:        .d  $80 #      int
        !            80:        .d  ax    di ) mov
        !            81:        next
        !            82: end-code
        !            83: 
        !            84: 
        !            85: 
        !            86: \ sysexit ( code --  | exit to system with code )
        !            87: \   sysexit is NOT the recommended way to exit back to the 
        !            88: \   system from Forth. It is provided here as a demo of a very 
        !            89: \   simple syscall.
        !            90: 
        !            91: : sysexit  1 syscall1 ;
        !            92: 
        !            93: : getpid ( -- u | get process id )
        !            94:        20 syscall0 ;
        !            95: 
        !            96: : open ( ^zaddr  flags -- fd | file descriptor is returned)
        !            97: \   Note zaddr points to a buffer containing the counted filename
        !            98: \   string terminated with a null character.
        !            99:         swap 1+ swap
        !           100:        0       \ set mode to zero  
        !           101:        5 syscall3 ;
        !           102: 
        !           103: : close ( fd -- flag )  6 syscall1 ;
        !           104: 
        !           105: 
        !           106: : read ( fd  buf  count --  n | read count byes into buf from file )
        !           107:        3 syscall3 ;
        !           108: 
        !           109: 
        !           110: : write ( fd  buf  count  --  n  | write count bytes from buf to file )
        !           111:        4 syscall3 ;
        !           112: 
        !           113: 
        !           114: : lseek ( fd  offset  type  --  offs  | reposition the file ptr )
        !           115:        19 syscall3 ;
        !           116: 
        !           117: : ioctl ( fd  request argp -- error )
        !           118:         54 syscall3 ;
        !           119: 
        !           120: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>