Page 2 of 2

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

Posted: Tue Oct 09, 2018 7:21 pm
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.

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

Posted: Wed Oct 10, 2018 9:15 pm
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
}

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

Posted: Fri Oct 12, 2018 8:56 pm
by JimBoyd
dmsc wrote:
You are right, this should work:
It does. As Mike said, well done!

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

Posted: Mon Oct 15, 2018 9:53 pm
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

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

Posted: Tue Oct 16, 2018 6:50 am
by paul_nicholls
Thanks for all the answers everyone, much appreciated :)

I think I'm good now :D

cheers,
Paul