Assignment 2
Since you've all received Assignment 2 by now, I will start posting information about this assignment on here. Please check back regularly as I will keep updating this page.
if(product & 0x1)...
There seems to be a lot of confusion here, mainly about how to translate this. In C, the & means bitwise AND. 0x1 is a bitmask, which we use to check bit-0 (the first bit, 0 or 1) in product. Becase 0x1 only has 1 bit, when you do a bitwise AND, the rest of the bits in product are not compared. That's because 0x1 is equivalent to 0x0000000000001 (as many preceding 0's as you want), and a bitwise AND with 0 is just 0. Back to the condition, if the bit-0 of product is 1, then the condition is true. If the bit-0 of product is 0, then the condition is false.
So in ARMv8 assembly, you have a few options to make this comparison. You could do an AND, and then compare the result with CMP, but it's not as efficient. Instead, you should use ANDS or TST.
ANDS (AND Set): same as AND, but the Z (zero) and N (sign) flags are set depending on the result of the ANDS operation
ANDS wzr, product, 0x1 // bitwise AND of product and 0x1
b.eq productEndsWithZero // b.eq doesn't mean true, but rather means the Z flag is set.
// The Z flag is set here if the ANDS operation results in zero (0).
// That means the Z flag is set (b.eq) when bit-0 of product is 0.
b.ne productEndsWithOne // This is not necessary if you just let your code fall through (skipping the b.eq above),
// but just to show you... b.ne (Z flag is not set) when bit-0 of product is 1.
// If bit-0 of product is 1, then ANDS product, 0x1 will product 1 (Z flag is not set).
TST (Test)
TST product, 0x1 // TST is an alias for ANDS wzr, Wn, Wm.
ANDS wzr, product, 0x1 // If you make the zero register (wzr) the destination, nothing is saved, since wzr is read-only.
In the C code, you will see this line which uses a ternary operator:
negative = multiplier < 0 ? TRUE: FALSE;
This syntax (? :) is equivalent to the following:
if(multiplier < 0) negative = TRUE;
else negative = FALSE;