Trying to reverse engineer a function

后端 未结 3 805
慢半拍i
慢半拍i 2021-01-07 05:59

I\'m trying to understand assembly in x86 more. I have a mystery function here that I know returns an int and takes an int argument. So it looks l

3条回答
  •  灰色年华
    2021-01-07 06:21

    The LEA is just a left-shift by 3, and truncating the result to 32 bit (i.e. zero-extending EDI into RDI implicilty). x86-64 System V passes the first integer arg in RDI, so all of this is consistent with one int arg. LEA uses memory-operand syntax and machine encoding, but it's really just a shift-and-add instruction. Using it as part of a multiply by a constant is a common compiler optimization for x86.

    The compiler that generated this function missed an optimization here; the first mov could have been avoided with

    lea  0x0(,%rdi, 8), %eax     # n << 3 = n*8
    sub  %edi, %eax              # eax = n*7
    lea  4(%rax), %edi           # rdi = 4 + n*7
    

    But instead, the compiler got stuck on generating n*7 in %edi, probably because it applied a peephole optimization for the constant multiply too late to redo register allocation.


    mystery_util returns the bitwise AND of the low 2 bits of its arg, in the low bit, so a 0 or 1 integer value, which could also be a bool.

    (shr with no count means a count of 1; remember that x86 has a special opcode for shifts with an implicit count of 1. 8086 only has counts of 1 or cl; immediate counts were added later as an extension and the implicit-form opcode is still shorter.)

提交回复
热议问题