A thread where I'm going to ask some math questions

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: A thread where I'm going to ask some math questions

Post by tokumaru »

Yeah, an arithmetic right shift will shift the most significant bit right, but also preserve it as the most significant bit.

When used for division, an arithmetic right shift will round the result down, towards negative infinity, not towards zero.

If you try to divide -3 by 2, the correct result is -1.5, but in two's complement that gets rounded down to -2. The same happens when you divide -1 by 2, which should be -0.5, but that gets rounded down to -1 and you're stuck with -1 no matter how much you keep shifting... there will never be negative 0.
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: A thread where I'm going to ask some math questions

Post by rainwarrior »

tokumaru wrote:Yeah, an arithmetic right shift will shift the most significant bit right, but also preserve it as the most significant bit.
This instruction doesn't exist on the 6502. There is "arithmetic shift left" (ASL) and "logical shift right" (LSR), both of which are the same as rotate (ROL/ROR) if carry was zero.

There is no sign preserving right shift, AFAIK, you have to manually move the sign to carry before starting with ROR. Samophlange's LDA/CMP is a valid way to load carry with the sign. Could also LDA/ROL for the same result in one less byte.
User avatar
pubby
Posts: 583
Joined: Thu Mar 31, 2016 11:15 am

Re: A thread where I'm going to ask some math questions

Post by pubby »

illegal opcode ARR #$FF is pretty close to being a sign-preserving right shift. At least when doing multiple shifts it is.

To round up, add N-1 to the value before shifting, where N is the amount you're dividing by.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: A thread where I'm going to ask some math questions

Post by tokumaru »

rainwarrior wrote:This instruction doesn't exist on the 6502.
I didn't say it did, I was just explaining how the operation he implemented in several 6502 instructions worked.
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: A thread where I'm going to ask some math questions

Post by rainwarrior »

pubby wrote:illegal opcode ARR #$FF is pretty close to being a sign-preserving right shift. At least when doing multiple shifts it is.
Just to fully clarify, ARR appears to be:

1. Overflow (V) is set as if an ADC of: (A & #immediate) + #immediate ?
2. Result is: AND A with #immediate, followed by ROR
3. Carry (C) is set from bit 6 of result.
4. Zero/Negative (Z/N) from result.

So you still need to load the carry with sign before starting the shifts, but if you're doing multiple shifts on a single byte ARR #-1 can keep putting the sign back into the carry after each shift.

Apparently only helps if you need to shift more than once. Looks like this:

Code: Select all

; value to be shifted already in A
CMP #$80 ; load sign into carry
ARR #$FF ; signed /2
ARR #$FF ; signed /4
ARR #$FF ; signed /8
ROR ; signed /16
; note that the last shift in the chain can be optimized as a ROR to save a byte.
(ARR is illegal opcode $6B.)
Last edited by rainwarrior on Sat Dec 08, 2018 10:47 pm, edited 1 time in total.
Garth
Posts: 246
Joined: Wed Nov 30, 2016 4:45 pm
Location: Southern California
Contact:

Re: A thread where I'm going to ask some math questions

Post by Garth »

We had basically the same conversation on 6502.org, at http://forum.6502.org/viewtopic.php?f=2&t=5317 . Various code suggestions were made there.
http://WilsonMinesCo.com/ lots of 6502 resources
User avatar
samophlange
Posts: 50
Joined: Sun Apr 08, 2018 11:45 pm
Location: Southern California

Re: A thread where I'm going to ask some math questions

Post by samophlange »

Thanks to all the folks in this thread (and re-reading that article on 6502.org several times) I've got some good general purpose math routines and I'm slowly changing some game objects over to have some nicer movement characteristics. :D
Is there a clever/compact way to see if two variables have the same sign? The only way I can think of involves ANDing with %10000000 into two temporaries and comparing those, but it seems like there should be some way to use the bit 7 "negative flag".
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: A thread where I'm going to ask some math questions

Post by rainwarrior »

AND, ORA, and EOR all set the sign flag with bit 7 of the result, which can be useful for stuff like this.

Comparing value in A to variable "v":

Code: Select all

EOR v
; sign bit will be 0 if the signs match, 1 if they differ

AND v
; sign bit will be set if both signs are negative

ORA v
; sign bit will be clear if both signs are positive
It's very often useful to store bit flags in the high bit, for this reason. A lot of instructions have "easy" access to it. (Also note that ROR/ROL make it easy to pass the the sign in/out of the carry flag too.)

Bit 6 is also convenient just for the BIT instruction, which can test it directly and puts the result in the overflow flag V... which is weird and special to that one instruction, but pretty handy especially for testing some hardware registers.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: A thread where I'm going to ask some math questions

Post by tepples »

Bit 6 and 0 are only slightly less convenient than bit 7. Even in indexed or indirect contexts, where you can't bit, you can still send bit 6 to the N flag using asl a or bit 0 to the C flag using lsr a.
User avatar
Kasumi
Posts: 1293
Joined: Wed Apr 02, 2008 2:09 pm

Re: A thread where I'm going to ask some math questions

Post by Kasumi »

Just to get a little meta, it might be worth looking into the overflow flag if you're not super familiar with it. Some cases where I used to want to check for same sign were helped by better understanding of the overflow flag. That's not to say using the eor is bad, just that there might be something better for the longer term goal sometimes.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: A thread where I'm going to ask some math questions

Post by tokumaru »

samophlange wrote:Is there a clever/compact way to see if two variables have the same sign? The only way I can think of involves ANDing with %10000000 into two temporaries and comparing those, but it seems like there should be some way to use the bit 7 "negative flag".
You don't need to isolate the bits or use any temporaries for this. Just EOR one value with the other directly and the N flag of the result will give you the answer. The result of EOR is 0 if both bits are equal, or 1 if they differ. So you can simply do this:

Code: Select all

  lda Variable0
  eor Variable1
  bmi DifferentSigns
SameSign:
  ;(...)
DifferentSigns:
  ;(...)
Bitwise operations like EOR, AND and OR between two values only operate in bits at the same position in their respective bytes, so you don't need to isolate the bits of interest out of fear that the other ones might influence the result in any way. You only need to isolate the result, if the CPU doesn't do it for you automatically (as it does by copying bit 7 to the N flag).
Post Reply