Path: utzoo!attcan!uunet!willett!ForthNet From: ForthNet@willett.UUCP (ForthNet articles from GEnie) Newsgroups: comp.lang.forth Subject: F-PC Forth Tutorial Message-ID: <1055.UUL1.3#5129@willett.UUCP> Date: 1 Jun 90 03:45:47 GMT Organization: Latest link in the ForthNet chain. (Pgh, PA) Lines: 77 Date: 05-30-90 (22:48) Number: 17 (Echo) To: JACK BROWN Refer#: NONE From: ROY RICE Read: NO Subj: LESSON3 Status: PUBLIC MESSAGE jACK, HERE IS LESSON 3, EX.3.6: \ rarl3a.seq - answers to lesson 3, continued at ex.3.6 : D1+ ( d -- D+1 ) 1. D+ ; : D2+ ( d -- d+2 ) 2. D+ ; : D1- ( d -- d-1 ) 1. D- ; : D2- ( d -- d-2 ) 2. D- ; comment: 05/26/90 : D2* ( d -- 2d ) 2 *D ; this won't work, because *D takes a 16 bit integer as its argument. The "normal" D2* is code, because a multiply by 2 is just a left shift of a binary number, which is how numbers are stored and manipulated in the machine. So far I haven't found an arithmetic operator that I can use to form the reqiired word. Later: 05/28/90 The following seems to be a bruite force way. Am I over complicating this question?? It seems that you must handle the overflow from the low order 2 bytes some way, and this is the only way I could come up with to do it. comment; : d2* ( d -- 2d ) \ multiply the double number by 2 \ stack comments du= upper 2 bytes, dl= lower 2 bytes 2 UM* ( dl dh*2_lo dh*2_hi --) \ mul high byte x2 ROT ( dh*2_lo dh*2_hi dl --) 2 UM* ( dh*2_lo dh*2_hi dl*2_lo dl*2_hi -- ) \ both are now doubles, but the dh*2 must be swapped to get \ its bytes in the proper order 2SWAP SWAP D+ ; \ add with bytes in proper order : D2/ ( d -- d/2 ) 2 MU/MOD ROT DROP ; comment: um/mod leaves a single quotient, so it will give a wrong result on any devide which results in a double quotient. Is this definition of any use, or is it just in the kernel as a source for mu/mod? CODE UM/MOD ( ud un -- URemainder UQuotient ) \ Unsigned double number divided by unsigned single results in unsigned \ remainder and quotient, with quotient on top. POP BX POP DX POP AX CMP DX, BX U>= ( divide by zero? ) IF MOV AX, # -1 MOV DX, AX 2PUSH THEN DIV BX 2PUSH END-CODE : MU/MOD ( ud# un1 -- rem d#quot ) \ Divide unsigned double by a single, leaving a remainder and quotient. >R 0 R@ UM/MOD R> SWAP >R UM/MOD R> ; The stack comments above are confusing. Why did he use ud# instead of ud and un1 instead of un in the second definition? comment; : test2 ( d -- ) \ test the above double number operators 2 ?ENOUGH 2DUP CR ." INPUT= " D. CR 2DUP D1+ ." D1+ = " D. CR 2DUP D2+ ." D2+ = " D. CR 2DUP D1- ."" D1- = " D. CRR 2DUP D2- ." D2- = " D. CR 2DUP D2* ." D2* = " D. CR D2/ ." D2/ = " D. CR ; best, Roy ----- This message came from GEnie via willett through a semi-automated process. Report problems to: uunet!willett!dwp or willett!dwp@hobbes.cert.sei.cmu.edu