News:

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

Indirect Jumps

Started by cman, October 25, 2007, 06:39:49 PM

Previous topic - Next topic

cman

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

MichaelW

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

eschew obfuscation

Rockoon

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..
When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

cman

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....

u

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
Please use a smaller graphic in your signature.