News:

11 March 2016 - Forum Rules

Main Menu

6502 multiplication?

Started by KingMike, October 03, 2012, 09:06:30 PM

Previous topic - Next topic

KingMike

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

     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

Yes, it looks like a multiplication, and no, there is nothing "strangle" about it.

BRPXQZME

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