📄 macros.inc
字号:
; NOTE: The parameter order is the normal
; assembler order of,
; instruction/destination/source
; -------------------------------------------
; --------------------------
; szstring to szstring copy
; --------------------------
cst MACRO arg1,arg2
invoke szCopy,reparg(arg2),tstarg(arg1)
ENDM
; ----------------------------
; memory to memory assignment
; ----------------------------
m2m MACRO M1, M2
push M2
pop M1
ENDM
; --------------------------------------------------
; memory to memory assignment using the EAX register
; --------------------------------------------------
mrm MACRO m1, m2
mov eax, m2
mov m1, eax
ENDM
; *******************************************
; String Assign *
; Assign quoted text to a locally declared *
; string handle (DWORD variable) in a proc *
; to effectively have a LOCAL scope strings *
; EXAMPLE : *
; sas MyVar,"This is an assigned string" *
; *******************************************
sas MACRO var,quoted_text:VARARG
LOCAL txtname
.data
txtname db quoted_text,0
align 4
.code
mov var, OFFSET txtname
ENDM
; -----------------------------------
; create a font and return its handle
; -----------------------------------
GetFontHandle MACRO fnam:REQ,fsiz:REQ,fwgt:REQ
invoke RetFontHandle,reparg(fnam),fsiz,fwgt
EXITM <eax>
ENDM
; **************
; File IO Macros
; **************
; ---------------------------------------------------------------------
; create a new file with read / write access and return the file handle
; ---------------------------------------------------------------------
fcreate MACRO filename
invoke CreateFile,reparg(filename),GENERIC_READ or GENERIC_WRITE,
NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
EXITM <eax> ;; return file handle
ENDM
; ------------------
; delete a disk file
; ------------------
fdelete MACRO filename
invoke DeleteFile,reparg(filename)
EXITM <eax>
ENDM
; ------------------------------
; flush open file buffer to disk
; ------------------------------
fflush MACRO hfile
invoke FlushFileBuffers,hfile
ENDM
; -------------------------------------------------------------------------
; open an existing file with read / write access and return the file handle
; -------------------------------------------------------------------------
fopen MACRO filename
invoke CreateFile,reparg(filename),GENERIC_READ or GENERIC_WRITE,
NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
EXITM <eax> ;; return file handle
ENDM
; ------------------
; close an open file
; ------------------
fclose MACRO arg:REQ
invoke CloseHandle,arg
ENDM
; ------------------------------------------------
; read data from an open file into a memory buffer
; ------------------------------------------------
fread MACRO hFile,buffer,bcnt
LOCAL var
.data?
var dd ?
.code
invoke ReadFile,hFile,buffer,bcnt,ADDR var,NULL
mov eax, var
EXITM <eax> ;; return bytes read
ENDM
; ----------------------------------------
; write data from a buffer to an open file
; ----------------------------------------
fwrite MACRO hFile,buffer,bcnt
LOCAL var
.data?
var dd ?
.code
invoke WriteFile,hFile,buffer,bcnt,ADDR var,NULL
mov eax, var
EXITM <eax> ;; return bytes written
ENDM
; ----------------------------------------------------
; write a line of zero terminated text to an open file
; ----------------------------------------------------
fprint MACRO hFile:REQ,text:VARARG ;; zero terminated text
LOCAL var
LOCAL pst
.data?
var dd ?
pst dd ?
.code
mov pst, repargv(text)
invoke WriteFile,hFile,pst,len(pst),ADDR var,NULL
invoke WriteFile,hFile,chr$(13,10),2,ADDR var,NULL
ENDM
; ---------------------------------
; write zero terminated text with C
; style formatting to an open file.
; ---------------------------------
fprintc MACRO hFile:REQ,text:VARARG ;; zero terminated text
LOCAL var
LOCAL pst
.data?
var dd ?
pst dd ?
.code
mov pst, cfm$(text)
invoke WriteFile,hFile,pst,len(pst),ADDR var,NULL
ENDM
; ------------------------------------
; set the position of the file pointer
; ------------------------------------
fseek MACRO hFile,distance,location
IFIDN <location>,<BEGIN>
var equ <FILE_BEGIN>
ELSEIFIDN <location>,<CURRENT>
var equ <FILE_CURRENT>
ELSEIFIDN <location>,<END>
var equ <FILE_END>
ELSE
var equ <location>
ENDIF
invoke SetFilePointer,hFile,distance,0,var
EXITM <eax> ;; return current file offset
ENDM
; ------------------------------------------------
; set end of file at current file pointer location
; ------------------------------------------------
fseteof MACRO hFile
invoke SetEndOfFile,hFile
ENDM
; -------------------------------
; return the size of an open file
; -------------------------------
fsize MACRO hFile
invoke GetFileSize,hFile,NULL
EXITM <eax>
ENDM
; ---------------------------------------
; extended formatting version writes text
; to the current file pointer location
; ---------------------------------------
ftext MACRO hFile:REQ,args:VARARG
push esi ;; preserve ESI
mov esi, alloc(16384) ;; allocate 16k of buffer
catargs ftext,esi,args ;; write ALL args to a single string
push eax ;; make 4 bytes on the stack
invoke WriteFile,hFile,esi,len(esi),esp,NULL
pop eax ;; release the 4 bytes
free esi ;; free the memory buffer
pop esi ;; restore ESI
ENDM
; ----------------------------------------------------------
; function position macros that takes a DWORD parameter and
; returns the address of the buffer that holds the result.
; The return format is for use within the INVOKE syntax.
; ----------------------------------------------------------
str$ MACRO DDvalue
LOCAL rvstring
.data
rvstring db 20 dup (0)
align 4
.code
invoke dwtoa,DDvalue,ADDR rvstring
EXITM <ADDR rvstring>
ENDM
hex$ MACRO DDvalue
LOCAL rvstring
.data
rvstring db 12 dup (0)
align 4
.code
invoke dw2hex,DDvalue,ADDR rvstring
EXITM <ADDR rvstring>
ENDM
; *************************************************
; The following numeric to string conversions were
; written by Greg Lyon using the "sprintf" function
; in the standard C runtime DLL MSVCRT.DLL
; *************************************************
ubyte$ MACRO ubytevalue:req
;; unsigned byte
LOCAL buffer, ubtmp
.data?
ubtmp BYTE ?
buffer BYTE 4 dup(?)
IFNDEF ubfmt
.data
ubfmt BYTE "%hhu", 0
ENDIF
.code
IFE issize(ubytevalue, 1)
echo ----------------------
echo ubyte$ - requires BYTE
echo ----------------------
.ERR
ENDIF
mov buffer[0], 0
IF isregister(ubytevalue)
mov ubtmp, ubytevalue
movzx eax, ubtmp
ELSE
mov al, ubytevalue
movzx eax, al
ENDIF
invoke crt_sprintf, ADDR buffer, ADDR ubfmt, eax
EXITM <OFFSET buffer>
ENDM
; ----------------------------------------------
sbyte$ MACRO sbytevalue:req
;; signed byte
LOCAL buffer, sbtmp
.data?
sbtmp SBYTE ?
buffer BYTE 8 dup(?)
IFNDEF sbfmt
.data
sbfmt BYTE "%hhd", 0
ENDIF
.code
IFE issize(sbytevalue, 1)
echo -----------------------
echo sbyte$ - requires SBYTE
echo -----------------------
.ERR
ENDIF
mov buffer[0], 0
IF isregister(sbytevalue)
mov sbtmp, sbytevalue
movsx eax, sbtmp
ELSE
mov al, sbytevalue
movsx eax, al
ENDIF
invoke crt_sprintf, ADDR buffer, ADDR sbfmt, eax
EXITM <OFFSET buffer>
ENDM
; ----------------------------------------------
xbyte$ MACRO xbytevalue:req
;; unsigned hex byte
LOCAL buffer, xbtmp
.data?
xbtmp BYTE ?
buffer BYTE 4 dup(?)
IFNDEF xbfmt
.data
xbfmt BYTE "%hhX", 0
ENDIF
.code
IFE issize(xbytevalue, 1)
echo ----------------------
echo xbyte$ - requires BYTE
echo ----------------------
.ERR
ENDIF
mov buffer[0], 0
IF isregister(xbytevalue)
mov xbtmp, xbytevalue
movzx eax, xbtmp
ELSE
mov al, xbytevalue
movzx eax, al
ENDIF
invoke crt_sprintf, ADDR buffer, ADDR xbfmt, eax
EXITM <OFFSET buffer>
ENDM
; ----------------------------------------------
uword$ MACRO uwordvalue:req
;; unsigned word
LOCAL buffer, uwtmp
.data?
uwtmp WORD ?
buffer BYTE 8 dup(?)
IFNDEF uwfmt
.data
uwfmt BYTE "%hu", 0
ENDIF
.code
IFE issize(uwordvalue, 2)
echo ----------------------
echo uword$ - requires WORD
echo ----------------------
.ERR
ENDIF
mov buffer[0], 0
IF isregister(uwordvalue)
mov uwtmp, uwordvalue
movzx eax, uwtmp
ELSE
mov ax, uwordvalue
movzx eax, ax
ENDIF
invoke crt_sprintf, ADDR buffer, ADDR uwfmt, eax
EXITM <OFFSET buffer>
ENDM
; ----------------------------------------------
sword$ MACRO swordvalue:req
;; signed word
LOCAL buffer, swtmp
.data?
swtmp SWORD ?
buffer BYTE 8 dup(?)
IFNDEF swfmt
.data
swfmt BYTE "%hd", 0
ENDIF
.code
IFE issize(swordvalue, 2)
echo -----------------------
echo sword$ - requires SWORD
echo -----------------------
.ERR
ENDIF
mov buffer[0], 0
IF isregister(swordvalue)
mov swtmp, swordvalue
movsx eax, swtmp
ELSE
mov ax, swordvalue
movsx eax, ax
ENDIF
invoke crt_sprintf, ADDR buffer, ADDR swfmt, eax
EXITM <OFFSET buffer>
ENDM
; ----------------------------------------------
xword$ MACRO xwordvalue:req
;; unsigned hex word
LOCAL buffer, xwtmp
.data?
xwtmp WORD ?
buffer BYTE 8 dup(?)
IFNDEF xwfmt
.data
xwfmt BYTE "%hX", 0
ENDIF
.code
IFE issize(xwordvalue, 2)
echo ----------------------
echo xword$ - requires WORD
echo ----------------------
.ERR
ENDIF
mov buffer[0], 0
IF isregister(xwordvalue)
mov xwtmp, xwordvalue
movzx eax, xwtmp
ELSE
mov ax, xwordvalue
movzx eax, ax
ENDIF
invoke crt_sprintf, ADDR buffer, ADDR xwfmt, eax
EXITM <OFFSET buffer>
ENDM
; ----------------------------------------------
udword$ MACRO udwordvalue:req
;; unsigned dword
LOCAL buffer, udtmp
.data?
udtmp DWORD ?
buffer BYTE 12 dup(?)
IFNDEF udfmt
.data
udfmt BYTE "%lu", 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -