diff --git a/math32.tal b/math32.tal index 656e3ad..fad17a1 100644 --- a/math32.tal +++ b/math32.tal @@ -19,27 +19,30 @@ ( ) ( Operations supported: ) ( ) -( - add32 x** y** -> z** [x + y] ) -( - sub32 x** y** -> z** [x - y] ) -( - mul16 x* y* -> z** [x * y] ) -( - mul32 x** y** -> z** [x * y] ) -( - div32 x** y** -> z** [x / y] ) -( - negate32 x** -> z** [-x] ) -( - left-shift x** n^ -> z** [x< z** [x>>n] ) -( - and32 x** y** -> z** [x & y] ) -( - or32 x** y** -> z** [x | y] ) -( - xor32 x** y** -> z** [x ^ y] ) -( - complement32 x** -> z** [~x] ) -( - eq32 x** y** -> bool^ [x == y] ) -( - ne32 x** y** -> bool^ [x != y] ) -( - is-zero32 x** -> bool^ [x == 0] ) -( - non-zero32 x** -> bool^ [x != 0] ) -( - lt32 x** y** -> bool^ [x < y] ) -( - gt32 x** y** -> bool^ [x > y] ) -( - lteq32 x** y** -> bool^ [x <= y] ) -( - gteq32 x** y** -> bool^ [x >= y] ) -( - bitcount x** -> bool^ log2+1 ) +( NAME STACK EFFECT DEFINITION ) +( add32 x** y** -> z** x + y ) +( sub32 x** y** -> z** x - y ) +( mul16 x* y* -> z** x * y ) +( mul32 x** y** -> z** x * y ) +( div32 x** y** -> z** x / y ) +( negate32 x** -> z** -x ) +( lshift32 x** n^ -> z** x< z** x>>n ) +( and32 x** y** -> z** x & y ) +( or32 x** y** -> z** x | y ) +( xor32 x** y** -> z** x ^ y ) +( complement32 x** -> z** ~x ) +( eq32 x** y** -> bool^ x == y ) +( ne32 x** y** -> bool^ x != y ) +( is-zero32 x** -> bool^ x == 0 ) +( non-zero32 x** -> bool^ x != 0 ) +( lt32 x** y** -> bool^ x < y ) +( gt32 x** y** -> bool^ x > y ) +( lteq32 x** y** -> bool^ x <= y ) +( gteq32 x** y** -> bool^ x >= y ) +( bitcount8 x^ -> bool^ floor(log2(x))+1 ) +( bitcount16 x* -> bool^ floor(log2(x))+1 ) +( bitcount32 x** -> bool^ floor(log2(x))+1 ) ( ) ( In addition to the code this file uses 44 bytes of registers ) ( to store temporary state: ) @@ -51,7 +54,7 @@ %DEBUG { #ff #0e DEO } %RTN { JMP2r } %TOR { ROT ROT } ( a b c -> c a b ) -%TOR2 { ROT2 ROT2 } +( %TOR2 { ROT2 ROT2 } ) %COMPLEMENT32 { SWP2 #ffff EOR2 SWP2 #ffff EOR2 } ( bitcount: number of bits needed to represent number ) @@ -172,15 +175,15 @@ ( bit shifting ) ( x >> n ) -@right-shift ( x** n^ -> x< x< x< x< x< x< x< x< x< x< x< x< x< x< x< x< x< x< x< x< cur ) #0000 #0001 ROT2 POP - ;left-shift JSR2 ;div32/cur1 STA2 ;div32/cur0 STA2 + ;lshift32 JSR2 ;div32/cur1 STA2 ;div32/cur0 STA2 ( div< div ) ;div32/div0 LDA2 ;div32/div1 LDA2 ROT2 POP - ;left-shift JSR2 ;div32/div1 STA2 ;div32/div0 STA2 + ;lshift32 JSR2 ;div32/div1 STA2 ;div32/div0 STA2 &loop ( if rem >= the current divisor, we can subtract it and add to quotient ) @@ -380,8 +383,8 @@ ,&quo0 LDR2 ,&quo1 LDR2 ,&cur0 LDR2 ,&cur1 LDR2 ;add32 JSR2 ,&quo1 STR2 ,&quo0 STR2 ( quo += cur ) &rem-lt - ,&div0 LDR2 ,&div1 LDR2 #01 ;right-shift JSR2 ,&div1 STR2 ,&div0 STR2 ( div >>= 1 ) - ,&cur0 LDR2 ,&cur1 LDR2 #01 ;right-shift JSR2 ,&cur1 STR2 ,&cur0 STR2 ( cur >>= 1 ) + ,&div0 LDR2 ,&div1 LDR2 #01 ;rshift32 JSR2 ,&div1 STR2 ,&div0 STR2 ( div >>= 1 ) + ,&cur0 LDR2 ,&cur1 LDR2 #01 ;rshift32 JSR2 ,&cur1 STR2 ,&cur0 STR2 ( cur >>= 1 ) ,&cur0 LDR2 ,&cur1 LDR2 ;non-zero32 JSR2 ,&loop JCN ( if cur>0, loop. else we're done ) ,&quo0 LDR2 ,&quo1 LDR2 ( TODO: consider making this divmod32 ) RTN diff --git a/test-math32.tal b/test-math32.tal index c1ac0f2..8bdd3c7 100644 --- a/test-math32.tal +++ b/test-math32.tal @@ -52,8 +52,8 @@ RTN ;buf LDA LIT '* EQU ;test-mul32 JCN2 ;buf LDA LIT '- EQU ;test-sub32 JCN2 ;buf LDA LIT '/ EQU ;test-div32 JCN2 - ;buf LDA LIT 'L EQU ;test-left-shift JCN2 - ;buf LDA LIT 'R EQU ;test-right-shift JCN2 + ;buf LDA LIT 'L EQU ;test-lshift32 JCN2 + ;buf LDA LIT 'R EQU ;test-rshift32 JCN2 ;buf LDA LIT 'B EQU ;test-bitcount32 JCN2 ;buf LDA LIT '& EQU ;test-and32 JCN2 ;buf LDA LIT '| EQU ;test-or32 JCN2 @@ -123,8 +123,8 @@ RTN @test-mul32 ;mul32 ;binary-32-test JMP2 @test-sub32 ;sub32 ;binary-32-test JMP2 @test-div32 ;div32 ;binary-32-test JMP2 -@test-left-shift ;left-shift ;binary-32-8-32-test JMP2 -@test-right-shift ;right-shift ;binary-32-8-32-test JMP2 +@test-lshift32 ;lshift32 ;binary-32-8-32-test JMP2 +@test-rshift32 ;rshift32 ;binary-32-8-32-test JMP2 @test-bitcount32 ;bitcount32 ;unary-32-8-test JMP2 @test-and32 ;and32 ;binary-32-test JMP2 @test-or32 ;or32 ;binary-32-test JMP2