panraven

ce_asmblock.lua

Jul 23rd, 2021 (edited)
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.35 KB | None | 0 0
  1. local function asmBlock(addr, asm, ctx, ceself)
  2.   ceself = not not ceself
  3.   addr = type(addr)~='number' and getAddressSafe(addr, ceself) or addr
  4.   if type(addr)~='number' or addr==0 then return nil,'invalid addr'end
  5.   ctx = ctx or {}
  6.   ctx.RID, ctx._Start = '_'..math.random(0x10000,0x99999), addr
  7.   asm = string.format([[
  8. unregisterSymbol(<!RID>)
  9. label(<!RID>)
  10. <!_Start>:
  11. %s
  12. <!RID>:
  13. registerSymbol(<!RID>)
  14.   ]],asm)
  15.   asm = asm:gsub("<!([_#%.%a][_#%.%w]*)>",function(k)
  16.       local v = ctx[k]
  17.       if type(v)=='number' and math.tointeger(v)then
  18.         return v<0 and string.format('-%X',-v)or string.format('%X',v)
  19.       else
  20.         return tostring(v)
  21.       end
  22.     end)
  23.   local ok = not ceself and autoAssemble(asm) or ceself and autoAssemble(asm,true)
  24.   local endAddr = ok and getAddressSafe(ctx.RID, ceself),unregisterSymbol(ctx.RID)
  25.   if not endAddr then return nil,'failed asmBlock'end
  26.   local size = endAddr-addr
  27.   if ceself then
  28.     return readBytesLocal(addr,size,true),size
  29.   else
  30.     return readBytes(addr,size,true),size
  31.   end
  32. end
  33. ---- test
  34. autoAssemble("globalalloc(___,$1000)")
  35. print(byteTableToString(asmBlock('___', [[
  36. mov    rax,1000
  37. mov    rbx,<!.TestVar>
  38. mov    rcx,<!TestVar>
  39. ret
  40. ]], {['.TestVar']=0x123456789, TestVar=0x987654321} ) or {0}):
  41.   gsub('.',function(c)return string.format(' %02X',c:byte())end):sub(2))
Add Comment
Please, Sign In to add comment