Lua:getPreviousOpcode

From Cheat Engine
(Redirected from getPreviousOpcode)
Jump to navigation Jump to search

<> 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