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

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

This macrolibrary syswin.htm is MS Windows version of macroinstructions which encapsulate system calls in EuroAssembler.

List of services hired from Windows API:
CloseHandle CreateDirectory CreateFile CreateFileMapping ExitProcess FileTimeToDosDateTime FindClose FindFirstFile FindNextFile GetCommandLine GetEnvironmentVariable GetLastError GetModuleFileName GetModuleFileName GetModuleHandle GetSystemInfo GetSystemTime GlobalFree MapViewOfFile MultiByteToWideChar SetFilePointer SystemTimeToFileTime UnmapViewOfFile VirtualAlloc VirtualFree WriteFile

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

    syswin  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.
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
  ZF=0, output buffer written.
Error
  ZF=1 if insuffucuent buffer, invalid translation etc.
Invoked from
  ExpStoreUString.

    SysANSI2Wide %MACRO Codepage, ANSIPtr, ANSISize, WidePtr, WideSize
          PUSH EAX,ECX,EDX
           WinAPI MultiByteToWideChar, %Codepage, MB_PRECOMPOSED, %ANSIPtr, %ANSISize, %WidePtr, %WideSize
           TEST EAX
          POP EDX,ECX,EAX
       %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

↑ 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

↑ SysCreateFile File, FileName$
  Open the file for writing.
Input
  File Pointer to a FILE.
  FileName$ Pointer to 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
  ECX= size of argument. It is readonly and not zero-terminated.
Error
  CF=1 if the argument was not provided.
  ESI=0
  ECX=0
Uses
  GetArg

    SysGetArg %MACRO ArgNr
                GetArg %ArgNr
              %ENDMACRO SysGetArg

↑ SysGetCurrentDirectory DirNamePtr
  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.
Output
  CF=0
Error
  CF=1
Invoked from
  EaMaiate.

    SysGetCurrentDirectory %MACRO DirNamePtr
          PUSH EAX,ECX,EDX
            WinAPI GetCurrentDirectoryA, MAX_PATH_SIZE, %DirNamePtr
            CMP EAX,1 ; Set CF if EAX=0 on return.
          POP EDX,ECX,EAX
         %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 value is not zero-terminated.
Output
  ZF=0 if at least one byte was written to OutBuffer.
Error
  ZF=1 if the environment variable was not found or it was empty.
Invoked from
  Stm_pcSETE.

    SysGetEnvironment %MACRO NamePtr, NameSize, OutEnvBuffer
         PUSHD %OutEnvBuffer, %NameSize,%NamePtr
         CALL SysGetEnvironment@RT:
    SysGetEnvironment@RT: PROC1
         PUSHAD
    :     Invoke EaBufferReserve::,SysGetEnvironment@RT
          MOV EBX,EAX
          MOV ESI,[ESP+36]                     ; NamePtr
          MOV ECX,[ESP+40]                     ; NameSize
          BufferStore EBX,ESI,ECX
          Msg cc=C,'9314',SysGetEnvironment@RT ; Allocation error storing to buffer in !1H.
          BufferStore EBX,=W(0),2
          BufferRetrieve EBX
    :     Invoke EaBufferRelease::,EBX
          ; ESI is pointer to ASCIIZ variable name.
          SUB ECX,ECX
          WinAPI GetEnvironmentVariableA,ESI,ECX,ECX
          MOV ECX,EAX                           ; Required value size.
          TEST EAX
          JZ .99:
          MOV EDI,[ESP+44]                      ; OutEnvBuffer
          BufferNew EDI,ECX
          Msg cc=C,'9314',SysGetEnvironment@RT  ; Allocation error storing to buffer in !1H.
          JC .99:
          WinAPI GetEnvironmentVariableA,ESI,EAX,ECX
          BufferDecrement EDI
          TEST EAX                              ; ZF=0 on success.
    .99: POPAD
         RET 12
         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,"Win" ; 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 somewhere below ImageBase but I didn't find a reliable way how to get its VA, so I read it from optional header in the loaded executable module.

Input
  -
Output
  CF=0, EAX= reserved stack size in bytes.
Error
  CF=1, EAX= default (1M).

    SysGetStackSize %MACRO
         PUSHAD
          MOV EBX,1M ; Initialize SizeOfStackReserve with default value.
          WinAPI GetModuleHandle,0
          TEST EAX
          STC
          JZ .R%.:
          CMPW [EAX+PFMZ_DOS_HEADER.e_magic],'MZ'
          STC
          JNE .R%.:
          ADD EAX,[EAX+PFMZ_DOS_HEADER.e_lfanew] ; Get pointer to PE signature.
          CMPD [EAX],'PE' ; EAX points to PE signature, file header and optional header.
          STC
          JNE .R%.:
          CLC
          MOV EBX,[EAX+4+SIZE#PFCOFF_FILE_HEADER+PFPE_OPTIONAL_HEADER32.SizeOfStackReserve]
    .R%.: MOV [ESP+28],EBX ; %ReturnEAX.
         POPAD
        %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
        CALL SysGetUTC@RT:
    SysGetUTC@RT: PROC1
        PUSH ECX,EDX,ESI,EDI
         SUB ESP,8 + 16                    ; 8 + SIZE# SYSTEMTIME
         MOV EDI,ESP                       ; EDI is pointer to FILETIME (QWORD).
         LEA ESI,[ESP+8]                   ; ESI is pointer to SYSTEMTIME.
         WinAPI GetSystemTime,ESI
         WinAPI SystemTimeToFileTime,ESI,EDI
         ; FILETIME at EDI is now an unsigned integer QWORD with 100ns intervals elapsed since midnight 1.1.1601.
         ; 134774 days elapsed between 1.1.1601 and 1.1.1970,
         ; i.e. 134774*24*60*60*10000000=0x019DB1DE_D53E8000 must be subtracted from FILETIME at EDI.
         MOV EAX,[EDI+0]
         MOV EDX,[EDI+4]
         SUB EAX,0xD53E8000
         SBB EDX,0x019DB1DE
         MOV ECX,10000000
         DIV ECX
         ADD ESP,8 + 16                    ; 8 + SIZE# SYSTEMTIME
        POP EDI,ESI,EDX,ECX
        RET
       ENDPROC1 SysGetUTC@RT:
     %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
         MOV EAX,%FileNamePtr
         CALL SysGetFileTime@RT:
    SysGetFileTime@RT: PROC1
       PUSH EBX,ECX,EDX,EDI
         SUB ESP,8 ; Make room for FILETIME structure (QWORD).
         MOV EDI,ESP
         WinAPI CreateFile,EAX,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
         MOV EBX,EAX
         WinAPI GetFileTime,EBX,0,0,EDI
         TEST EAX
         JZ .80:
         ; FILETIME at EDI is now an unsigned integer QWORD with 100ns since midnight 1.1.1601.
         ; 134774 days elapsed between 1.1.1601 and 1.1.1970,
         ; i.e. 134774*24*60*60*10_000_000=0x019D_B1DE_D53E_8000 must be subtracted from FileTime at EDI.
         MOV EAX,[EDI+0]
         MOV EDX,[EDI+4]
         SUB EAX,0xD53_E8000
         SBB EDX,0x019_DB1DE
         MOV ECX,10_000_000
         DIV ECX
    .80: ADD ESP,8
         PUSH EAX
           WinAPI CloseHandle,EBX
         POP EAX
       POP EDI,EDX,ECX,EBX
       RET
      ENDP1 SysGetFileTime@RT:
     %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
         MOV EAX,%FileNamePtr
         CALL SysGetFileSize@RT:
    SysGetFileSize@RT: PROC1
       PUSH EBX,ECX,EDI
         SUB ESP,8 ; Make room for FileSize (QWORD).
         LEA EDI,[ESP+4]
         WinAPI CreateFile,EAX,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
         MOV EBX,EAX
         WinAPI GetFileSize,EBX,EDI
         SUB EDI,4
         MOV [EDI],EAX
         INC EAX
         JNZ .80:
         WinAPI GetLastError
         TEST EAX
         JZ .80:
         SUB EAX,EAX
         MOV [EDI+0],EAX
         MOV [EDI+4],EAX
    .80: WinAPI CloseHandle,EBX
         MOV EAX,[EDI+0]
         MOV EDX,[EDI+4]
         ADD ESP,8
       POP EDI,ECX,EBX
       RET
      ENDP1 SysGetFileSize@RT:
     %ENDMACRO SysGetFileSize

       ENDHEAD syswin

▲Back to the top▲