Assembly code snippets
Details
Title | MakeArray |
---|---|
Author | bitRAKE |
Submitted by: | bitRAKE |
Date added: | 2002-02-17 21:10:35 |
Date modified: | 2002-02-17 21:10:35 |
Comments
Multi-dimensional array indexing.
Snippet
;* @ArgI - Macro function returns an argument specified by number
;* from a VARARG list.
;*
;* Params: index - one-based number of the argument to be returned
;* arglist - argument list
@ArgI MACRO index:REQ, arglist:VARARG
LOCAL count,retstr
count = 0
FOR arg,<arglist>
count = count + 1
IF count EQ index
retstr TEXTEQU <arg>
ENDIF
ENDM
EXITM retstr
ENDM
;; MakeArray - Static Array Object Macro, create data in BSS
;; segment and an indexing macro based on the array name.
;;
;; Params: thename - label name to give the array and macro base
;; thetype - a type identifier that comprises the array
;; vars - dimensions of array (least thru greatest)
MakeArray MACRO thename:REQ,thetype:REQ,vars:VARARG
LOCAL elements
;; Figure out the total number of elements
elements TEXTEQU <1>
FOR var,<&vars>
elements CATSTR %elements,<*>,%var
ENDM
;; Create the Array
_BSS segment DWORD public 'BSS'
thename LABEL thetype
;; Get a warning for standard data types in BSS, but works for all
thetype elements dup (<?>)
;; thetype elements dup (?) ;; Standard TYPEs only
_BSS ends
;; Create the indexing macro
thename&_Item MACRO args:VARARG
LOCAL count,dwidth,dist
count = 0
dwidth = 1
dist = 0
FOR arg,<&args>
;; Should test that arg is constant, output code
count = count + 1
dwidth = dwidth * TYPE thename
dist = dist + (arg * dwidth)
dwidth = dwidth * @ArgI(%count,vars)
ENDM
EXITM @CatStr(<thename>,<+>,%dist)
ENDM
ENDM
;* from a VARARG list.
;*
;* Params: index - one-based number of the argument to be returned
;* arglist - argument list
@ArgI MACRO index:REQ, arglist:VARARG
LOCAL count,retstr
count = 0
FOR arg,<arglist>
count = count + 1
IF count EQ index
retstr TEXTEQU <arg>
ENDIF
ENDM
EXITM retstr
ENDM
;; MakeArray - Static Array Object Macro, create data in BSS
;; segment and an indexing macro based on the array name.
;;
;; Params: thename - label name to give the array and macro base
;; thetype - a type identifier that comprises the array
;; vars - dimensions of array (least thru greatest)
MakeArray MACRO thename:REQ,thetype:REQ,vars:VARARG
LOCAL elements
;; Figure out the total number of elements
elements TEXTEQU <1>
FOR var,<&vars>
elements CATSTR %elements,<*>,%var
ENDM
;; Create the Array
_BSS segment DWORD public 'BSS'
thename LABEL thetype
;; Get a warning for standard data types in BSS, but works for all
thetype elements dup (<?>)
;; thetype elements dup (?) ;; Standard TYPEs only
_BSS ends
;; Create the indexing macro
thename&_Item MACRO args:VARARG
LOCAL count,dwidth,dist
count = 0
dwidth = 1
dist = 0
FOR arg,<&args>
;; Should test that arg is constant, output code
count = count + 1
dwidth = dwidth * TYPE thename
dist = dist + (arg * dwidth)
dwidth = dwidth * @ArgI(%count,vars)
ENDM
EXITM @CatStr(<thename>,<+>,%dist)
ENDM
ENDM