This is my code:
init:
; expected to go in "zero" part
mvi a, 0ah
mvi e, 05h
; content of reg b is "251"
; expected to go in "zero" part
;mvi a, 0ah
;mvi e, 0ah
; if followed two are commented out content of reg b is "254"
; expected to go in "minus" part
;mvi a, 0ah
;mvi e, 03h
; if followed two are commented out content of reg b is "254"
; expected to go in "minus" part
;mvi a,0ah
;mvi e,0bh
; if followed two are commented out content of reg b is "255"
subtractionLoop:
sub e
jp subtractionLoop
jz zero
jm minus
minus:
mvi b, 0ffh
; print value as 255 to see it comes within "minus" part
; the part means last result is minus, so we can get remainder by adding
; content of reg E only one time
hlt
zero:
mvi b, 0bh
; print value as 11 to see it comes within "zero" part
hlt
I simply try to achieve simple division, but I get different and interesting results as you read on comments(;).
My idea is as following:
So long as the dividend is positive, subtractionLoop goes on subtraction. If it hits to 0, go to the zero part. Otherwise, go to the minus part.
Where is/are my mistake/s?
The jumps don't seem right.
Here's what happens:
mvi a, 0ahais set 10.mvi e, 05heis set to 5.sub eais set to 10 - 5, that is, 5. The sign flag is dropped, meaning the result is not negative.jp subtractionLoopJumps back to
sub e.sub e(again)ais set to 5 - 5 = 0. The sign flag is still dropped.jp subtractionLoopMoves control back to
sub eonce again.sub eais set to 0 - 5 = -5, which unsigned value is 256 - 5 = 251. The sign flag is raised this time.So my guess is that it is the value of the
aregister you are looking at when you see251, and not the value of thebregister.Note that the
jzjump will never actually pass control tozero:as zero result inawould leave the sign flag dropped and so the precedingjpinstruction will do its job jumping to the beginning of the cycle.