Multiplication by fixed numbers is very easy. The easiest multiplication is by powers of two. For example:
Code: Select all
add a,a ; Once: x2
add a,a ; Twice: x4
add a,a ; Three times: x8
add a,a ; Four times: x16
add a,a ; Five times: x32
add a,a ; Six times: x64
; (and so on)
If you wish to multiply by a non power of two, you'll need to work out the powers of two that make up that number, then calculate them and sum them. For example, multiplication by 12 is [x2]+[x4]+[x8], so:
Code: Select all
; a = a * 12
add a,a ; a = a * 2
ld b,a ; b = a * 2
add a,a ; a = a * 4
ld c,a ; c = a * 4
add a,a ; a = a * 8
add a,b ; a = a * 10 (a*8+a*2)
add a,c ; a = a * 12 (a*8+a*2+a*4)
I'm a bit worried by that calculation you provided, as it looks like it's the way you calculate the address of the byte you start copying to when drawing a sprite, and the division and modulo 12 looks wrong. Maybe I'm assuming too much - what are you trying to do? Usually with sprites you only have to deal with multiplications by 12 and division/modulo 8.
EDIT: Oh, I see.
Let's do this more easily: let's say you want to take an (x,y) and want that to end up as the correct location.
Obviously, one x moves you one byte, but each y coordinate needs to move you down by 8*12 = 96 bytes. This is easy enough, as seen above - 96 = 64+32:
Code: Select all
; a = x
; l = y
ld h,0
add hl,hl ; *2
add hl,hl ; *4
add hl,hl ; *8
add hl,hl ; *16
add hl,hl ; *32
ld d,h \ ld e,l
add hl,hl ; *64
add hl,de ; *96 :)
ld d,0 \ ld e,a
add hl,de ; hl = l*96+a
ld de,plotSScreen
add hl,de
; hl->address on screen to draw sprite
I'm not sure why you'd want to use a single value to determine location (as opposed to just using an (x,y) coordinate), but with the above you should be able to work it out (though it could be a bit messy what with dealing with 12, not a tidy number).