I have this repeating iterative process in 16-bit x86 asm
rec_loop:
mul bx ; constant value of 0x11bf
add ax, 0x4743
loop rec_loop
it's running several times but for our case let's say it runs 3 times, now after it is finished I get dx:ax and need to recover the initial ax of this program (the initial dx is 0).
From first sight, we can see that information about dx is being lost in this process because mul override the last dx with a new one.
I'm looking for a way to reverse this process if it's even possible, of course with the assumption the code above can't be changed.
If I forgot to explain something or didn't give enough details about the problem just tell me and I'll answer.
The computation xn+1 := (a * xn + c) mod m, with a =
0x11bf, c =0x4743, m= 216 forms a pseudo-random number generator. Specifically, a linear congruential generator with period 211. So clearly not a high-quality PRNG, but it may be sufficient for use cases that merely require data that looks kind of random. I note that the question does not state the context in which this code is used.The sequence of xi (here successively generated in
AX) can be reversed by using the modular multiplicative inverse ainverse like so: xn-1 = ainverse* (xn - c) mod m. In this case the modular multiplicative inverse is0xde3f.Note that the value of
DXis not needed in the backwards computation, and at any point in the sequence can readily be determined fromAXif needed. The determination of the multiplicative inverse below uses a naive method based on the definition. For larger operand sizes this is hugely inefficient, one would want to use the Extended Euclidean algorithm instead.Simple C99 code for experimenting with the computation of the multiplicative inverse and the reversing of the sequence of values in
AX: