This is a question I saw in an interview :
A,B are non-negative numbers and you need to return C=|A-B| where you have only the following instructions:
A solution with just the instructions allowed may be this (though not elegant). Where the pseudo registers a and b holds the operands and the pseudo register c the result (which is initially zero as stated).
_dec_a:
dec a
inc a
jnz _dec_b
;a is zero here
_a_zero_dec_b:
dec b
inc b
jnz _a_zero_b_non_zero
;a and b are zero here
;;C is the result
inc c
jnz _result
_a_zero_b_non_zero:
dec b
inc c
jnz _a_zero_dec_b
;CANNOT FALL HERE
_dec_b:
dec b
inc b
jnz _subtract
;b is zero here
_b_zero_dec_a:
dec a
inc a
jnz _b_zero_a_non_zero
;a and b are zero here
;; C is the result
inc c
jnz _result
_b_zero_a_non_zero:
dec a
inc c
jnz _b_zero_dec_a
;CANNOT FALL HERE
_subtract:
dec a
dec b
jnz _dec_a
;Result
_result:
dec c