Task

Test whether an integer is even or odd.

There is more than one way to solve this task:

 Use the even and odd predicates, if the language provides them.
 Check the least significant digit. With binary integers, i bitwise-and 1 equals 0 iff i is even, or equals 1 iff i is odd.
 Divide i by 2. The remainder equals 0 iff i is even. The remainder equals +1 or -1 iff i is odd.
 Use modular congruences:
 i ≡ 0 (mod 2) iff i is even.
 i ≡ 1 (mod 2) iff i is odd.


 
// Input number; output its residue mod 2
       INP        // read input into acc
       BRZ write  // input = 0 is special case
loop   SUB k2     // keep subtracting 2 from acc
       BRZ write  // if acc = 0, input is even
       BRP loop   // if acc > 0, loop back
                  // (BRP branches if acc >= 0, but we've dealt with acc = 0)
       LDA k1     // if acc < 0, input is odd
write  OUT        // output 0 or 1
       HLT        // halt
k1     DAT 1      // constant 1
k2     DAT 2      // constant 2
// end
 