News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Bad bug in MASM? Or newbie misunderstanding?

Started by cork, July 23, 2010, 06:08:05 AM

Previous topic - Next topic

cork

Why does MASM encode "CMP eax, 4294967295" as "83 F8 FF"?

00000000  83 F8 FF          cmp EAX, 4294967295

I used: "ml /c /Fl test2.asm" to generate the listing.

If the number is >= 4294967168 and <= 4294967295, masm gives me the bad encoding. For all other 32-bit immediate operands, it gives me the correct encoding.

That is 128 numbers for which MASM produces the bad encoding.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here's the output listing so you can see the abrupt transition in how MASM encodes an instruction.

00000000  83 F8 FF          cmp EAX, 4294967295
00000003  83 F8 FE          cmp EAX, 4294967294
00000006  83 F8 FD          cmp EAX, 4294967293
00000009  83 F8 FC          cmp EAX, 4294967292
0000000C  83 F8 FB          cmp EAX, 4294967291
0000000F  83 F8 FA          cmp EAX, 4294967290
00000012  83 F8 F9          cmp EAX, 4294967289
00000015  83 F8 F8          cmp EAX, 4294967288
00000018  83 F8 F7          cmp EAX, 4294967287
0000001B  83 F8 F6          cmp EAX, 4294967286
0000001E  83 F8 F5          cmp EAX, 4294967285
00000021  83 F8 F4          cmp EAX, 4294967284
00000024  83 F8 F3          cmp EAX, 4294967283
00000027  83 F8 F2          cmp EAX, 4294967282
0000002A  83 F8 F1          cmp EAX, 4294967281
0000002D  83 F8 F0          cmp EAX, 4294967280

00000030  83 F8 EF          cmp EAX, 4294967279
00000033  83 F8 EE          cmp EAX, 4294967278
00000036  83 F8 ED          cmp EAX, 4294967277
00000039  83 F8 EC          cmp EAX, 4294967276
0000003C  83 F8 EB          cmp EAX, 4294967275
0000003F  83 F8 EA          cmp EAX, 4294967274
00000042  83 F8 E9          cmp EAX, 4294967273
00000045  83 F8 E8          cmp EAX, 4294967272
00000048  83 F8 E7          cmp EAX, 4294967271
0000004B  83 F8 E6          cmp EAX, 4294967270

0000004E  83 F8 E5          cmp EAX, 4294967269
00000051  83 F8 E4          cmp EAX, 4294967268
00000054  83 F8 E3          cmp EAX, 4294967267
00000057  83 F8 E2          cmp EAX, 4294967266
0000005A  83 F8 E1          cmp EAX, 4294967265
0000005D  83 F8 E0          cmp EAX, 4294967264
00000060  83 F8 DF          cmp EAX, 4294967263
00000063  83 F8 DE          cmp EAX, 4294967262
00000066  83 F8 DD          cmp EAX, 4294967261
00000069  83 F8 DC          cmp EAX, 4294967260

0000006C  83 F8 DB          cmp EAX, 4294967259
0000006F  83 F8 DA          cmp EAX, 4294967258
00000072  83 F8 D9          cmp EAX, 4294967257
00000075  83 F8 D8          cmp EAX, 4294967256
00000078  83 F8 D7          cmp EAX, 4294967255
0000007B  83 F8 D6          cmp EAX, 4294967254
0000007E  83 F8 D5          cmp EAX, 4294967253
00000081  83 F8 D4          cmp EAX, 4294967252
00000084  83 F8 D3          cmp EAX, 4294967251
00000087  83 F8 D2          cmp EAX, 4294967250

0000008A  83 F8 D1          cmp EAX, 4294967249
0000008D  83 F8 D0          cmp EAX, 4294967248
00000090  83 F8 CF          cmp EAX, 4294967247
00000093  83 F8 CE          cmp EAX, 4294967246
00000096  83 F8 CD          cmp EAX, 4294967245
00000099  83 F8 CC          cmp EAX, 4294967244
0000009C  83 F8 CB          cmp EAX, 4294967243
0000009F  83 F8 CA          cmp EAX, 4294967242
000000A2  83 F8 C9          cmp EAX, 4294967241
000000A5  83 F8 C8          cmp EAX, 4294967240

000000A8  83 F8 C7          cmp EAX, 4294967239
000000AB  83 F8 C6          cmp EAX, 4294967238
000000AE  83 F8 C5          cmp EAX, 4294967237
000000B1  83 F8 C4          cmp EAX, 4294967236
000000B4  83 F8 C3          cmp EAX, 4294967235
000000B7  83 F8 C2          cmp EAX, 4294967234
000000BA  83 F8 C1          cmp EAX, 4294967233
000000BD  83 F8 C0          cmp EAX, 4294967232
000000C0  83 F8 BF          cmp EAX, 4294967231
000000C3  83 F8 BE          cmp EAX, 4294967230
               
000000C6  83 F8 BD          cmp EAX, 4294967229
000000C9  83 F8 BC          cmp EAX, 4294967228
000000CC  83 F8 BB          cmp EAX, 4294967227
000000CF  83 F8 BA          cmp EAX, 4294967226
000000D2  83 F8 B9          cmp EAX, 4294967225
000000D5  83 F8 B8          cmp EAX, 4294967224
000000D8  83 F8 B7          cmp EAX, 4294967223
000000DB  83 F8 B6          cmp EAX, 4294967222
000000DE  83 F8 B5          cmp EAX, 4294967221
000000E1  83 F8 B4          cmp EAX, 4294967220

000000E4  83 F8 B3          cmp EAX, 4294967219
000000E7  83 F8 B2          cmp EAX, 4294967218
000000EA  83 F8 B1          cmp EAX, 4294967217
000000ED  83 F8 B0          cmp EAX, 4294967216
000000F0  83 F8 AF          cmp EAX, 4294967215
000000F3  83 F8 AE          cmp EAX, 4294967214
000000F6  83 F8 AD          cmp EAX, 4294967213
000000F9  83 F8 AC          cmp EAX, 4294967212
000000FC  83 F8 AB          cmp EAX, 4294967211
000000FF  83 F8 AA          cmp EAX, 4294967210

00000102  83 F8 A9          cmp EAX, 4294967209
00000105  83 F8 A8          cmp EAX, 4294967208
00000108  83 F8 A7          cmp EAX, 4294967207
0000010B  83 F8 A6          cmp EAX, 4294967206
0000010E  83 F8 A5          cmp EAX, 4294967205
00000111  83 F8 A4          cmp EAX, 4294967204
00000114  83 F8 A3          cmp EAX, 4294967203
00000117  83 F8 A2          cmp EAX, 4294967202
0000011A  83 F8 A1          cmp EAX, 4294967201
0000011D  83 F8 A0          cmp EAX, 4294967200

00000120  83 F8 9F          cmp EAX, 4294967199
00000123  83 F8 9E          cmp EAX, 4294967198
00000126  83 F8 9D          cmp EAX, 4294967197
00000129  83 F8 9C          cmp EAX, 4294967196
0000012C  83 F8 9B          cmp EAX, 4294967195
0000012F  83 F8 9A          cmp EAX, 4294967194
00000132  83 F8 99          cmp EAX, 4294967193
00000135  83 F8 98          cmp EAX, 4294967192
00000138  83 F8 97          cmp EAX, 4294967191
0000013B  83 F8 96          cmp EAX, 4294967190

0000013E  83 F8 95          cmp EAX, 4294967189
00000141  83 F8 94          cmp EAX, 4294967188
00000144  83 F8 93          cmp EAX, 4294967187
00000147  83 F8 92          cmp EAX, 4294967186
0000014A  83 F8 91          cmp EAX, 4294967185
0000014D  83 F8 90          cmp EAX, 4294967184
00000150  83 F8 8F          cmp EAX, 4294967183
00000153  83 F8 8E          cmp EAX, 4294967182
00000156  83 F8 8D          cmp EAX, 4294967181
00000159  83 F8 8C          cmp EAX, 4294967180

0000015C  83 F8 8B          cmp EAX, 4294967179
0000015F  83 F8 8A          cmp EAX, 4294967178
00000162  83 F8 89          cmp EAX, 4294967177
00000165  83 F8 88          cmp EAX, 4294967176
00000168  83 F8 87          cmp EAX, 4294967175
0000016B  83 F8 86          cmp EAX, 4294967174
0000016E  83 F8 85          cmp EAX, 4294967173
00000171  83 F8 84          cmp EAX, 4294967172
00000174  83 F8 83          cmp EAX, 4294967171
00000177  83 F8 82          cmp EAX, 4294967170

0000017A  83 F8 81          cmp EAX, 4294967169
0000017D  83 F8 80          cmp EAX, 4294967168
00000180  3D FFFFFF7F          cmp EAX, 4294967167
00000185  3D FFFFFF7E          cmp EAX, 4294967166
0000018A  3D FFFFFF7D          cmp EAX, 4294967165
0000018F  3D FFFFFF7C          cmp EAX, 4294967164
00000194  3D FFFFFF7B          cmp EAX, 4294967163
00000199  3D FFFFFF7A          cmp EAX, 4294967162
0000019E  3D FFFFFF79          cmp EAX, 4294967161
000001A3  3D FFFFFF78          cmp EAX, 4294967160

000001A8  3D FFFFFF77          cmp EAX, 4294967159
000001AD  3D FFFFFF76          cmp EAX, 4294967158
000001B2  3D FFFFFF75          cmp EAX, 4294967157
000001B7  3D FFFFFF74          cmp EAX, 4294967156
000001BC  3D FFFFFF73          cmp EAX, 4294967155
000001C1  3D FFFFFF72          cmp EAX, 4294967154
000001C6  3D FFFFFF71          cmp EAX, 4294967153
000001CB  3D FFFFFF70          cmp EAX, 4294967152
000001D0  3D FFFFFF6F          cmp EAX, 4294967151
000001D5  3D FFFFFF6E          cmp EAX, 4294967150

000001DA  3D FFFFFF6D          cmp EAX, 4294967149
000001DF  3D FFFFFF6C          cmp EAX, 4294967148
000001E4  3D FFFFFF6B          cmp EAX, 4294967147
000001E9  3D FFFFFF6A          cmp EAX, 4294967146
000001EE  3D FFFFFF69          cmp EAX, 4294967145
000001F3  3D FFFFFF68          cmp EAX, 4294967144
000001F8  3D FFFFFF67          cmp EAX, 4294967143
000001FD  3D FFFFFF66          cmp EAX, 4294967142
00000202  3D FFFFFF65          cmp EAX, 4294967141
00000207  3D FFFFFF64          cmp EAX, 4294967140

0000020C  3D FFFFFF63          cmp EAX, 4294967139
00000211  3D FFFFFF62          cmp EAX, 4294967138
00000216  3D FFFFFF61          cmp EAX, 4294967137
0000021B  3D FFFFFF60          cmp EAX, 4294967136
00000220  3D FFFFFF5F          cmp EAX, 4294967135
00000225  3D FFFFFF5E          cmp EAX, 4294967134
0000022A  3D FFFFFF5D          cmp EAX, 4294967133
0000022F  3D FFFFFF5C          cmp EAX, 4294967132
00000234  3D FFFFFF5B          cmp EAX, 4294967131
00000239  3D FFFFFF5A          cmp EAX, 4294967130

0000023E  3D FFFFFF59          cmp EAX, 4294967129
00000243  3D FFFFFF58          cmp EAX, 4294967128
00000248  3D FFFFFF57          cmp EAX, 4294967127
0000024D  3D FFFFFF56          cmp EAX, 4294967126
00000252  3D FFFFFF55          cmp EAX, 4294967125
00000257  3D FFFFFF54          cmp EAX, 4294967124
0000025C  3D FFFFFF53          cmp EAX, 4294967123
00000261  3D FFFFFF52          cmp EAX, 4294967122
00000266  3D FFFFFF51          cmp EAX, 4294967121
0000026B  3D FFFFFF50          cmp EAX, 4294967120

00000270  3D FFFFFF4F          cmp EAX, 4294967119
00000275  3D FFFFFF4E          cmp EAX, 4294967118
0000027A  3D FFFFFF4D          cmp EAX, 4294967117
0000027F  3D FFFFFF4C          cmp EAX, 4294967116
00000284  3D FFFFFF4B          cmp EAX, 4294967115
00000289  3D FFFFFF4A          cmp EAX, 4294967114
0000028E  3D FFFFFF49          cmp EAX, 4294967113
00000293  3D FFFFFF48          cmp EAX, 4294967112
00000298  3D FFFFFF47          cmp EAX, 4294967111
0000029D  3D FFFFFF46          cmp EAX, 4294967110

000002A2  3D FFFFFF45          cmp EAX, 4294967109
000002A7  3D FFFFFF44          cmp EAX, 4294967108
000002AC  3D FFFFFF43          cmp EAX, 4294967107
000002B1  3D FFFFFF42          cmp EAX, 4294967106
000002B6  3D FFFFFF41          cmp EAX, 4294967105
000002BB  3D FFFFFF40          cmp EAX, 4294967104
000002C0  3D FFFFFF3F          cmp EAX, 4294967103
000002C5  3D FFFFFF3E          cmp EAX, 4294967102
000002CA  3D FFFFFF3D          cmp EAX, 4294967101
000002CF  3D FFFFFF3C          cmp EAX, 4294967100

jj2007

Not a bug. It's a feature.
Seriously: the range from -128 ... +127 has shorter encodings for many instructions. Test e.g. add eax, -1 against add eax, -129. Unfortunately, mov is not one of them.

cork

Quote from: jj2007 on July 23, 2010, 06:12:21 AM
Not a bug. It's a feature.
Seriously: the range from -128 ... +127 has shorter encodings for many instructions. Test e.g. add eax, -1 against add eax, -129. Unfortunately, mov is not one of them.


Instead of "cmp EAX, 4294967295", can I indicate to MASM that "4294967295" is indeed a positive integer and that it should treat it as such?

sinsi

Really, they are the same to masm and the cpu. The thing that is different about them is how you use the flags after a compare, ja/jb is unsigned, jg/jl is signed.
Light travels faster than sound, that's why some people seem bright until you hear them.

Geryon

Quote from: cork on July 23, 2010, 06:18:19 AM
Instead of "cmp EAX, 4294967295", can I indicate to MASM that "4294967295" is indeed a positive integer and that it should treat it as such?
It can't be a positive integer. Because there is not enough bits to indicate its sign. So It must be a unsigned integer.
"Some people have got a mental horizon of radius zero and call it their point of view." --D.Hilbert

cork

Quote from: Geryon on July 23, 2010, 06:41:12 AM
Quote from: cork on July 23, 2010, 06:18:19 AM
Instead of "cmp EAX, 4294967295", can I indicate to MASM that "4294967295" is indeed a positive integer and that it should treat it as such?
It can't be a positive integer. Because there is not enough bits to indicate its sign. So It must be a unsigned integer.

The bottom line is that I'd like to use CMP to produce an encoding of "3D FFFFFFFF". Is there a way, using MASM syntax, to accomplish this?

dedndave

of course it can be signed
the value itself is not necessarily signed or unsigned, so much as the context in how it is used
the byte-sized values are "logically" sign-extended to full width prior to any math operation
it is the nature of twos-compliment that adding 1 is the same as subtracting -1   :U

cork - if you want to force it, you might try assigning the value to an equate using "="
that way, the assembler will make no assumptions about the size of the constant

japheth

Quote from: cork on July 23, 2010, 06:18:19 AM
Instead of "cmp EAX, 4294967295", can I indicate to MASM that "4294967295" is indeed a positive integer and that it should treat it as such?

If you want to force Masm to use the longer encoding, use

cmp EAX, dword ptr 4294967295


cork

Quote from: japheth on July 23, 2010, 07:05:57 AM
If you want to force Masm to use the longer encoding, use

cmp EAX, dword ptr 4294967295


Thank you japheth. That works perfectly.

(I wish I didn't have to remember to do that, but such is life.)

Geryon

Quote from: dedndave on July 23, 2010, 07:04:13 AM
of course it can be signed
the value itself is not necessarily signed or unsigned, so much as the context in how it is used
the byte-sized values are "logically" sign-extended to full width prior to any math operation
it is the nature of twos-compliment that adding 1 is the same as subtracting -1   :U
Ok, let me give a more detailed example then.
All signed integers must be 31 bit wide, not 32 bit, because the most significant bit is indicating the sign.
FFFFFFFFh = a signed -1 or a unsigned 4294967295 (2^32 -1)

So, a register can not contain -4294967295
max = + or - 2147483647 (2^31 -1)
"Some people have got a mental horizon of radius zero and call it their point of view." --D.Hilbert

dedndave

you are right, of course   :bg
but, you can use the same 32 bit register to hold:

signed values from -2147483648 to +2147483647

OR

unsigned values from 0 to 4294967295

as you can see, it is a matter of interpretation
if the value is used in a signed context, bit 31 is the sign bit
if the same value is used in an unsigned context, bit 31 is the most-signifigant-bit

clive

It could be a random act of randomness. Those happen a lot as well.