The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: whakamaru on November 28, 2011, 10:04:16 PM

Title: compiling masm to intel... and loops
Post by: whakamaru on November 28, 2011, 10:04:16 PM
The following is an instruction in a program, where SymSIZE has previously been defined as EQU 4*10
add eax, [esp][ecx*4][SymSIZE]

I wonder what the Intel instructions are?  Several?
Also, I have not seen the LOOP instruction used.  People seem to prefer to set ECX, then DEC ECX and JNZ or JNS
Is there a reason for this?  Are the jumps still restricted to +/- 80h?
Title: Re: compiling masm to intel... and loops
Post by: qWord on November 28, 2011, 10:44:18 PM
Quote from: whakamaru on November 28, 2011, 10:04:16 PM
The following is an instruction in a program, where SymSIZE has previously been defined as EQU 4*10
add eax, [esp][ecx*4][SymSIZE]

I wonder what the Intel instructions are?  Several?
This is a instruction, which is using SIB-Addressing (Scale Index Base). ESP is the base, ECX the index which is scaled by 4 and SymSIZE is a displacement (=Offset). A syntax variantion:
add eax,[esp+ecx*4+4*10]

Quote from: whakamaru on November 28, 2011, 10:04:16 PM
Also, I have not seen the LOOP instruction used.  People seem to prefer to set ECX, then DEC ECX and JNZ or JNS
Is there a reason for this?
This instruction is obsolete (and slow) and should not be used (see AMD'S optimization Manual)

Quote from: whakamaru on November 28, 2011, 10:04:16 PM
Are the jumps still restricted to +/- 80h?
16 and 32 bit offset are also possible.
Title: Re: compiling masm to intel... and loops
Post by: FORTRANS on November 28, 2011, 11:06:08 PM
Quote from: whakamaru on November 28, 2011, 10:04:16 PM
Also, I have not seen the LOOP instruction used.  People seem to prefer to set ECX, then DEC ECX and JNZ or JNS
Is there a reason for this?  Are the jumps still restricted to +/- 80h?

Hi,

   The choice of using or lot using LOOP is personal preference.
As qWord said, it is slower.  I tend to use it as it saves on some
typing.  As to jumps, no you are not limited to 80H (SHORT).
If you enable 32-bit instructions, you can use NEAR jumps.
But if in real mode (MS-DOS), you have to be careful not to
jump out of the current segment.

Regards,

Steve N.
Title: Re: compiling masm to intel... and loops
Post by: MichaelW on November 29, 2011, 04:18:31 AM
Short jumps are not restricted to +/- 80h but to -128/+127, the range of a signed byte.
Title: Re: compiling masm to intel... and loops
Post by: clive on November 29, 2011, 04:10:26 PM
Quote from: MichaelWShort jumps are not restricted to +/- 80h but to -128/+127, the range of a signed byte.

But it's relative to the NEXT instruction, so fun like "JMP $+81h" is valid, and "JMP $-80h" is not encodable with 8-bits, and uses the 32-bit form instead.

Quote from: whakamaruThe following is an instruction in a program, where SymSIZE has previously been defined as EQU 4*10
add eax, [esp][ecx*4][SymSIZE]

I wonder what the Intel instructions are?  Several?

Well technically it's a SINGLE instruction, but the opcode/machine-code spans several bytes. Use the -Fl option of MASM to generate a listing to see the codes.

00000000  EB 7F         JMP $+81h
00000002  E9 FFFFFF7B         JMP $-80h

= 00000028 SymSIZE equ 4*10

00000007  03 44 8C 28         add     eax, [esp][ecx*4][SymSIZE]
Title: Re: compiling masm to intel... and loops
Post by: hutch-- on December 02, 2011, 12:49:07 AM
Hi whakamura,

Good to see another Kiwi in the place.

With the notation,


add eax, [esp][ecx*4][SymSIZE]


the paired square brackets function like an addition operator.


add eax, [esp][ecx*4][SymSIZE]
add eax, [esp+ecx*4+SymSIZE]


The capacity is useful for readability when for example you need to correct ESP for changes in the stack with PUSH or POP.


mov eax, [esp+4][4]
push eax
mov ecx, [esp+4][8]


RE: The use of the old LOOP instruction, it is rarely ever used these days as it is much slower than a CMP or TEST then branching back to a label. On modern processors you have the difference between preferred instructions that are hard coded in silicon and old instructions that are constructed in microcode which work as documented but perform poorly, LOOP is one of those old instructions.