0

I am trying to implement division and modulo in a strictly 8-bit microcomputer via an assembly like system. The task is to get the correct result out of two unsigned 8-bit numbers A and B (B > 0) for each of the two functions (or optionally both at the same time, in the best case). The calculations I can already do are:

  • Add: x+y (optionally using carry bit)
  • Sub: x-y
  • Bitwise Nor (Invert if x == y)
  • logical shift right (optionally shifting through carry)
  • logical shift left (by adding)
  • jump if a value is zero
  • jump if a value is "negative" (but as is said, 10000000 would be a positive number in this case)
  • jump if carry out is set

Every time I am trying to find a solution, I end up failing when trying to compare numbers, other than equal (would be: x-y === 0). Every solution I have found online, requires a subtraction and then checking the sign-bit (which I don't have here).

My last attempt was rewriting this answer, but ultimately failed at the greater or equal comparison for the reason mentioned above.

It would be great if someone got an idea of how to realize this, if it is even possible, or at least give a small hint. I thought about this for hours now... Please let me know if some information is missing.

Beragrom
  • 23
  • 3
  • 1
    *Unsigned* comparison is based on the carry flag, you do have that. There is a way to implement signed comparison with these operations too but you shouldn't need it here. – harold Jun 04 '23 at 15:27
  • 1
    x86 `jb` (jump if below) is a synonym for `jc` (jump if carry-out is set). x86 sets CF=1 after subtraction if there was a borrow, opposite of how some other ISAs like ARM set it. Checking the MSB of a subtraction result doesn't work as a compare for the full range of inputs because wrapping is possible. That's why unsigned compares need to check the 9th bit in the carry flag instead, and signed compares need to check SF != OF (https://www.felixcloutier.com/x86/jcc), not just the "negative" condition. (x86 OF is the signed-overflow flag, like ARM's V flag for example.) – Peter Cordes Jun 04 '23 at 15:51
  • Thanks to both of you @harold and @PeterCordes! Seems so obvious once you know about it ^^ I had just tried it and made a successful division and modulo calculation by checking the carry-out – Beragrom Jun 04 '23 at 18:30

0 Answers0