The MASM Forum

General => The Campus => Topic started by: xoreaxeax on September 21, 2013, 10:39:10 AM

Title: Question on NOT
Post by: xoreaxeax on September 21, 2013, 10:39:10 AM
Are the the following lines equivalent?

and eax, NOT 31

and eax,-32

I found the first line in some code that converts a bitmap image to a file and was just wondering why it was written that way.

I also found the formula in C and it looks like they do it the same exact way too:

int bytes=(((24*bitmap_dx+31)&(~31)/8)*bitmap_dy;
                                                  ^^^^

Title: Re: Question on NOT
Post by: qWord on September 21, 2013, 10:46:21 AM
Quote from: xoreaxeax on September 21, 2013, 10:39:10 AM
Are the the following lines equivalent?
yes.
See also Two's complement (http://en.wikipedia.org/wiki/Twos_complement)
Title: Re: Question on NOT
Post by: dedndave on September 21, 2013, 12:36:55 PM
they are the same, in terms of math
but - slightly different, if you were to want to understand or modify the code, later
i prefer the -32 form   :P

as for NOT 31, why not just say 0FFFFFFE0h

i am pretty sure these instructions all code as 3-byte opcodes, no matter how you write it

the end result is....
the length of each line (row) in bitmap image data is always evenly divisable by 4
i.e., if the image data for a line requires 15 bytes, the line data must be 16 bytes in length
it's one of the "rules" for bitmap image data
Title: Re: Question on NOT
Post by: jj2007 on September 21, 2013, 04:42:22 PM
Look at the binary representations:

  PrintLine Bin$(-32)
  PrintLine Bin$(not 31, f)


Result:
  11111111111111111111111111100000
  11111111111111111111111111100000
  10987654321098765432109876543210


By the way, and al, not 31 and and al, -32 also do the job (but they might be slower).
Title: Re: Question on NOT
Post by: dedndave on September 21, 2013, 11:09:33 PM
yes - i use AND AL,-4, and it seems to work well

int bytes=(((24*bitmap_dx+31)&(~31)/8)*bitmap_dy

seems like the equation is a little over-complicated, to begin with
for a 24-bit image....
(((3 * bitmap_dx) +3) AND -4) * bitmap_dy
you could also use
((3 * (bitmap_dx +1)) AND -4) * bitmap_dy