Friday, November 29, 2013

Petit Computer Journal #27

Boolean Operator Division

Have you ever wondered just how difficult it is to do division using nothing but NOT,AND,OR,XOR? Well, so do I. It turns out I also need ADD,SUB, and CMP. Also, Shift command. Still, these are easily within the domain of any assembly language programming.


  1. CLS
  2. A=RND(255):B=RND(15)
  3. ?A,B
  4. C=0:D=128:E=B*D
  5. FOR I=1 TO 8
  6. IF A-E>=0 THEN A=A-E:C=C+D
  7. ?A,B,C,D,E
  8. D=D/2:E=E/2
  9. NEXT I
  10. ?C,A
  11. FOR I=0 TO 8
  12. VSYNC 1:I=BTRIG():NEXT
  13. GOTO @DIV2


A contains the number to be divided. B contains the number that divides. This computes C=A/B and A=A%B. A pretty nifty code that fits single screen challenge. Notice, that you can also do it this way (untested) on line 6:


  1. A=A-E:IF A<0 THEN A=A+E ELSE C=C+D

This way, due to the calculation in Assembly language where the register holds the resulting value of the calculation.



No comments:

Post a Comment