News: 11 March 2016 - Forum Rules

Author Topic: 6502 multiplication?  (Read 3438 times)

KingMike

  • Forum Moderator
  • Hero Member
  • *****
  • Posts: 7183
  • *sigh* A changed avatar. Big deal.
    • View Profile
6502 multiplication?
« on: October 03, 2012, 09:06:30 pm »
So, I got this strange piece of code.
Just from trying a few examples, it looks like it is effectively doing an 8-bit by 8-bit multiplication.
I'm guessing should just go ahead with that assumption?
$40/41 the multipliers, $42/43 the little-endian result.
(looking at the formula for determining enemy damage to the player.
$40 = (enemy attack - player defense)>>4
$41 = random value 0-6
since 42 is going to be overwritten by 8 bits anyways, it would seem it's initial value is undefined and unimportant
Code: [Select]
     txa
     pha
     lda #$00
     sta $43
     ldx #$08
MainLoop:
     lsr $40
     bcc NoCarry
     clc
     adc $41
NoCarry:
     ror a
     ror $42
     dex
     bne MainLoop
     sta $43
     pla
     tax
     rts
"My watch says 30 chickens" Google, 2018

Bregalad

  • Hero Member
  • *****
  • Posts: 2763
    • View Profile
Re: 6502 multiplication?
« Reply #1 on: October 04, 2012, 02:58:25 am »
Yes, it looks like a multiplication, and no, there is nothing "strangle" about it.

BRPXQZME

  • Hero Member
  • *****
  • Posts: 4572
  • じー
    • View Profile
    • The BRPXQZME Network
Re: 6502 multiplication?
« Reply #2 on: October 04, 2012, 03:06:08 am »
Your analysis is correct. The loop just adds to the accumulator whenever it detects the low bit in $40, and shifts things right until the 8 low bits are in $42 (logically, you could think of it as the add value in $41 keeps getting shifted left, but that doesn’t make computer-sense).
we are in a horrible and deadly danger