Difference between revisions of "Lua:registerAutoAssemblerCommand"
m (→Example 1) |
m |
||
Line 65: | Line 65: | ||
local function registerLabel(parameters, syntaxcheck) | local function registerLabel(parameters, syntaxcheck) | ||
− | + | ---- registerLabel(name) | |
− | + | if ExitOnSyntaxCheck and syntaxcheck then return end | |
− | + | local le = LineEnd | |
− | + | local name = parameters | |
− | + | if name == nil then | |
− | + | return nil, t('Wrong number of parameters, no "name", for "registerLabel" custom AA command.') | |
− | + | end | |
− | + | local str = 'label(' .. name .. ')' .. le | |
− | + | str = str .. 'registerSymbol(' .. name .. ')' | |
− | + | return str | |
end | end | ||
local function unregisterLabel(parameters, syntaxcheck) | local function unregisterLabel(parameters, syntaxcheck) | ||
− | + | ---- unregisterLabel(name) | |
− | + | if ExitOnSyntaxCheck and syntaxcheck then return end | |
− | + | local name = parameters | |
− | + | if name == nil then | |
− | + | return nil, t('Wrong number of parameters, no "name", for "unregisterLabel" custom AA command.') | |
− | + | end | |
− | + | local str = 'unregisterSymbol(' .. name .. ')' | |
− | + | return str | |
end | end | ||
Line 106: | Line 106: | ||
<span style="font-weight: 700;">local</span> <span style="font-weight: 700;">function</span> <span style="color: rgb(136, 0, 0); font-weight: 700;">registerLabel</span>(parameters, syntaxcheck) | <span style="font-weight: 700;">local</span> <span style="font-weight: 700;">function</span> <span style="color: rgb(136, 0, 0); font-weight: 700;">registerLabel</span>(parameters, syntaxcheck) | ||
− | + | <span style="color: rgb(136, 136, 136);">---- registerLabel(name)</span> | |
− | + | <span style="font-weight: 700;">if</span> ExitOnSyntaxCheck <span style="font-weight: 700;">and</span> syntaxcheck <span style="font-weight: 700;">then</span> <span style="font-weight: 700;">return</span> <span style="font-weight: 700;">end</span> | |
− | + | <span style="font-weight: 700;">local</span> le = LineEnd | |
− | + | <span style="font-weight: 700;">local</span> name = parameters | |
− | + | <span style="font-weight: 700;">if</span> name == <span style="font-weight: 700;">nil</span> <span style="font-weight: 700;">then</span> | |
− | + | <span style="font-weight: 700;">return</span> <span style="font-weight: 700;">nil</span>, t(<span style="color: rgb(136, 0, 0);">'Wrong number of parameters, no "name", for "registerLabel" custom AA command.'</span>) | |
− | + | <span style="font-weight: 700;">end</span> | |
− | + | <span style="font-weight: 700;">local</span> str = <span style="color: rgb(136, 0, 0);">'label('</span> .. name .. <span style="color: rgb(136, 0, 0);">')'</span> .. le | |
− | + | str = str .. <span style="color: rgb(136, 0, 0);">'registerSymbol('</span> .. name .. <span style="color: rgb(136, 0, 0);">')'</span> | |
− | + | <span style="font-weight: 700;">return</span> str | |
<span style="font-weight: 700;">end</span> | <span style="font-weight: 700;">end</span> | ||
<span style="font-weight: 700;">local</span> <span style="font-weight: 700;">function</span> <span style="color: rgb(136, 0, 0); font-weight: 700;">unregisterLabel</span>(parameters, syntaxcheck) | <span style="font-weight: 700;">local</span> <span style="font-weight: 700;">function</span> <span style="color: rgb(136, 0, 0); font-weight: 700;">unregisterLabel</span>(parameters, syntaxcheck) | ||
− | + | <span style="color: rgb(136, 136, 136);">---- unregisterLabel(name)</span> | |
− | + | <span style="font-weight: 700;">if</span> ExitOnSyntaxCheck <span style="font-weight: 700;">and</span> syntaxcheck <span style="font-weight: 700;">then</span> <span style="font-weight: 700;">return</span> <span style="font-weight: 700;">end</span> | |
− | + | <span style="font-weight: 700;">local</span> name = parameters | |
− | + | <span style="font-weight: 700;">if</span> name == <span style="font-weight: 700;">nil</span> <span style="font-weight: 700;">then</span> | |
− | + | <span style="font-weight: 700;">return</span> <span style="font-weight: 700;">nil</span>, t(<span style="color: rgb(136, 0, 0);">'Wrong number of parameters, no "name", for "unregisterLabel" custom AA command.'</span>) | |
− | + | <span style="font-weight: 700;">end</span> | |
− | + | <span style="font-weight: 700;">local</span> str = <span style="color: rgb(136, 0, 0);">'unregisterSymbol('</span> .. name .. <span style="color: rgb(136, 0, 0);">')'</span> | |
− | + | <span style="font-weight: 700;">return</span> str | |
<span style="font-weight: 700;">end</span> | <span style="font-weight: 700;">end</span> | ||
Line 150: | Line 150: | ||
local function split(s, delimiter) | local function split(s, delimiter) | ||
− | + | result = {} | |
− | + | for match in (s .. delimiter):gmatch('(.-)' .. delimiter) do | |
− | + | table.insert(result, match) | |
− | + | end | |
− | + | return result | |
end | end | ||
function repeteBytes(parameters, syntaxcheck) | function repeteBytes(parameters, syntaxcheck) | ||
− | + | ---- repeteBytes(numberOfByte, arrayOfBytes) | |
− | + | if ExitOnSyntaxCheck and syntaxcheck then return end | |
− | + | local le = LineEnd | |
− | + | local args = split(parameters, ',') | |
− | + | local numberOfBytes = args[1] | |
− | + | if numberOfBytes == nil then | |
− | + | return nil, t('Wrong number of parameters, no "numberOfBytes", for "repeteBytes" custom AA command.') | |
− | + | end | |
− | + | local arrayOfBytes = args[2] | |
− | + | if arrayOfBytes == nil then | |
− | + | return nil, t('Wrong number of parameters, no "arrayOfBytes", for "repeteBytes" custom AA command.') | |
− | + | end | |
− | + | arrayOfBytes = arrayOfBytes:upper() | |
− | + | if arrayOfBytes:find('#') or arrayOfBytes:find('(INT)') | |
− | + | or arrayOfBytes:find('(FLOAT)') or arrayOfBytes:find('(DOUBLE)') then | |
− | + | return nil, t('Wrong format of parameters, "arrayOfBytes" must be in hexadecimal format, for "repeteBytes" custom AA command.') | |
− | + | end | |
− | + | local bc = (#arrayOfBytes) / 2 | |
− | + | numberOfBytes = numberOfBytes:upper():gsub(' ', '') | |
− | + | if numberOfBytes:sub(1, 5) == '(INT)' then | |
− | + | numberOfBytes = tonumber(numberOfBytes:sub(6)) | |
− | + | elseif numberOfBytes:sub(1, 1) == '#' then | |
− | + | numberOfBytes = tonumber(numberOfBytes:sub(2)) | |
− | + | elseif numberOfBytes:sub(1, 1) == '$' then | |
− | + | numberOfBytes = tonumber(numberOfBytes:sub(2), 16) | |
− | + | elseif numberOfBytes:sub(1, 2) == '0X' then | |
− | + | numberOfBytes = tonumber(numberOfBytes:sub(3), 16) | |
− | + | else | |
− | + | numberOfBytes = tonumber(numberOfBytes, 16) | |
− | + | end | |
− | + | if type(numberOfBytes) ~= 'number' then | |
− | + | return nil, t('Could not parse "numberOfBytes" for "repeteBytes" custom AA command.') | |
− | + | end | |
− | + | local str = '' | |
− | + | for i = 1, numberOfBytes / bc do | |
− | + | str = str .. 'db ' .. arrayOfBytes .. le | |
− | + | end | |
− | + | return str | |
end | end | ||
Line 206: | Line 206: | ||
-- registerSymbol(memTestMemory) | -- registerSymbol(memTestMemory) | ||
-- memTestMemory: | -- memTestMemory: | ||
− | -- | + | -- repeteBytes(0x64, 90 52) |
---------------------------------------------------- | ---------------------------------------------------- | ||
Line 231: | Line 231: | ||
<span style="font-weight: 700;">local</span> <span style="font-weight: 700;">function</span> <span style="color: rgb(136, 0, 0); font-weight: 700;">split</span>(s, delimiter) | <span style="font-weight: 700;">local</span> <span style="font-weight: 700;">function</span> <span style="color: rgb(136, 0, 0); font-weight: 700;">split</span>(s, delimiter) | ||
− | + | result = {} | |
− | + | <span style="font-weight: 700;">for</span> match <span style="font-weight: 700;">in</span> (s .. delimiter):gmatch(<span style="color: rgb(136, 0, 0);">'(.-)'</span> .. delimiter) <span style="font-weight: 700;">do</span> | |
− | + | <span style="color: rgb(57, 115, 0);">table</span>.insert(result, match) | |
− | + | <span style="font-weight: 700;">end</span> | |
− | + | <span style="font-weight: 700;">return</span> result | |
<span style="font-weight: 700;">end</span> | <span style="font-weight: 700;">end</span> | ||
<span style="font-weight: 700;">function</span> <span style="color: rgb(136, 0, 0); font-weight: 700;">repeteBytes</span>(parameters, syntaxcheck) | <span style="font-weight: 700;">function</span> <span style="color: rgb(136, 0, 0); font-weight: 700;">repeteBytes</span>(parameters, syntaxcheck) | ||
− | + | <span style="color: rgb(136, 136, 136);">---- repeteBytes(numberOfByte, arrayOfBytes)</span> | |
− | + | <span style="font-weight: 700;">if</span> ExitOnSyntaxCheck <span style="font-weight: 700;">and</span> syntaxcheck <span style="font-weight: 700;">then</span> <span style="font-weight: 700;">return</span> <span style="font-weight: 700;">end</span> | |
− | + | <span style="font-weight: 700;">local</span> le = LineEnd | |
− | + | <span style="font-weight: 700;">local</span> args = split(parameters, <span style="color: rgb(136, 0, 0);">','</span>) | |
− | + | <span style="font-weight: 700;">local</span> numberOfBytes = args[<span style="color: rgb(136, 0, 0);">1</span>] | |
− | + | <span style="font-weight: 700;">if</span> numberOfBytes == <span style="font-weight: 700;">nil</span> <span style="font-weight: 700;">then</span> | |
− | + | <span style="font-weight: 700;">return</span> <span style="font-weight: 700;">nil</span>, t(<span style="color: rgb(136, 0, 0);">'Wrong number of parameters, no "numberOfBytes", for "repeteBytes" custom AA command.'</span>) | |
− | + | <span style="font-weight: 700;">end</span> | |
− | + | <span style="font-weight: 700;">local</span> arrayOfBytes = args[<span style="color: rgb(136, 0, 0);">2</span>] | |
− | + | <span style="font-weight: 700;">if</span> arrayOfBytes == <span style="font-weight: 700;">nil</span> <span style="font-weight: 700;">then</span> | |
− | + | <span style="font-weight: 700;">return</span> <span style="font-weight: 700;">nil</span>, t(<span style="color: rgb(136, 0, 0);">'Wrong number of parameters, no "arrayOfBytes", for "repeteBytes" custom AA command.'</span>) | |
− | + | <span style="font-weight: 700;">end</span> | |
− | + | arrayOfBytes = arrayOfBytes:upper():gsub(<span style="color: rgb(136, 0, 0);">'0X'</span>, <span style="color: rgb(136, 0, 0);">''</span>):gsub(<span style="color: rgb(136, 0, 0);">'$'</span>, <span style="color: rgb(136, 0, 0);">''</span>):gsub(<span style="color: rgb(136, 0, 0);">' '</span>, <span style="color: rgb(136, 0, 0);">''</span>) | |
− | + | <span style="font-weight: 700;">if</span> arrayOfBytes:find(<span style="color: rgb(136, 0, 0);">'#'</span>) <span style="font-weight: 700;">or</span> arrayOfBytes:find(<span style="color: rgb(136, 0, 0);">'(INT)'</span>) | |
− | + | <span style="font-weight: 700;">or</span> arrayOfBytes:find(<span style="color: rgb(136, 0, 0);">'(FLOAT)'</span>) <span style="font-weight: 700;">or</span> arrayOfBytes:find(<span style="color: rgb(136, 0, 0);">'(DOUBLE)'</span>) <span style="font-weight: 700;">then</span> | |
− | + | <span style="font-weight: 700;">return</span> <span style="font-weight: 700;">nil</span>, t(<span style="color: rgb(136, 0, 0);">'Wrong format of parameters, "arrayOfBytes" must be in hexadecimal format, for "repeteBytes" custom AA command.'</span>) | |
− | + | <span style="font-weight: 700;">end</span> | |
− | + | <span style="font-weight: 700;">local</span> bc = (#arrayOfBytes) / <span style="color: rgb(136, 0, 0);">2</span> | |
− | + | numberOfBytes = numberOfBytes:upper():gsub(<span style="color: rgb(136, 0, 0);">' '</span>, <span style="color: rgb(136, 0, 0);">''</span>) | |
− | + | <span style="font-weight: 700;">if</span> numberOfBytes:sub(<span style="color: rgb(136, 0, 0);">1</span>, <span style="color: rgb(136, 0, 0);">5</span>) == <span style="color: rgb(136, 0, 0);">'(INT)'</span> <span style="font-weight: 700;">then</span> | |
− | + | numberOfBytes = <span style="color: rgb(57, 115, 0);">tonumber</span>(numberOfBytes:sub(<span style="color: rgb(136, 0, 0);">6</span>)) | |
− | + | <span style="font-weight: 700;">elseif</span> numberOfBytes:sub(<span style="color: rgb(136, 0, 0);">1</span>, <span style="color: rgb(136, 0, 0);">1</span>) == <span style="color: rgb(136, 0, 0);">'#'</span> <span style="font-weight: 700;">then</span> | |
− | + | numberOfBytes = <span style="color: rgb(57, 115, 0);">tonumber</span>(numberOfBytes:sub(<span style="color: rgb(136, 0, 0);">2</span>)) | |
− | + | <span style="font-weight: 700;">elseif</span> numberOfBytes:sub(<span style="color: rgb(136, 0, 0);">1</span>, <span style="color: rgb(136, 0, 0);">1</span>) == <span style="color: rgb(136, 0, 0);">'$'</span> <span style="font-weight: 700;">then</span> | |
− | + | numberOfBytes = <span style="color: rgb(57, 115, 0);">tonumber</span>(numberOfBytes:sub(<span style="color: rgb(136, 0, 0);">2</span>), <span style="color: rgb(136, 0, 0);">16</span>) | |
− | + | <span style="font-weight: 700;">elseif</span> numberOfBytes:sub(<span style="color: rgb(136, 0, 0);">1</span>, <span style="color: rgb(136, 0, 0);">2</span>) == <span style="color: rgb(136, 0, 0);">'0X'</span> <span style="font-weight: 700;">then</span> | |
− | + | numberOfBytes = <span style="color: rgb(57, 115, 0);">tonumber</span>(numberOfBytes:sub(<span style="color: rgb(136, 0, 0);">3</span>), <span style="color: rgb(136, 0, 0);">16</span>) | |
− | + | <span style="font-weight: 700;">else</span> | |
− | + | numberOfBytes = <span style="color: rgb(57, 115, 0);">tonumber</span>(numberOfBytes, <span style="color: rgb(136, 0, 0);">16</span>) | |
− | + | <span style="font-weight: 700;">end</span> | |
− | + | <span style="font-weight: 700;">if</span> <span style="color: rgb(57, 115, 0);">type</span>(numberOfBytes) ~= <span style="color: rgb(136, 0, 0);">'number'</span> <span style="font-weight: 700;">then</span> | |
− | + | <span style="font-weight: 700;">return</span> <span style="font-weight: 700;">nil</span>, t(<span style="color: rgb(136, 0, 0);">'Could not parse "numberOfBytes" for "repeteBytes" custom AA command.'</span>) | |
− | + | <span style="font-weight: 700;">end</span> | |
− | + | <span style="font-weight: 700;">local</span> str = <span style="color: rgb(136, 0, 0);">''</span> | |
− | + | <span style="font-weight: 700;">for</span> i = <span style="color: rgb(136, 0, 0);">1</span>, numberOfBytes / bc <span style="font-weight: 700;">do</span> | |
− | + | str = str .. <span style="color: rgb(136, 0, 0);">'db '</span> .. arrayOfBytes .. le | |
− | + | <span style="font-weight: 700;">end</span> | |
− | + | <span style="font-weight: 700;">return</span> str | |
<span style="font-weight: 700;">end</span> | <span style="font-weight: 700;">end</span> | ||
Line 287: | Line 287: | ||
<span style="color: rgb(136, 136, 136);">-- registerSymbol(memTestMemory)</span> | <span style="color: rgb(136, 136, 136);">-- registerSymbol(memTestMemory)</span> | ||
<span style="color: rgb(136, 136, 136);">-- memTestMemory:</span> | <span style="color: rgb(136, 136, 136);">-- memTestMemory:</span> | ||
− | <span style="color: rgb(136, 136, 136);">-- | + | <span style="color: rgb(136, 136, 136);">-- repeteBytes(0x64, 90 52)</span> |
<span style="color: rgb(136, 136, 136);">----------------------------------------------------</span> | <span style="color: rgb(136, 136, 136);">----------------------------------------------------</span> | ||
Revision as of 19:42, 13 January 2018
function registerAutoAssemblerCommand(Command, Function(parameters, syntaxcheckonly) )
Registers an auto assembler command to call the specified function. The command will be replaced by the string this function returns when executed. The function can be called twice. Once for syntax check and symbol lookup (1), and the second time for actual execution by the assembler (2) if it has not been removed in phase 1.
If the function returns nil, and as secondary parameter a string, this will make the auto assembler fail with that error.
- Note: You will only get a message pop-up if the Auto Assembler is preforming a syntax check.
The Auto Assembler form syntax highlighter will highlight the registered command.
Contents
Function Parameters
Parameter | Type | Description |
---|---|---|
Command | String | The command string it self |
Function | Function | The Lua function to be called when the command string is found in an Auto Assembler script |
Callback Function Parameters
Parameter | Type | Description |
---|---|---|
parameters | String | This will be the string between parenthesis after the command string |
syntaxcheckonly | Boolean | This will be true if the script is running though a syntax check |
Callback Function Return(s)
Parameter | Type | Description |
---|---|---|
ReplaceString | String or Nil | This will replace the commands line in the Auto Assembler script, can be a multi-line string, if Nil the commands line is removed |
ErrorMessage | String or Nil (OPTIONAL) | if the ReplaceString is nil and this is set to a string then, this will raise an error with the given error message for the command |
Examples
Example 1
local t = translate local LineEnd = '\r\n' local ExitOnSyntaxCheck = false local function registerLabel(parameters, syntaxcheck) ---- registerLabel(name) if ExitOnSyntaxCheck and syntaxcheck then return end local le = LineEnd local name = parameters if name == nil then return nil, t('Wrong number of parameters, no "name", for "registerLabel" custom AA command.') end local str = 'label(' .. name .. ')' .. le str = str .. 'registerSymbol(' .. name .. ')' return str end local function unregisterLabel(parameters, syntaxcheck) ---- unregisterLabel(name) if ExitOnSyntaxCheck and syntaxcheck then return end local name = parameters if name == nil then return nil, t('Wrong number of parameters, no "name", for "unregisterLabel" custom AA command.') end local str = 'unregisterSymbol(' .. name .. ')' return str end registerAutoAssemblerCommand('registerLabel', registerLabel) ---- Register the command registerAutoAssemblerCommand('unregisterLabel', unregisterLabel) ---- Register the command ---- Usage in an Auto Assembler script ---------------------------------------------------- -- alloc(memTestMemory, 0x400) -- registerSymbol(memTestMemory) -- registerLabel(memTestMemoryEND) -- memTestMemory: -- db 90 90 90 90 -- memTestMemoryEND: ----------------------------------------------------
Example 2
local t = translate local LineEnd = '\r\n' local ExitOnSyntaxCheck = false local function split(s, delimiter) result = {} for match in (s .. delimiter):gmatch('(.-)' .. delimiter) do table.insert(result, match) end return result end function repeteBytes(parameters, syntaxcheck) ---- repeteBytes(numberOfByte, arrayOfBytes) if ExitOnSyntaxCheck and syntaxcheck then return end local le = LineEnd local args = split(parameters, ',') local numberOfBytes = args[1] if numberOfBytes == nil then return nil, t('Wrong number of parameters, no "numberOfBytes", for "repeteBytes" custom AA command.') end local arrayOfBytes = args[2] if arrayOfBytes == nil then return nil, t('Wrong number of parameters, no "arrayOfBytes", for "repeteBytes" custom AA command.') end arrayOfBytes = arrayOfBytes:upper():gsub('0X', ''):gsub('$', ''):gsub(' ', '') if arrayOfBytes:find('#') or arrayOfBytes:find('(INT)') or arrayOfBytes:find('(FLOAT)') or arrayOfBytes:find('(DOUBLE)') then return nil, t('Wrong format of parameters, "arrayOfBytes" must be in hexadecimal format, for "repeteBytes" custom AA command.') end local bc = (#arrayOfBytes) / 2 numberOfBytes = numberOfBytes:upper():gsub(' ', '') if numberOfBytes:sub(1, 5) == '(INT)' then numberOfBytes = tonumber(numberOfBytes:sub(6)) elseif numberOfBytes:sub(1, 1) == '#' then numberOfBytes = tonumber(numberOfBytes:sub(2)) elseif numberOfBytes:sub(1, 1) == '$' then numberOfBytes = tonumber(numberOfBytes:sub(2), 16) elseif numberOfBytes:sub(1, 2) == '0X' then numberOfBytes = tonumber(numberOfBytes:sub(3), 16) else numberOfBytes = tonumber(numberOfBytes, 16) end if type(numberOfBytes) ~= 'number' then return nil, t('Could not parse "numberOfBytes" for "repeteBytes" custom AA command.') end local str = '' for i = 1, numberOfBytes / bc do str = str .. 'db ' .. arrayOfBytes .. le end return str end registerAutoAssemblerCommand('repeteBytes', repeteBytes) ---- Register the command ---- Usage in an Auto Assembler script ---------------------------------------------------- -- alloc(memTestMemory, 0x400) -- registerSymbol(memTestMemory) -- memTestMemory: -- repeteBytes(0x64, 90 52) ---------------------------------------------------- ---- Memory View Form copy of "memTestMemory" ---------------------------------------------------- -- 0032FFFE - - ?? -- 0032FFFF - - ?? -- memTestMemory- 90 - nop -- 00330001 - 52 - push edx -- 00330002 - 90 - nop -- 00330003 - 52 - push edx -- 00330004 - 90 - nop -- ... -- 00330060 - 90 - nop -- 00330061 - 52 - push edx -- 00330062 - 90 - nop -- 00330063 - 52 - push edx -- 00330064 - 00 00 - add [eax],al ----------------------------------------------------