EuroAssembler Index Manual Download Source Macros


Sitemap Links Forum Tests Projects

syswin.htm
Macros
SysAlloc
SysANSI2Wide
SysAssignFile
SysCloseFile
SysCreateFile
SysEachFile
SysExitProcess
SysFileTimeToDosDateTime
SysFree
SysGetAllocationGranularity
SysGetArg
SysGetCurrentDirectory
SysGetEnvironment
SysGetEuroasmOS
SysGetExe
SysGetFileSize
SysGetFileTime
SysGetStackSize
SysGetUTC
SysOpenFileMap
SysOutConsole
SysWriteFile

This file 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,... and set it to %^EuroasmOS variable instead of Win.
  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  PROGRAM FORMAT=COFF,MODEL=FLAT,WIDTH=32
        INCLUDEHEAD "euroasm.htm" ; Interface (structures, symbols and macros) of other modules.
        INCLUDEHEAD ea.htm,  eaopt.htm, msg.htm,  pgmopt.htm  ; Include headers of another modules used in this module.
syswin  HEAD ; Start of interface, which is included to other modules.
        INCLUDEHEAD1 winapi.htm, wins.htm, winscon.htm, winstime.htm, winsfile.htm, winf32.htm
↑ SysGetAllocationGranularity
This inline macro returns granularity with which virtual memory is allocated by the operating system, usually it is 64 KB.
Input
-
Output
EAX= size of allocatable blocks.
Error
-
Invoked from
PoolCreate, PoolNew
SysGetAllocationGranularity %MACRO
    PUSH EBX,ECX,EDX
     SUB ESP,SIZE# SYSTEM_INFO
     MOV EBX,ESP
     WinAPI GetSystemInfo,EBX
     MOV EAX,[EBX+SYSTEM_INFO.AllocationGranularity]
     ADD ESP,SIZE# SYSTEM_INFO
    POP EDX,ECX,EBX
  %ENDMACRO SysGetAllocationGranularity
↑ SysAlloc BlockSize
Allocate block of physical memory from the operating system.
Input
BlockSize= is the requested amount of memory in bytes, rounded up to AllocationGranularity.
Output
ZF=0
EAX= address of allocated block.
Error
ZF=1
EAX=0
See also
SysGetAllocationGranularity
Invoked from
PoolNew
SysAlloc %MACRO BlockSize
            PUSH ECX,EDX
              MOV EAX,[Ea.MemAllocated::]
              MOV ECX,[Ea.MemPeak::]
              MOV EDX,%BlockSize
              ADD EAX,EDX
              MOV [Ea.MemAllocated::],EAX
              CMP EAX,ECX
              JBE SysAlloc%.:
              MOV [Ea.MemPeak::],EAX
 SysAlloc%.:  WinAPI VirtualAlloc, 0, EDX, MEM_COMMIT, PAGE_READWRITE
              TEST EAX
            POP EDX,ECX
         %ENDMACRO SysAlloc
↑ SysFree BlockAddress, BlockSize
Return the block of memory allocated by SysAlloc to the operating system.
Input
BlockAddress is the virtual address of memory block being freed.
BlockSize is the brutto size of released block. Is is not used by the function VirtualFree(), because Windows virtual memory manager remebers the size of each previously allocated block.
Output
ZF=0, EAX= nonzero value.
Error
ZF=1, EAX=0
See also
SysAlloc
Invoked from
PoolDestroy
SysFree %MACRO BlockAddress, BlockSize
          PUSH ECX,EDX
            SUB [Ea.MemAllocated],%BlockSize
            WinAPI VirtualFree, %BlockAddress, 0, MEM_RELEASE
            TEST EAX
          POP EDX,ECX
        %ENDMACRO SysFree
↑ 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
↑ 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$
     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
                 WinAPI ExitProcess, %Errorlevel ; WinAPI function.
               %ENDMACRO SysExitProcess
↑SysFileTimeToDosDateTime FileTimePtr
Macro SysFileTimeToDosDateTime converts a 64-bit file time to MS-DOS date and time.
Input
FileTimePtr is pointer to FILETIME object, which is QWORD representing he number of 100 ns since 1.1.1601 00:00:00 as 64bit signed integer.
Output
EAX= DosDateTime (FAT format) where low word (AX) is DosTime and high word is DosDate.
Example
Time 2004/08/28 21:07:42 UTC represented in memory as FileTime 0x01C4_8D3A__ACCD_B800 returns DosDateTime EAX=0xF5A8_1C31.
SysFileTimeToDosDateTime %MACRO FileTimePtr
       PUSH ECX,EDX,EAX
         MOV EDX,%FileTimePtr
         MOV EAX,ESP
         LEA ECX,[EAX+2]
         WinAPI FileTimeToDosDateTime, EDX,ECX,EAX
       POP EAX,EDX,ECX
     %ENDMACRO SysFileTimeToDosDateTime
↑ 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
EaCreate.
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
↑ SysGetExe FileNamePtr
Macro will fill the memory pointed to by FileNamePtr with full path to the actually running euroasm.exe. The memory must be at least MAX_PATH_SIZE=260 bytes long.
SysGetExe %MACRO FilenamePtr
     PUSH ECX,EDX
       WinAPI GetModuleFileName,0,%1,MAX_PATH_SIZE
     POP EDX,ECX
   %ENDMACRO SysGetExe
↑ 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 + 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 + 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
↑ SysOutConsole TextPtr, TextSize
Write ASCIIZ text byte string to the standard output. The text is either zero-terminated, or the second parameter Size must be specified.
Input
TextPtr points to the first byte of text.
TextSize specifies the number of bytes. It may be omitted when the text is zero-terminated.
Output
-
Uses
FileAssign, FileWrite
SysOutConsole %MACRO TextPtr, TextSize
      %IF %# = 1 ; If only one parameter provided, use the max.integer value as text size.
        PUSHD -1
      %ELSE
        PUSHD %TextSize
      %ENDIF
      PUSHD %TextPtr
      CALL SysOutConsoleRT:
SysOutConsoleRT: PROC1
      PUSHAD
        MOV EBP,ESP
        SUB ESP,SIZE# FILE
        MOV EBX,ESP ; ^FILE.
        MOV EDI,ESP
        MOV ECX,SIZE# FILE / 4
        XOR EAX,EAX
        REP STOSD ; Clear the FILE.
        FileAssign EBX,0 ; Standard output instead of filename.
        FileCreate EBX   ; Open the FILE for output.
        MOV EDI,[EBP+36] ; %TextPtr.
        MOV ECX,[EBP+40] ; %TextSize.
        MOV ESI,EDI
        JECXZ .Empty:
        REPNE SCASB
        SUB EDI,ESI
        CMPB [ESI+EDI-1],0
        JNZ .NZ:
        DEC EDI ; EDI is now netto size of the text.
 .NZ:   FileWrite EBX,ESI,EDI
 .Empty:MOV ESP,EBP
      POPAD
      RET 2*4
    ENDPROC1 SysOutConsoleRT:
  %ENDMACRO SysOutConsole
↑ 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*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
.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
ENDPROGRAM syswin

▲Back to the top▲