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.

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.

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:

ADD EAX,63

AND EAX,63

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:

ADD EAX,63

AND EAX,63

That should be

ADD eax, 63

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

ADD eax, 63

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

Sorry, I was in a hurry and skipped the NOT (made my typing faster :grin: ).

;)

;)

```
; Align memory/register value by power of two
```

ialign MACRO memreg:REQ, imm:REQ

add memreg, imm - 1

and memreg, 0 - imm

ENDM

ialign eax, 64

ialign Buf.len, 256

Thanks Maverick and Mirno for coming up with the answer, and thanks to BitRake for making it cool :grin:

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

Or, if you want to get tech fancy, you could use the fpu and use its "round up". At least that would be fast. :)