1

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.

1 Answers1

0

Here's what happens:

  1. mvi a, 0ah

    a is set 10.

  2. mvi e, 05h

    e is set to 5.

  3. sub e

    a is set to 10 - 5, that is, 5. The sign flag is dropped, meaning the result is not negative.

  4. jp subtractionLoop

    Jumps back to sub e.

  5. sub e (again)

    a is set to 5 - 5 = 0. The sign flag is still dropped.

  6. jp subtractionLoop

    Moves control back to sub e once again.

  7. sub e

    a is 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 a register you are looking at when you see 251, and not the value of the b register.

Note that the jz jump will never actually pass control to zero: as zero result in a would leave the sign flag dropped and so the preceding jp instruction will do its job jumping to the beginning of the cycle.

Ivan Kosarev
  • 304
  • 1
  • 7