0

I am trying to implement some assembly code with python. I have the following code -

00DF26C7  |. B9 0C000000    MOV ECX,0C
00DF26CC  |. F7F9           IDIV ECX

and my registers are -

EAX FFFFFEB0
ECX 0000000C
EDX FFFFFFFF
EBX 003CF000
ESP 0053F858
EBP 0053F860
ESI 00000000

If EAX is a positive number, then the following python code have exactly the same output as the opcode -

int(eax / 0x0c) & 0xffffffff

but I can't make it work for a negative eax... from my understanding the idiv opcode uses EDX:EAX, so 0xFFFFFFFFFFFFFEB0 / 0x0c, but even when trying this way I get a wrong result.

Maybe someone know how to implement this opcode? P.S. the correct result is - 0xFFFFFFE4

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Meir Tolpin
  • 325
  • 2
  • 13
  • 1
    `IDIV` is signed hence `0xFFFFFFFFFFFFFEB0` means `-336` so the result is `-28` which is indeed `0xFFFFFFE4`. You will also need to take care of truncating in the proper direction if the remainder is nonzero. – Jester Oct 19 '22 at 22:34
  • Python uses arbitrary-precision integers, so you'll need to manually interpret the sign bit unless it has a way to take an integer as a 64-bit two's complement bit-pattern. Also note that `idiv` truncates towards zero, like C. IIRC, Python integer division is different. – Peter Cordes Oct 19 '22 at 22:59

0 Answers0