Difference between revisions of "Lua:getPreviousOpcode"

From Cheat Engine
Jump to navigation Jump to search
(Major overhaul of the post.)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''function''' getPreviousOpcode(''Address'') ''':''' integer
+
[[Category:Lua]]
 +
{{CodeBox|'''function''' getPreviousOpcode(''address'') ''':''' integer}}
  
Returns the address of the previous opcode (this is just an estimated guess).
+
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%" cellpadding="5%" cellspacing="0" border="0"
 
 
!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
+
|address
|[[CEAddressString]] or Integer
+
|Integer/String
|The address of the instruction
+
|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>
  
== See also ==
+
{{LuaSeeAlso}}
* [[Lua]]
 
* [[Help_File:Script engine|Script engine]]
 
  
=== Related Functions ===
+
{{Assembly}}
* [[disassemble]]
 
* [[getInstructionSize]]
 
* [[splitDisassembledString]]
 
* [[AOBScan]]
 
* [[autoAssemble]]
 
* [[readBytes]]
 
* [[readPointer]]
 
* [[writeBytes]]
 
* [[readBytesLocal]]
 
* [[readPointerLocal]]
 
* [[writeBytesLocal]]
 
* [[wordToByteTable]]
 
* [[dwordToByteTable]]
 
* [[qwordToByteTable]]
 
* [[floatToByteTable]]
 
* [[doubleToByteTable]]
 
* [[stringToByteTable]]
 
* [[wideStringToByteTable]]
 
* [[byteTableToWord]]
 
* [[byteTableToDword]]
 
* [[byteTableToQword]]
 
* [[byteTableToFloat]]
 
* [[byteTableToDouble]]
 
* [[byteTableToString]]
 
* [[byteTableToWideString]]
 

Latest revision as of 19:28, 25 June 2026

<> Lua API Reference

function getPreviousOpcode(address) : integer

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

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Assembler and Disassembler Related Lua Functions

Auto assembler, disassembly helpers, instruction size lookup, and assembler callbacks