I want to branch if x-y overflows.
I came up with idea to store x-y into register A , store 0 into register B and compare these two registers.
Unlike 8086, 8080 doesn't have an OF flag or jo / jno instructions.
x db
y db
lda x
mov b,a
lda y
sub b
mvi b,0
cmp b
jp overflow
notOverFlow HLT
overflow HLT
It works with x=128 , y=127 but not with values x=0, y=0.
If no overflow means the arithmetic result is representable as an 8-bit signed value, then the following rules should apply:
y = 0, then no overflow.y > 0, then overflow if truncate(x - y) > x.y < 0, then overflow if truncate(x - y) < x.Here truncate(x) means the truncated 8-bit signed value of x.
Then the code may look like this:
lda y ; Load y.
mov b, a
lda x ; Load x.
mov c, a
sub b ; No overflow if truncate(x - y) = x, that is, y = 0.
cmp c
jz no_overflow
jm else ; Jump if truncate(x - y) < x.
; At this point y != 0 and truncate(x - y) > x.
mov a, b ; Overflow if y > 0.
ana a
jp overflow
no_overflow:
...
; At this point y != 0 and truncate(x - y) < x.
else:
mov a, b ; Overflow if y < 0.
ana a
jp no_overflow
overflow:
...
As an optimization measure, the two mov a, b instructions can be replaced with a single mov a, b just before jm else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With