Signed 16-bit right shift question with -1 shift check

Programming the 6502 microprocessor and its relatives in assembly and other languages.
JimBoyd
Posts: 931
Joined: 05 May 2017

Re: Signed 16-bit right shift question with -1 shift check

Post by JimBoyd »

No, adding 1 before shifting doesn't work. If the dividend is -1, it is set to zero. The carry flag is still set from the test of the sign bit so when the number is shifted, the result returned is -32768.
dmsc
Posts: 153
Joined: 17 Sep 2018

Re: Signed 16-bit right shift question with -1 shift check

Post by dmsc »

Hi!
JimBoyd wrote:
No, adding 1 before shifting doesn't work. If the dividend is -1, it is set to zero. The carry flag is still set from the test of the sign bit so when the number is shifted, the result returned is -32768.
You are right, this should work:

Code: Select all

.macro div2_16bit(address) {
  ldy address + 1
  bpl ok
  inc address +0
  bne ok
  iny
ok:
  cpy #$80
  tya
  ror
  ror address + 0
  sta address + 1
}
JimBoyd
Posts: 931
Joined: 05 May 2017

Re: Signed 16-bit right shift question with -1 shift check

Post by JimBoyd »

dmsc wrote:
You are right, this should work:
It does. As Mike said, well done!
Chromatix
Posts: 1462
Joined: 21 May 2018

Re: Signed 16-bit right shift question with -1 shift check

Post by Chromatix »

Here's my attempt, which just special-cases the -1 case:

Code: Select all

.Div16_2
  LDA address
  AND address+1
  INC A    ; turns 0xFFFF into zero, anything else will be non-zero
  BNE Shift16
  STA address    ; explicitly store a zero result
  STA address+1
  BEQ End
.Shift16
  LDA address+1
  ASL A
  ROR address+1
  ROR address
.End
paul_nicholls
Posts: 42
Joined: 19 Jan 2011
Contact:

Re: Signed 16-bit right shift question with -1 shift check

Post by paul_nicholls »

Thanks for all the answers everyone, much appreciated :)

I think I'm good now :D

cheers,
Paul
"The plastic veneer of civilization is easily melted in the heat of the moment" - Paul Nicholls
Post Reply