The MASM Forum

General => The Laboratory => Topic started by: jj2007 on September 16, 2013, 05:42:37 PM

Title: Long version of add reg32 encoding
Post by: jj2007 on September 16, 2013, 05:42:37 PM
  mov edx, MbQsAsc   ; addr of sort proc; default is ascending
  test eax, eax
  .if Sign?
   add edx, MbQsDesc-MbQsAsc
  .endif
...
MbQsAsc proc
...
MbQsDesc proc


Olly says this is add edx, 16h
However, all ML versions and JWasm use the long form, i.e.
81C2 16000000          add edx, 16

Is that because the difference in offsets becomes known only at runtime? But that would apply also to jmp instructions... :(
Title: Re: Long version of add reg32 encoding
Post by: japheth on September 16, 2013, 06:09:44 PM
Quote from: jj2007 on September 16, 2013, 05:42:37 PM
Is that because the difference in offsets becomes known only at runtime? But that would apply also to jmp instructions... :(

The difference of two local labels is known at assembly-time. It has to be, because there is no relocation type to tell the linker how to compute the difference.

Additionally, the local labels must be in the same section.

If they aren't


.386
.model flat

.code x1
lbl1:
.code x2
lbl2:
mov eax, lbl2 - lbl1

end


you'll get error 'operands must be in the same segment'
Title: Re: Long version of add reg32 encoding
Post by: jj2007 on September 16, 2013, 07:35:52 PM
Quote from: japheth on September 16, 2013, 06:09:44 PM
The difference of two local labels is known at assembly-time.

So the assembler could choose the shorter encoding?
Title: Re: Long version of add reg32 encoding
Post by: japheth on September 16, 2013, 10:24:48 PM
Quote from: jj2007 on September 16, 2013, 07:35:52 PM
So the assembler could choose the shorter encoding?

Actually, jwasm does and masm could.


                                .386
                                .model flat

                                .code

00000000                        start:

00000000  83C001                add eax, lbl2-lbl1
00000003  0501000000            add eax, dword ptr (lbl2-lbl1)
00000008  83C001                add eax, 1
0000000B  0501000000            add eax, dword ptr 1
00000010                        lbl1:
00000010  00                    db 1 dup (0)
00000011                        lbl2:

                                end start

Title: Re: Long version of add reg32 encoding
Post by: dedndave on September 16, 2013, 10:32:18 PM
don't suppose you tried to override it
   add edx,byte ptr (MbQsDesc-MbQsAsc)
or
   add edx,sbyte ptr (MbQsDesc-MbQsAsc)
or maybe
   add edx,sdword ptr (MbQsDesc-MbQsAsc)

maybe just the parens - they could force evaluation before the size is determined
   add edx,(MbQsDesc-MbQsAsc)
Title: Re: Long version of add reg32 encoding
Post by: qWord on September 16, 2013, 10:37:40 PM
a small macro should do MASM's job:
add_imm macro a,b
    IF b GE -128 AND b LE 127
        add a,0
        org $-1
        db b
    ELSE
        add a,b
    ENDIF
endm
Title: Re: Long version of add reg32 encoding
Post by: dedndave on September 16, 2013, 10:46:05 PM
another idea...
MbQsDescAsc = MbQsDesc-MbQsAsc
   add edx,MbQsDescAsc
Title: Re: Long version of add reg32 encoding
Post by: jj2007 on September 17, 2013, 12:57:31 AM
Quote from: qWord on September 16, 2013, 10:37:40 PM
a small macro should do MASM's job:

You are cheating :eusa_naughty:
But it works :t

However, it complains if the label is defined after the add_imm call. No problem ;-)

Quote from: dedndave on September 16, 2013, 10:46:05 PM
another idea...
MbQsDescAsc = MbQsDesc-MbQsAsc
   add edx,MbQsDescAsc


No luck, unfortunately ...

As to JWasm, it seems I had tried an add edx, dword ptr (MbQsDesc-MbQsAsc), which forces the long encoding. The standard version is indeed short :t
Title: Re: Long version of add reg32 encoding
Post by: daydreamer on September 17, 2013, 02:13:09 AM
but isnt inc eax shortest?
Title: Re: Long version of add reg32 encoding
Post by: dedndave on September 17, 2013, 04:34:19 AM
not if you need 16h of them   :biggrin:
loop00: inc     edx
        cmp     edx,offset MbQsDesc
        jb      loop00