News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Code to align to 32 bits

Started by NoCforMe, November 09, 2024, 06:53:17 PM

Previous topic - Next topic

NoCforMe

Small li'l tidbit here: in my current program I needed to align some data to a 32-bit (DWORD) boundary. I'm dealing with BMP bitmaps here and needed to determine the padding necessary for lines that didn't fall on such a boundary.

I came up with the following code:
      MOV    EAX, <line length>
      NEG    EAX                       ;Flip bits arithmetically
      AND    EAX, 11B                  ;Isolate 2 low bits
      MOV    padding, EAX              ;and Bob's your uncle

which works out to:
   length AND 11B   padding
          0            0
          1            3
          2            2
          3            1

I actually figured this out myself. I know there are other ways to skin this particular cat, and maybe even some cleverer ones. (Would it be possible to do it in only two instructions?) So I'm wondering what other methods people have come up with.

Micro$oft has this tricky technique that I still haven't quite figured out how it works:
    pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits + 31) & ~31) /8
                                  * pbmi->bmiHeader.biHeight;
Assembly language programming should be fun. That's why I do it.

Vortex

#1
Hi NoCforMe,

Reading your code from this message :

; Calculate actual bitmap size including any needed padding:
    MOV    EAX, BMPwidth
    MUL    BMPbpp
    ADD    EAX, 31
    AND    EAX, NOT 31
    SHR    EAX, 3
    MUL    BMPheight
    MOV    actualBMPsize, EAX

https://masm32.com/board/index.php?msg=126368

;  Bitmap size = (( BITMAPINFO.bmiHeader.biWidth * BITMAP.bmPlanes * BITMAP.bmBitsPixel + 31 ) & ~31 ) / 8
;                 * BITMAPINFO.bmiHeader.biHeight

;  ~31 = NOT 31 = NOT 00011111 = 11100000

;  NOT 00011111 = 11100000 => -32


The count of bits is aligned to the next 32-bit boundry. ( +31 trick )

This line does the 32-bit alignment :

    AND    EAX, NOT 31  ;  AND EAX,-32
A quick test :

Test 1
x=0  ,  x AND -32 = 0
x=1  ,  x AND -32 = 0
x=2  ,  x AND -32 = 0
x=3  ,  x AND -32 = 0
x=4  ,  x AND -32 = 0
.
.
x=29  ,  x AND -32 = 0
x=30  ,  x AND -32 = 0
x=31  ,  x AND -32 = 0
x=32  ,  x AND -32 = 32
x=33  ,  x AND -32 = 32
x=34  ,  x AND -32 = 32
.
.
x=62  ,  x AND -32 = 32
x=63  ,  x AND -32 = 32
x=64  ,  x AND -32 = 64
x=65  ,  x AND -32 = 64
x=66  ,  x AND -32 = 64
x=67  ,  x AND -32 = 64

Test 2
x=0  ,  (x+31) AND -32 = 0
x=1  ,  (x+31) AND -32 = 32
x=2  ,  (x+31) AND -32 = 32
x=3  ,  (x+31) AND -32 = 32
x=4  ,  (x+31) AND -32 = 32
.
.
x=29  ,  (x+31) AND -32 = 32
x=30  ,  (x+31) AND -32 = 32
x=31  ,  (x+31) AND -32 = 32
x=32  ,  (x+31) AND -32 = 32
x=33  ,  (x+31) AND -32 = 64
x=34  ,  (x+31) AND -32 = 64
.
.
x=62  ,  (x+31) AND -32 = 64
x=63  ,  (x+31) AND -32 = 64
x=64  ,  (x+31) AND -32 = 64
x=65  ,  (x+31) AND -32 = 96
x=66  ,  (x+31) AND -32 = 96
x=67  ,  (x+31) AND -32 = 96

sinsi

Quote from: Vortex on November 09, 2024, 08:12:25 PMThe count of bits is aligned to the next 32-bit boundry. ( +31 trick )

This line does the 32-bit alignment :

Code Select Expand
    AND    EAX, NOT 31  ;  AND EAX,-32
Isn't that 32 byte alignment?

;EAX has an address
  add eax,3
  and eax,not 3
Tá fuinneoga a haon déag níos fearr :biggrin:

zedd151

Quote from: sinsi on November 09, 2024, 09:18:13 PMIsn't that 32 byte alignment?
:biggrin:  this made me chuckle.
So does the OP need 32 bit alignment, or 32 byte alignment? A big difference. I think I know the answer.  :cool:
Ventanas diez es el mejor.  :azn:

NoCforMe

So my way is simpler. I win!

(This technique could be adapted for 32-byte alignment.)

Not this:
Quote from: Vortex on November 09, 2024, 08:12:25 PMReading your code from this message :

; Calculate actual bitmap size including any needed padding:
    MOV    EAX, BMPwidth
    MUL    BMPbpp
    ADD    EAX, 31
    AND    EAX, NOT 31
    SHR    EAX, 3
    MUL    BMPheight
    MOV    actualBMPsize, EAX

https://masm32.com/board/index.php?msg=126368

Yikes; my old code comes back to haunt me ...
Assembly language programming should be fun. That's why I do it.

Vortex

#5
Hello,

Same method used to align the stack inside a procedure ( 64-bit coding ) :

and rsp,-16

sinsi

Quote from: Vortex on November 10, 2024, 05:57:21 AMHello,

Same method used to align the stack inside a procedure ( 64-bit coding ) :

and esp,-16
That would clear the high 32 bits of RSP though...ouch
and rsp,-16
and sp,-16
and spl,-16
Tá fuinneoga a haon déag níos fearr :biggrin:

Vortex

Hi Sinsi,

Sorry for the typo, statement corrected above : and rsp,-16

mineiro

Quote from: NoCforMe on November 10, 2024, 04:59:06 AMSo my way is simpler. I win!

What did you get? More ego?
Next time, stop posting lots of messages with technical errors, this is for you and zedd151. Waiting for the "gang of 4" to come and protect members of the bubble, but not criticize him as they have done in the past. So, to be fair and honest, I'm here.
A simple topic, just see in binary and see patterns. An and with 1111 will do the rest, wow,. For a person who owns an oscilloscope this would be trivial,...
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

NoCforMe

Quote from: mineiro on November 11, 2024, 10:20:51 AM
Quote from: NoCforMe on November 10, 2024, 04:59:06 AMSo my way is simpler. I win!

What did you get? More ego?

It was a, it was a, a joke, son.
What, do I have to use smileys to indicate that?

QuoteNext time, stop posting lots of messages with technical errors, this is for you and zedd151. Waiting for the "gang of 4" to come and protect members of the bubble, but not criticize him as they have done in the past. So, to be fair and honest, I'm here.
A simple topic, just see in binary and see patterns. An and with 1111 will do the rest, wow,. For a person who owns an oscilloscope this would be trivial,...

What technical errors did I commit here? None that I can see.

I like my method, and it seems to be simpler than the other ones I've seen.

Those patterns you mention aren't obvious to me.
And what does an oscilloscope have to do with it? I'm curious, because I do own one (an old Tektronix). How can this be used to illustrate bit alignment?
Assembly language programming should be fun. That's why I do it.

mineiro

Quote from: NoCforMe on November 11, 2024, 10:30:07 AMdoes an oscilloscope have to do with it? I'm curious,
Square waves? I'm wasting my time.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

NoCforMe

OK, what specifically about square waves? I'm quite familiar with them, but have no idea what you're getting at here.
Assembly language programming should be fun. That's why I do it.

zedd151

Quote from: mineiro on November 11, 2024, 10:20:51 AMNext time, stop posting lots of messages with technical errors, this is for you and zedd151.
???
What are you even talking about?  :undecided:

"gang of four"?
Gang of Four - CCP
I am not now, or have I ever been, a member of the Chinese communist party.  :tongue:  I do own Chinese products though, of varying quality.  :smiley:

Or did you mean "gang of four" design patterns?
Gang of Four - Design Patterns
I have no clue what that is, I googled and it was one of the responses, besides the one mentioned above.  :rolleyes:

Ventanas diez es el mejor.  :azn:

NoCforMe

That's what I'm trying to figure out.
Maybe they had too much to drink.
Assembly language programming should be fun. That's why I do it.

TimoVJL

May the source be with you