Been twisting my head over this little assembly problem for quite some time without much progress. I have a nested conditional that I want to calculate as fast as possible and in constant time.
Assuming three 8-bit integers a, b, and d, I want to calculate the following:
Code: Select all
if (abs(a - b) > d) {
if (a > b) return a + d;
else return a - d;
} else return b;
Code: Select all
if (b > a) {
if (b - a > d) return a + d;
else return b;
} else {
if (a - b > d) return a - d;
else return b;
}
1. In the second version, (b - a > d) could be replaced by (a + d <= b) or probably (a + d < b), so the return value for the true branch would already be calculated in the condition.
2. This looks kind of interesting:
Code: Select all
; b is in reg A, a is in reg B, d is in reg D
sub b
sbc a,a
xor d
; if (b > a) return d else return -d - 1