Search code examples
c++macrosreverse-engineeringida

IDA Pro - rewriting HIBYTE, LOBYTE and __OFSUB__ macros to C++


I was able to hook one function and now I'm trying to rewrite it's code, but I have issues with translating some macros generated by IDA Pro's pseudocode

 LOBYTE(v8) = v8 & 3;  
 LOBYTE(v12) = 0;
 HIBYTE(v12) = *result;
 v21 = __OFSUB__(v24 + 1, 30); 

LOBYTE and HIBYTE are detected by Visual Studio but when I copy-paste them I get this error

Error (active) E0137 expression must be a modifiable lvalue

I tried to rewrite it to something like this

LOBYTE(v8); v8 = v8 & 3;

and then the error dissappears, but it doesn't seem to work fine. As for offsub, it is not detected at all as a valid macro.

Do you have any ideas what should I do?


Solution

  • LOBYTE and HIBYTE are macros that do some bit shifting logic so you can extract specific values from an unsigned short. So, for example if you had an unsigned short with value 0xAB93, you could get the specific bytes as so:

    unsigned char lo = LOBYTE(0xAB93);
    unsigned char hi = HIBYTE(0xAB93);
    

    lo would hold the value 0x93 and hi would hold the value 0xAB

    You want to use a separate macro to combine the values. For example:

    unsigned short both = MAKEWORD(lo,hi);