input = read_line(); /* Get input */ phase_1(input); /* Run the phase */ phase_defused(); /* Drat! They figured it out! * Let me know how they did it. */ printf("Phase 1 defused. How about the next one?\n");
CMP subtracts the operands and sets the flags. Namely, it sets the zero flag(ZF) if the difference is zero (operands are equal).
TEST sets the zero flag, ZF, when the result of the AND operation is zero. If two operands are equal, their bitwise AND is zero when both are zero. TEST also sets the sign flag, SF, when the most significant bit is set in the result, and the parity flag, PF, when the number of set bits is even.
JE [Jump if Equals] tests the zero flag and jumps if the flag is set. JE is an alias of JZ
Two's complement is executed by 1) inverting (i.e. flipping) all bits, then 2) adding a place value of 1 to the inverted number. For example, say the number +6 is of interest. +6 in binary is 0110 (the leftmost MSB is needed for the sign; positive 6 is not 110 because it would be interpreted as -2). Step one is to flip all bits, yielding 1001. Step two is to add the place value one to the flipped number, which yields 1010. To verify that 1010 indeed has a value of -6, remember that two's complement makes the most significant bit represent a negative place value, then add the place values: 1010 = 1(-23)+0(22)+1(21)+0(20) = 1(-8) + 0 + 1(2) + 0 = -6.