Tuesday, November 19, 2013

Simpleton Geek Teaches Computer Programming Book 1 Appendix


Appendix A - Increment Operator

Trying to come up with an algorithm that will do increment using byte-size operand is quite painful. It would be trivial if you can specify bit(n) where individual bit is addressed. However, I did managed to do it, albeit clumsily.

A=Hexadecimal to be incremented
B=0001, to be shifted left (Multiply by 2) each pass
C=B OR C
D=A AND B OR G, OR G is needed for flag
E=1111<<(4*D), in effect if D==0 then E=15 else E=0
F=C AND E OR F, in effect, C only if E==15.

Here is the full code (Petit Computer) that I did in order to test the effect. Notice that REP goes to 4, instead of 3 in order to have 15 properly incremented to 0.

FOR A=0 TO 15
CLS

B=1:C=1:D=A AND B:E=0:F=0:G=0
IF !D THEN E=15 ELSE E=0
F=C AND E:G=G OR E
GOSUB @DISP

FOR REP=1 TO 4
B=B*2:C=C OR B:D=A AND B OR G
IF !D THEN E=15 ELSE E=0
F=C AND E OR F:G=G OR E
GOSUB @DISP
NEXT REP

?A XOR F
FOR I=0 TO 8:VSYNC 1:I=BTRIG():NEXT

NEXT A
END

@DISP
?HEX$(A);”  “;
?HEX$(B);”  “;
?HEX$(C);”  “;
?HEX$(D);”  “;
?HEX$(E);”  “;
?HEX$(F);”  “;
?HEX$(G);”  “
RETURN



APPENDIX B

Adding two numbers in binary is really easy.

CLS
A=RND(15):B=RND(15)
?A,B
FOR I=0 TO 4
C=(A AND B)*2
B=A XOR B
A=C
?A,B,C
NEXT I

You may be tempted to write similar routines for multiplications and division. There's an old war story here. Chris Crawford, who is one of the grand old man in computer game programming, was having difficulty in coming up a good way to divide two numbers for his game (WIZARD). In the end, he used a convoluted method to divide numbers based on technique used by the ancient Mayan. He outlined the steps in his book “Chris Crawford on Game Design.” I must admit that his steps are convoluted, indeed!
However, binary division isn't that big of a deal. It's just like multiplication, except there's a comparison operator in between. So, I am at loss as to why the great man was having difficulty. Still, it goes to show you that just because you have a good solution, you still need to find a better one!
Here is a simple program for doing multiplication. All it does is shift the number, and check whether or not the relevant bit is zero. If it is, then no further processing is done. Else, add the number to the accumulator.

CLS
A=RND(15):B=RND(15):C=0:D=1
?A,B
FOR I=1 TO 4
IF (D AND A) THEN C=C+B
?A,B,C,D,(D AND A)
B=B*2:D=D*2
NEXT I
?C

And here is the division program. You can see how very little things change. All it does is doing it from the other direction and an extra comparison and addition.

CLS
A=RND(255):B=RND(15)
?A,B
C=0:D=8:F=0
FOR I=1 TO 4
E=B*D
IF F+E<=A THEN F=F+E:C=C+D
?A,B,C,D
D=D/2
NEXT I
?C

Alternatively, here is another division program, but in this case, A contains the remainder. In addition, I extended the bit operation to 8 bits.

CLS
A=RND(255):B=RND(15)
?A,B
C=0:D=128:E=B*D
FOR I=1 TO 8
IF A-E>=0 THEN A=A-E:C=C+D
?A,B,C,D,E
D=D/2:E=E/2
NEXT I
?C,A


No comments:

Post a Comment