informedcitizenry / 6502.Net

A .Net-based Cross-Assembler for Several 8-Bit Microprocessors
MIT License
58 stars 17 forks source link

Cannot 'lda' with the 'least significant' operator for certain negative numbers #5

Closed svallee-dev closed 4 years ago

svallee-dev commented 4 years ago

This is a very specific issue I have encountered when using this format:

lda #<NUM

Where NUM can be any number between 0 and 65535. With these positives numbers it works fine. It also seems to work fine with negative numbers... with the exception of numbers between -128 and -255 (inclusive).

For instance, these works:

lda #<-127
lda #<-256

But these doesn't:

lda #<-128
lda #<-200
lda #<-255

For any numbers between these two, assembly stops on this error:

Mode not supporter for "lda" in selected CPU.

svallee-dev commented 4 years ago

Turns out there a few more issues with negative numbers (incl most significant byte not getting the $ff for negative numbers). So in case others have this issue and it doesn't get fixed in 6502.net, here is my work around, two functions that I'll now use instead of the < and > operators:

LEAST   .function val
            .if val < 0
                .return 255 & (256 - ((-val) % 256))
            .else
                .return <val
            .endif
        .endfunction

MOST    .function val
            .return val >> 8
        .endfunction
informedcitizenry commented 4 years ago

Thanks again for the feedback. A very subtle bug in how we're handling the LSB and MSBs, I should have a fix posted soon.