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

**Moderator:** Moderators

### 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.

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.

- rainwarrior
**Posts:**7804**Joined:**Sun Jan 22, 2012 12:03 pm**Location:**Canada-
**Contact:**

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

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.tokumaru wrote:Yeah, an arithmetic right shift will shift the most significant bit right, but also preserve it as the most significant bit.

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.

### 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.

To round up, add N-1 to the value before shifting, where N is the amount you're dividing by.

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

I didn't say it did, I was just explaining how the operation he implemented in several 6502 instructions worked.rainwarrior wrote:This instruction doesn't exist on the 6502.

- rainwarrior
**Posts:**7804**Joined:**Sun Jan 22, 2012 12:03 pm**Location:**Canada-
**Contact:**

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

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

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.
```

Last edited by rainwarrior on Sat Dec 08, 2018 10:47 pm, edited 1 time in total.

### 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.

http://WilsonMinesCo.com/ lots of 6502 resources

- samophlange
**Posts:**48**Joined:**Sun Apr 08, 2018 11:45 pm**Location:**Southern California

### 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".

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".

- rainwarrior
**Posts:**7804**Joined:**Sun Jan 22, 2012 12:03 pm**Location:**Canada-
**Contact:**

### 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":
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.

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
```

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.

### 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`.### 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.

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

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: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".

Code: Select all

```
lda Variable0
eor Variable1
bmi DifferentSigns
SameSign:
;(...)
DifferentSigns:
;(...)
```