The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: cman on October 25, 2007, 06:39:49 PM

Title: Indirect Jumps
Post by: cman on October 25, 2007, 06:39:49 PM
How can instruction lengths be calculated for any instruction and choice of operands? I want to use the calculations to perform indirect jumps over groups of instructions. Thanks for any input. :U
Title: Re: Indirect Jumps
Post by: MichaelW on October 25, 2007, 08:14:44 PM
This won't work for any instruction or choice of operands, but it will work for many/most.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    ilen MACRO mnemonic,dest,source
      LOCAL lbl, sz
      .code junk
        lbl:
          IFNB <dest>
            IFNB <source>
              mnemonic dest,source
            ELSE
              mnemonic dest
            ENDIF
          ELSE
            mnemonic
          ENDIF
          sz = $-lbl
      .code
       EXITM <sz>
    ENDM
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
 
    print ustr$(ilen(mov, eax, 1)),13,10
    print ustr$(ilen(ror, eax, 3)),13,10
    print ustr$(ilen(xor, ecx, ecx)),13,10
    print ustr$(ilen(push, 123)),13,10
    print ustr$(ilen(inc, edx)),13,10
    print ustr$(ilen(clc)),13,10
    print ustr$(ilen(finit)),13,10
   
    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
.code junk
end start

Title: Re: Indirect Jumps
Post by: Rockoon on October 26, 2007, 05:08:13 AM
masm can produce a listing file, in which will be the actual instruction encoding that will be used. You can also find other information inside the listing such as how any of your macros got expanded.

I suspect that most other assemblers offer this sort of feature as well.

You should infact be able to write a macro which will insert a jump if and only if some distance condition is met. As you see in the above response the $ directive in masm returns the current position counter into the current output segment..
Title: Re: Indirect Jumps
Post by: cman on October 29, 2007, 05:41:20 PM
Thanks for the information! As I remember the length of instructions can be calculated by hand as well ( a certain number of bits for mnemonics and a certain number for operands of different sizes and types ) . Does anyone know the numbers to calculate the instruction length by hand? Thanks for any information....
Title: Re: Indirect Jumps
Post by: u on October 29, 2007, 08:30:33 PM
the MASM32 package comes with opcodes.hlp, which shows the bytes of each mnemonic.
Btw, a reminder: for jumping to an arbitrary address, loaded from a variable, the basic trick is

push ecx ; ecx = the address to jump to
retn