Data Packing

I was doing some random experiments using assembly and C. This is a simple example in packing 2 numbers inside a register in assembly. Let’s assume

al < 2^{5}
bl < 2^{3}

We want to store these 2 values inside the dl register. The dl register is of 8 bits, so 5 + 3 = 8 bits


; al < 2 ^ 5, bl < 2 ^ 3
mov al, bl
shl dl, 3
or dl, bl ; xor would also work fine


mov cl, dl ; Make a copy of dl
and dl, 111b ; Extract lowest 3 bits (b)
mov bl, dl
shr cl, 3 ; Extract highest 5 bits (a)
mov al, cl

After experimenting with this I thought of coding a high level version of this using C. In here we pack and unpack a DWORD and a short inside a DWORD64 variable and build a string value and again unpack the DWORD64 value back to their original variables with the original values.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s