| Index | Manual | Download | Source | Macros | Embedded word Case ins.
Sitemap | Links | Forum | Tests | Projects

# syslin.htm
Macros
SysANSI2Wide
SysAssignFile
SysCloseFile
SysCreateFile
SysEachFile
SysExitProcess
SysGetArg
SysGetCurrentDirectory
SysGetEnvironment
SysGetEuroasmOS
SysGetFileSize
SysGetFileTime
SysGetStackSize
SysGetUTC
SysMkDir
SysOpenFileMap
SysWriteFile

This macrolibrary syslin.htm is Linux version of macroinstructions which encapsulate system calls in EuroAssembler.

List of used kernel services:

> In order to port EuroAssembler to a different operating system:
> 1. Specify 1..4 character shortcut of the system, e.g. `Lin, Win, BSD, OS2, Sol,...` and set it to %^EuroasmOS variable instead of `Win`.
> 2. Create source file sys%^EuroasmOS.htm.
> 3. Rewrite all macros in this source file, exactly keeping their parameters, input, output and functionality.
> 4. Rebuild EuroAssembler.
> 5. Update documentation.

    syslin  HEAD ; Start of interface, which is included to other modules.

↑ SysANSI2Wide CodePage, ANSIPtr, ANSISize, WidePtr, WideSize
  SysANSI2Wide converts 8bit ANSI characters to 16bit Wide characters using the specified CodePage. Linux version of €ASM uses simplified version where ANSI character is converted to Unichar by clearing upper byte.
Input
  CodePage Integer codepage identifier, see [CodePageMS].
  ANSIPtr Pointer to the input byte string.
  ANSISize Number of bytes in input string.
  WidePtr Pointer to the output buffer.
  WideSize Size of the output buffer in 16bit UNICHARs.
Output
  CF=0, output buffer written.
Error
  CF=1 if insuffucuent buffer, invalid translation etc.
Invoked from
  ExpStoreUString.

    SysANSI2Wide %MACRO Codepage, ANSIPtr, ANSISize, WidePtr, WideSize
          PUSHAD
            MOV ESI,%ANSIPtr
            MOV ECX,%ANSISize
            MOV EDI,%WidePtr
            MOV EDX,%WideSize
            ADD EDX,EDI
            XOR EAX,EAX
    SysANSI2Wide2%.:
            LODSB
            CMP EDI,EDX
            JNB SysANSI2Wide9%.:
            STOSW
            LOOP SysANSI2Wide2%.:
    SysANSI2Wide9%.:
            CMC
          POPAD
       %ENDMACRO SysANSI2Wide

↑ SysAssignFile File, FilenamePtr, FilenameSize
  Assign filename to the FILE structure.
Input
  File Pointer to a FILE object.
  FilenamePtr ASCII filename without quotes. Not zero terminated.
  FilenameSize in bytes. Must not exceed 256.
Output
  CF=0
Error
  CF=1

    SysAssignFile %MACRO File, FilenamePtr, FilenameSize
           FileAssign %File, %FilenamePtr,Size=%FilenameSize, Unicode=0
         %ENDMACRO SysAssignFile

↑ SysCloseFile File
  Close the opened file, its memory mapping, free its buffers.
Input
  File Pointer to a FILE.
Output
  N/A
Uses
  FileClose

    SysCloseFile %MACRO File
          FileClose %File
        %ENDMACRO SysCloseFile

↑ SysCreateFile File, FileName$
  Open the file for writing.
Input
  File Pointer to a FILE.
  FileName$ Pointer to a zero-terminated file name.
Output
  CF=0
  EAX=0
Error
  CF=1
  EAX= error code
Uses
  FileAssign, FileCreate

    SysCreateFile %MACRO File, FileName$
         FileAssign %File,%FileName$
         FileMkDir  %File
         FileCreate %File
        %ENDMACRO SysCreateFile

↑ SysEachFile File, Callback
  Searches a directory for a file whose name matches the mask assigned to the file, and performs Callback procedure with each such filename. If no file matches the mask, %CallbackProc is never called.
Input
  File ^FILE with filename and path assigned to it. The filename may contain wildcards.
  Callback is the address of callback procedure performed with each file, see FileEach.
Output
  EAX= EAX=content of EAX at the last CallbackProc call exit or 0 if the CallbackProc was never called.
Uses
  FileEach

    SysEachFile %MACRO File, Callback
                  FileEach %File, %Callback
                %ENDMACRO SysEachFile

↑ SysExitProcess Errorlevel
  Terminate the euroasm program with specified errorlevel.
Input
  Errorlevel is a plain number 0..9.
Output
  N/A

    SysExitProcess %MACRO Errorlevel
                     TerminateProgram  %Errorlevel
                   %ENDMACRO SysExitProcess

↑ SysGetArg ArgNr
  Get AgrNr-th argument from the command line.
Input
  ArgNr is ordinal number of argument (1,2,,,).
Output
  CF=0
  ESI= pointer to the argument. It is readonly and not zero-terminated.
  ECX= size of argument.
Error
  CF=1 if the argument was not provided.
  ESI=0
  ECX=0
Uses
  GetArg

    SysGetArg %MACRO ArgNr
                GetArg %ArgNr,Frame=ESP
              %ENDMACRO SysGetArg

↑ SysGetCurrentDirectory DirNamePtr, DirNameSize=
  Macro will fill the room reserved at DirNamePtr with full path to the current directory. The buffer must be at least MAX_PATH_SIZE=260 bytes long.
Input
  DirNamePtr Points to the reserved space for the directory name.
  DirNameSize=NAME_MAX maximal possible size of directory name.
  Frame=ESP specifies the pointer to process stack, i. e. to the DWORD with process `argc` value.
Output
  CF=0, DirName set.
Error
  CF=1
when environment `$PWD` was not found.
Invoked from
  EaMain.

    SysGetCurrentDirectory %MACRO DirNamePtr, DirNameSize=NAME_MAX, Frame=ESP
         PUSHD %Frame, %DirNameSize, %DirNamePtr
         CALL SysGetCurrentDirectory@RT:
    SysGetCurrentDirectory@RT: PROC1
         PUSHAD
           MOV EBX,[ESP+11*4]              ; %Frame
           MOV EAX,[EBX]
           LEA EBX,[EBX+4*EAX+4]           ; EBX points at env.string, say PWD=/mnt
           MOV EDI,[ESP+9*4]               ; %NamePtr
           MOV ECX,[ESP+10*4]              ; %NameSize
           MOVB [EDI],0
    .10:   ADD EBX,4
           MOV ESI,[EBX]
           TEST ESI
           STC
           JZ .90:                         ; If not found.
           LODSD
           CMP EAX,'PWD='
           JNE .10:
           REP MOVSB
    .90:  POPAD
          RET 3*4
        ENDP1 SysGetCurrentDirectory@RT:
      %ENDMACRO SysGetCurrentDirectory

↑ SysGetEnvironment NamePtr, NameSize, OutEnvBuffer
  This macro retrieves environment variable value from the hosting operation system at assembly time.
Input
  NamePtr Points to the name of required environment variable.
  NameSize Size of variable name in bytes.
  OutEnvBuffer Pointer to a BUFFER where the contents of environment variable will be written to. The contents will not be zero-terminated.
Output
  ZF=0 if the variable was found and its contents written to OutEnvBuffer.
Error
  ZF=1 if the environment variable was not found.
Invoked from
  Stm_pcSETE.

    SysGetEnvironment %MACRO NamePtr, NameSize, OutEnvBuffer
         PUSHD %OutEnvBuffer, %NameSize, %NamePtr
         CALL SysGetEnvironment@RT:
    SysGetEnvironment@RT: PROC1
         PUSHAD
           MOV EBX,[Ea.StackOrg::]
           MOV EAX,[EBX]                   ; EAX is the number of cmd-line arguments.
           LEA EBX,[EBX+4*EAX+4]           ; EBX points at env.string, say TERM=/xterm
    .10:   ADD EBX,4
           MOV ESI,[EBX]
           TEST ESI
           JZ .80:                         ; If not found.
           MOV EDI,[ESP+9*4]               ; %NamePtr
           MOV ECX,[ESP+10*4]              ; %NameSize
           REPE CMPSB
           JNE .10:
           LODSB
           CMP AL,'='
           JNE .10:
           MOV EBX,[ESP+11*4]              ; %OutEnvBuffer
    .30:   LODSB
           CMP AL,0
           JE .80:
           BufferStoreByte EBX,EAX
           JMP .30:
    .80:   CMP AL,1                        ; Set ZF=0.
         POPAD
         RET 3*4
         ENDPROC1 SysGetEnvironment@RT:
      %ENDMACRO SysGetEnvironment

↑ SysGetEuroasmOS
  Macro returns NUL-padded 1..4 byte string `Win` (or `Lin` etc) depending on OS which the EuroAssembler is compiled for.
Input
  -
Output
  EAX= Operating system identifier.

    SysGetEuroasmOS %MACRO EuroasmOSPtr
            MOV EAX,"Lin" ; OS version is hardwired here. Change it in ported versions.
         %ENDMACRO SysGetEuroasmOS

↑ SysGetStackSize

  Macro SysGetStackSize returns the amount of memory in bytes reserved during linkage of the actually running euroasm.exe . Obtained value will be used for protection from stack overflow error.

  > Stack in the loaded PECOFF executable is allocated in virtual address space but I didn't find a reliable way how to get its size, so I set a fiction value here.

Input
  -
Output
  CF=0, EAX= reserved stack size in bytes.

    SysGetStackSize %MACRO
         MOV EAX,1M
         CLC
        %ENDMACRO SysGetStackSize

↑ SysGetUTC
  Macro returns current system time as the number of seconds since midnight 1.1.1970 UTC.
Input
  -
Output
  EAX number of seconds since January 1st 1970 0:0:0 UTC.

    SysGetUTC %MACRO
        LinAPI time,0
     %ENDMACRO SysGetUTC

↑ SysOpenFileMap File, FileNamePtr, FileNameSize=-1
  Open the file for reading and map it to memory.
Input
  File Pointer to a FILE. The object will be assigned with the FileName and opened for reading.
  FileNamePtr is pointer to the file name (unquoted ASCII byte string).
  FileNameSize= is the number of bytes in file name. It may be omitted if the file name is zero-terminated.
Output
  CF=0
  ESI= is pointer to memory mapped file contents.
  EAX= is file size.
On error
  CF=1
  ESI=0
  EAX= error code.
Uses
  FileAssign, FileMapOpen

    SysOpenFileMap %MACRO File, FileNamePtr, FileNameSize=-1
          FileAssign %File, %FileNamePtr, Size=%FileNameSize
          FileMapOpen %File
        %ENDMACRO SysOpenFileMap

↑ SysWriteFile File, DataPtr, DataSize
  Write data. The file must have been opened with SysCreateFile.
Input
  File Pointer to a FILE.
  DataPtr Pointer to the data to be written.
  DataSize size of the data.
Output
  CF=0
  EAX= number of bytes written to the file.
On error
  CF=1
  EAX= error code.
Uses
  FileWrite

    SysWriteFile %MACRO File, DataPtr, DataSize
          FileWrite %File,%DataPtr,%DataSize
         %ENDMACRO SysWriteFile

↑ SysGetFileTime FileNamePtr
  Macro returns the time when the file was last written to as a number of seconds since midnight 1.1.1970 UTC. If the file is not found or not accessible, 0 is returned.
Input
  File Pointer to ASCIIZ file name without quotes.
Output
  EAX=timestamp.
On error
  EAX=0.

    SysGetFileTime %MACRO FileNamePtr
       PUSH EDI
         SUB ESP,SIZE# STAT64 ; Make room for STAT64structure (44 bytes).
         MOV EDI,ESP
         LinAPI stat64,%FileNamePtr,EDI
         MOV EAX,[EDI+STAT64.st_mtime]
         ADD ESP,SIZE# STAT64
       POP EDI
     %ENDMACRO SysGetFileTime

↑ SysGetFileSize FileNamePtr
  Macro returns the size of input file in bytes. If the file is not found or not accessible, 0 is returned.
Input
  File Pointer to ASCIIZ file name without quotes.
Output
  EDX:EAX=filesize.
On error
  EDX:EAX=0.

    SysGetFileSize %MACRO FileNamePtr
       PUSH EDI
         SUB ESP,SIZE# STAT64 ; Make room for STAT64structure (44 bytes).
         MOV EDI,ESP
         LinAPI stat64,%FileNamePtr,EDI
         MOV EAX,[EDI+STAT64.st_size+0]
         MOV EDX,[EDI+STAT64.st_size+4]
         ADD ESP,SIZE# STAT64
       POP EDI
     %ENDMACRO SysGetFileSize

↑ SysMkDir theFile
  will parse the file name and create all directories in its path if they didn't exist.
  The last portion of assigned name is treated as directory only if it is terminated with slash.
Input
  theFile is pointer to FILE object with assigned name.
Output
  CF=0
  EAX=0 if the directory was created or it existed before.
Error
  CF=1
  EAX=last error if CreateDirectory failed.

    SysMkDir %MACRO theFile
        FileMkDir %theFile
     %ENDMACRO SysMkDir

       ENDHEAD syslin

▲Back to the top▲