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 iftruncate(x - y) > x.y < 0, then overflow iftruncate(x - y) < x.Here
truncate(x)means the truncated 8-bit signed value ofx.Then the code may look like this:
As an optimization measure, the two
mov a, binstructions can be replaced with a singlemov a, bjust beforejm else.