Advertisement
NovaYoshi

6502 multiply

Mar 19th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;
  2. ; Multiplies two 8-bit factors to produce a 16-bit product
  3. ; in about 153 cycles.
  4. ; @param A one factor
  5. ; @param Y another factor
  6. ; @return high 8 bits in A; low 8 bits in $0000
  7. ;         Y and $0001 are trashed; X is untouched
  8. .proc mul8
  9. factor2 = TempVal+1
  10. prodlo = TempVal
  11.  
  12.   ; Factor 1 is stored in the lower bits of prodlo; the low byte of
  13.   ; the product is stored in the upper bits.
  14.   lsr a  ; prime the carry bit for the loop
  15.   sta prodlo
  16.   sty factor2
  17.   lda #0
  18.   ldy #8
  19. loop:
  20.   ; At the start of the loop, one bit of prodlo has already been
  21.   ; shifted out into the carry.
  22.   bcc noadd
  23.   clc
  24.   adc factor2
  25. noadd:
  26.   ror a
  27.   ror prodlo  ; pull another bit out for the next iteration
  28.   dey         ; inc/dec don't modify carry; only shifts and adds do
  29.   bne loop
  30.   ldy TempVal
  31.   rts
  32. .endproc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement