nesdev.comhttp://forums.nesdev.com/ A thread where I'm going to ask some math questionshttp://forums.nesdev.com/viewtopic.php?f=10&t=17927 Page 3 of 3

 Author: tokumaru [ Sat Dec 08, 2018 9:09 pm ] Post subject: Re: A thread where I'm going to ask some math questions 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.

 Author: rainwarrior [ Sat Dec 08, 2018 9:30 pm ] Post subject: Re: A thread where I'm going to ask some math questions 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.

 Author: pubby [ Sat Dec 08, 2018 9:42 pm ] Post subject: Re: A thread where I'm going to ask some math questions 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.

 Author: tokumaru [ Sat Dec 08, 2018 10:13 pm ] Post subject: Re: A thread where I'm going to ask some math questions 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.

 Author: rainwarrior [ Sat Dec 08, 2018 10:30 pm ] Post subject: Re: A thread where I'm going to ask some math questions 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 ROR3. 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:; value to be shifted already in ACMP #\$80 ; load sign into carryARR #\$FF ; signed /2ARR #\$FF ; signed /4ARR #\$FF ; signed /8ROR ; 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.)

 Author: Garth [ Sat Dec 08, 2018 10:39 pm ] Post subject: Re: A thread where I'm going to ask some math questions 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.

 Author: samophlange [ Mon Jan 07, 2019 9:54 pm ] Post subject: Re: A thread where I'm going to ask some math questions 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. 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".

 Author: rainwarrior [ Mon Jan 07, 2019 10:00 pm ] Post subject: Re: A thread where I'm going to ask some math questions 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:EOR v; sign bit will be 0 if the signs match, 1 if they differAND v; sign bit will be set if both signs are negativeORA v; sign bit will be clear if both signs are positiveIt'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.

 Author: tepples [ Mon Jan 07, 2019 10:50 pm ] Post subject: Re: A thread where I'm going to ask some math questions 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.

 Author: Kasumi [ Mon Jan 07, 2019 11:00 pm ] Post subject: Re: A thread where I'm going to ask some math questions 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.

 Author: tokumaru [ Tue Jan 08, 2019 12:32 am ] Post subject: Re: A thread where I'm going to ask some math questions 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:  lda Variable0  eor Variable1  bmi DifferentSignsSameSign:  ;(...)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).

 Page 3 of 3 All times are UTC - 7 hours Powered by phpBB® Forum Software © phpBB Grouphttp://www.phpbb.com/