dmsc / fastbasic

FastBasic - Fast BASIC interpreter for the Atari 8-bit computers
GNU General Public License v2.0
135 stars 20 forks source link

Bitwise shift #27

Open rpocc opened 4 years ago

rpocc commented 4 years ago

Evolving the idea of implementing the bitwise shift operators I could suggest a syntax for such shifting: First of all, I think logical shift is a lot more used and more abstract and common operation that arithmetic shift right because the latter can be implemented through the first but not vice versa.

Possible notation can be like this: LSR v,n 'v: value, n: number of bits LSL v,n 'v:value, n: number of bits So, it's variable-modifying logical shift right or left.

Also, if 6502 can't do logical shift to variable amount of bits, this can be shrinked to just ope-operand command like LSR v / LSL v

Another approach is the opposite, self-containing function like LSR(v,n) / LSL(v,n), returning the result of non-destructive logical shift. The functional approach is of course more universal because it could be used in expressions.

And the third approach is just copying C language: through binary operators << and >>. I suspect that operators in basic are actually macros for functions in a generalized form, so I believe there's shoudnt' be much difference between making it either way and the operator approach should be the most powerful, intuitive and readable.

dmsc commented 4 years ago

I'm liking more the "LSL()" and "LSR()" functions, or as you said that all shifts should be "logical", perhaps "SHR()" and "SHL()" would be even better. Also, it is simpler (and uses less memory) to implement only shifts by one.

Implementing the C syntax is possible, but even if I like C, I think that BASIC should not be like C :)

I mean, BASIC should be a language without too much symbols and easier syntax.

Note that this is the implementation of the current shift left in FastBasic:

.proc   EXE_USHL ; AX = AX * 2 (UNSIGNED)
        asl
        tay
        txa
        rol
        tax
        tya
        jmp     next_instruction
.endproc

It really shows how bad the 6502 is at 16bit arithmetic :-P