Difference between revisions of "Lua:getPreviousOpcode"
Jump to navigation
Jump to search
(→Related Functions) |
(Major overhaul of the post.) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
[[Category:Lua]] | [[Category:Lua]] | ||
| − | '''function''' getPreviousOpcode('' | + | {{CodeBox|'''function''' getPreviousOpcode(''address'') ''':''' integer}} |
| − | Returns the address of the | + | Returns the estimated address of the instruction before the given address. |
| − | === Function Parameters === | + | ===Function Parameters=== |
| − | + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | |
| − | {|width="85%" cellpadding="10 | ||
!align="left"|Parameter | !align="left"|Parameter | ||
!align="left"|Type | !align="left"|Type | ||
!style="width: 80%;background-color:white;" align="left"|Description | !style="width: 80%;background-color:white;" align="left"|Description | ||
|- | |- | ||
| − | | | + | |address |
| − | | | + | |Integer/String |
| − | |The address | + | |The address to estimate the previous opcode for. |
|} | |} | ||
| + | ===Returns=== | ||
| + | Integer — The estimated address of the previous opcode. | ||
| + | |||
| + | ===Description=== | ||
| + | getPreviousOpcode attempts to find the instruction that comes before the given address. | ||
| + | |||
| + | Because x86/x64 instructions have variable length, finding the previous instruction by only knowing the current address is not always exact. The returned address should be treated as an estimated guess. | ||
| + | |||
| + | This function is useful when working with disassembly, nearby instructions, or when trying to inspect code before a known address. | ||
| + | |||
| + | ===Examples=== | ||
| + | |||
| + | ====Get the previous opcode address==== | ||
| + | <syntaxhighlight lang="lua" line highlight="2"> | ||
| + | local address = getAddress("game.exe+12345") | ||
| + | local previous = getPreviousOpcode(address) | ||
| + | |||
| + | print(string.format("%X", previous)) | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ====Disassemble the previous opcode==== | ||
| + | <syntaxhighlight lang="lua" line highlight="2"> | ||
| + | local address = getAddress("game.exe+12345") | ||
| + | local previous = getPreviousOpcode(address) | ||
| + | |||
| + | print(disassemble(previous)) | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ====Compare current and previous instruction==== | ||
| + | <syntaxhighlight lang="lua" line highlight="2"> | ||
| + | local address = getAddress("game.exe+12345") | ||
| + | local previous = getPreviousOpcode(address) | ||
| + | |||
| + | print("Previous: " .. disassemble(previous)) | ||
| + | print("Current : " .. disassemble(address)) | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ====Walk backwards through nearby instructions==== | ||
| + | <syntaxhighlight lang="lua" line highlight="5"> | ||
| + | local address = getAddress("game.exe+12345") | ||
| + | |||
| + | for i = 1, 5 do | ||
| + | print(string.format("%X", address) .. " - " .. disassemble(address)) | ||
| + | address = getPreviousOpcode(address) | ||
| + | end | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ====Use getPreviousOpcode with getInstructionSize==== | ||
| + | <syntaxhighlight lang="lua" line highlight="2"> | ||
| + | local address = getAddress("game.exe+12345") | ||
| + | local previous = getPreviousOpcode(address) | ||
| + | local size = getInstructionSize(previous) | ||
| + | |||
| + | print("Previous instruction size: " .. tostring(size)) | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ====Check the result before using it==== | ||
| + | <syntaxhighlight lang="lua" line highlight="2"> | ||
| + | local address = getAddress("game.exe+12345") | ||
| + | local previous = getPreviousOpcode(address) | ||
| + | |||
| + | if previous ~= nil and previous ~= 0 then | ||
| + | print(disassemble(previous)) | ||
| + | end | ||
| + | </syntaxhighlight> | ||
{{LuaSeeAlso}} | {{LuaSeeAlso}} | ||
| − | + | {{Assembly}} | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Latest revision as of 19:28, 25 June 2026
Returns the estimated address of the instruction before the given address.
Function Parameters[edit]
| Parameter | Type | Description |
|---|---|---|
| address | Integer/String | The address to estimate the previous opcode for. |
Returns[edit]
Integer — The estimated address of the previous opcode.
Description[edit]
getPreviousOpcode attempts to find the instruction that comes before the given address.
Because x86/x64 instructions have variable length, finding the previous instruction by only knowing the current address is not always exact. The returned address should be treated as an estimated guess.
This function is useful when working with disassembly, nearby instructions, or when trying to inspect code before a known address.
Examples[edit]
Get the previous opcode address[edit]
1 local address = getAddress("game.exe+12345")
2 local previous = getPreviousOpcode(address)
3
4 print(string.format("%X", previous))
Disassemble the previous opcode[edit]
1 local address = getAddress("game.exe+12345")
2 local previous = getPreviousOpcode(address)
3
4 print(disassemble(previous))
Compare current and previous instruction[edit]
1 local address = getAddress("game.exe+12345")
2 local previous = getPreviousOpcode(address)
3
4 print("Previous: " .. disassemble(previous))
5 print("Current : " .. disassemble(address))
Walk backwards through nearby instructions[edit]
1 local address = getAddress("game.exe+12345")
2
3 for i = 1, 5 do
4 print(string.format("%X", address) .. " - " .. disassemble(address))
5 address = getPreviousOpcode(address)
6 end
Use getPreviousOpcode with getInstructionSize[edit]
1 local address = getAddress("game.exe+12345")
2 local previous = getPreviousOpcode(address)
3 local size = getInstructionSize(previous)
4
5 print("Previous instruction size: " .. tostring(size))
Check the result before using it[edit]
1 local address = getAddress("game.exe+12345")
2 local previous = getPreviousOpcode(address)
3
4 if previous ~= nil and previous ~= 0 then
5 print(disassemble(previous))
6 end