[Z80 ASM] Multiplication macro idea

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

Post Reply
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

[Z80 ASM] Multiplication macro idea

Post by driesguldolf »

Check the bottom edit note first!


The idea is to be able to multiply a value with a constant number without making macros for every case

This is my first idea (but it doesn't work):
reg is either A or HL, sup is the support register (make sup=reg before using) and times the constant you want your value multiplied with

Code: Select all

.deflong MULT(reg, sup, times)
	i = times
.while i != 1
.if i % 2 = 1
	i = i - 1
	add reg, sup
.else
	i = i / 2
	add reg, reg
.endif
.endwhile
.enddeflong
As you can clearly see this writes the additions in the wrong directions

I was thinking of using a combination of .org directives and a logaritm to calculate the amount of additions... There must be an easier way! ^^

Now I think of it...

Code: Select all

   j=i
   n=0
.while j!=1
   n=n+1
.if j%2=1
   j=j-1
.else
   j=j/2
.endif
.endwhile
But still this idea isn't so elegant...

Any ideas?

And yes, it's meant for Brass (obviously ^^).



EDIT:
WHOAAAA hold on... I missed something...
there is no .while, though it shouldn't be that hard to synthesize it with a .repeat/.loop
But the concept stays, is there a way to create an elegant macro to multiply a value with a constant?
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

The idea is good, but why don't you just do it yourself? It's not like constant-multiplication is hard..
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post by driesguldolf »

I tried but brass ain't really cooporating :'( link
if that would work I have one but it isn't really elegant ;)
Spencer
Extreme Poster
Posts: 346
Joined: Mon 17 Jan, 2005 8:56 am
Location: Indiana

Post by Spencer »

Jim and I wrote one for spasm a while ago. Maybe you can create something like this

Code: Select all

#macro multhlfast( num ) 
#define mfstart 0 
 muloop(15) 
#if mfstart=0 \ ld hl,0 \ #endif 
#endmacro

#define muloop( loop ) #if loop >= 0\ do_mul( 1<<loop )\ muloop( eval( loop-1) )\ #endif

#macro do_mul(bit)
 #if mfstart = 1
   add hl,hl
 #endif
 #if num&bit = bit
   #if mfstart = 0
     #if num&(bit-1)>0
       ld e,l \ ld d,h
     #endif
     #define mfstart 1
   #else
     add hl,de
   #endif
  #endif
#endmacro
I believe it does what you suggest, and optimizes the first iteration.
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post by driesguldolf »

brass doesn't seem to support arbitrary loops (the amount of time something is loop is determed beforehand, so you cannot modify the loop variable)
Post Reply