Author Topic: Long version of add reg32 encoding  (Read 5678 times)

jj2007

  • Member
  • *****
  • Posts: 11476
  • Assembler is fun ;-)
    • MasmBasic
Long version of add reg32 encoding
« 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... :(

japheth

  • Guest
Re: Long version of add reg32 encoding
« Reply #1 on: September 16, 2013, 06:09:44 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

Code: [Select]
.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'

jj2007

  • Member
  • *****
  • Posts: 11476
  • Assembler is fun ;-)
    • MasmBasic
Re: Long version of add reg32 encoding
« Reply #2 on: September 16, 2013, 07:35:52 PM »
The difference of two local labels is known at assembly-time.

So the assembler could choose the shorter encoding?

japheth

  • Guest
Re: Long version of add reg32 encoding
« Reply #3 on: September 16, 2013, 10:24:48 PM »
So the assembler could choose the shorter encoding?

Actually, jwasm does and masm could.

Code: [Select]
                                .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

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: Long version of add reg32 encoding
« Reply #4 on: September 16, 2013, 10:32:18 PM »
don't suppose you tried to override it
Code: [Select]
   add edx,byte ptr (MbQsDesc-MbQsAsc)or
Code: [Select]
   add edx,sbyte ptr (MbQsDesc-MbQsAsc)or maybe
Code: [Select]
   add edx,sdword ptr (MbQsDesc-MbQsAsc)
maybe just the parens - they could force evaluation before the size is determined
Code: [Select]
   add edx,(MbQsDesc-MbQsAsc)

qWord

  • Member
  • *****
  • Posts: 1476
  • The base type of a type is the type itself
    • SmplMath macros
Re: Long version of add reg32 encoding
« Reply #5 on: September 16, 2013, 10:37:40 PM »
a small macro should do MASM's job:
Code: [Select]
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
MREAL macros - when you need floating point arithmetic while assembling!

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: Long version of add reg32 encoding
« Reply #6 on: September 16, 2013, 10:46:05 PM »
another idea...
Code: [Select]
MbQsDescAsc = MbQsDesc-MbQsAsc
   add edx,MbQsDescAsc

jj2007

  • Member
  • *****
  • Posts: 11476
  • Assembler is fun ;-)
    • MasmBasic
Re: Long version of add reg32 encoding
« Reply #7 on: September 17, 2013, 12:57:31 AM »
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 ;-)

another idea...
Code: [Select]
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

daydreamer

  • Member
  • *****
  • Posts: 1635
  • building nextdoor
Re: Long version of add reg32 encoding
« Reply #8 on: September 17, 2013, 02:13:09 AM »
but isnt inc eax shortest?
SIMD fan and macro fan
why assembly is fastest is because its switch has no (brakes) breaks
:P
only in 16bit assembly you can get away with "Only words" :P

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: Long version of add reg32 encoding
« Reply #9 on: September 17, 2013, 04:34:19 AM »
not if you need 16h of them   :biggrin:
Code: [Select]
loop00: inc     edx
        cmp     edx,offset MbQsDesc
        jb      loop00