Hi guys,
i am crap at math in asm, due to lack of practice, so i decided to ask here so i get it right first time....

I have loaded a file into memory, and i am about to call an MD5 summing routine to checksum it. The problem is, i have a variable that contains the length of the data, but i need to round that length value up to the nearest factor of 64 (in other words, the length variable must be evenly divisible by 64). How can i best pad this number up? The file i am testing on is 9725 bytes long (hex 25FD), this particular value needs to rounded up to 9728 (hex 2600).

I could mess around and hack out a solution, but it wouldn't be pretty, and it probably wouldn't be fast. So if anyone could show me a cool way to do this (it _has_ to be cool :grin: ), i would very much appreciate it.
Posted on 2002-04-16 07:21:00 by sluggy
Hi sluggy,

I'm in a hurry, but If I got it right what you want is:

\$2500 -> \$2500

\$2501 -> \$2540

\$253F -> \$2540

\$2540 -> \$2540

\$2541 -> \$2580

So simply ADD 63 and then AND 63:

AND EAX,63
Posted on 2002-04-16 07:43:44 by Maverick
That should be
AND eax, NOT(63)

This will work for any power of 2 based rounding.
To round to 2^x, add (2^x - 1), and with NOT(2^x - 1)

Mirno
Posted on 2002-04-16 08:14:51 by Mirno
Sorry, I was in a hurry and skipped the NOT (made my typing faster :grin: ).

;)
Posted on 2002-04-16 08:54:13 by Maverick
``````; Align memory/register value by power of two
ialign MACRO memreg:REQ, imm:REQ
and memreg, 0 - imm
ENDM

ialign eax, 64
ialign Buf.len, 256``````
Posted on 2002-04-16 09:57:04 by bitRAKE
Thanks Maverick and Mirno for coming up with the answer, and thanks to BitRake for making it cool :grin:
Posted on 2002-04-16 16:16:46 by sluggy
In case you need nonbinary rounding, the general rule is

``````if value >= 0
roundup(value, boundary) =
((value + (boundary - 1)) / boundary) * boundary
else
roundup(value, boundary) =
value / boundary

where "/" is the conventional round-to-zero integer divide
``````
Posted on 2002-04-17 22:10:21 by tenkey
Or, if you want to get tech fancy, you could use the fpu and use its "round up". At least that would be fast. :)
Posted on 2002-04-23 23:38:03 by tmb