The MASM Forum

General => The Laboratory => Topic started by: hutch-- on April 03, 2014, 01:13:37 AM

Title: Jump inversion algorithm.
Post by: hutch-- on April 03, 2014, 01:13:37 AM
I thought someone may like this, its an algo to invert a conditional jump to its inverse (JZ - JNZ) etc ....


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

jump_inversion proc ptxt:DWORD

    mov eax, [esp+4]

    cmp BYTE PTR [eax+0], "j"
    jne notfound
    cmp BYTE PTR [eax+1], "a"
    jne lbl0
    cmp BYTE PTR [eax+2], 0
    jne lbl1
  ; -------------------
    .data
      ptr_jna db "jna",0
    .code
    mov eax, OFFSET ptr_jna  ; ja
    ret 4
  ; -------------------
  lbl1:
    cmp BYTE PTR [eax+2], "e"
    jne notfound
    cmp BYTE PTR [eax+3], 0
    jne notfound
  ; -------------------
    .data
      ptr_jnae db "jnae",0
    .code
    mov eax, OFFSET ptr_jnae  ; jae
    ret 4
  ; -------------------
  lbl0:
    cmp BYTE PTR [eax+1], "b"
    jne lbl2
    cmp BYTE PTR [eax+2], 0
    jne lbl3
  ; -------------------
    .data
      ptr_jnb db "jnb",0
    .code
    mov eax, OFFSET ptr_jnb  ; jb
    ret 4
  ; -------------------
  lbl3:
    cmp BYTE PTR [eax+2], "e"
    jne notfound
    cmp BYTE PTR [eax+3], 0
    jne notfound
  ; -------------------
    .data
      ptr_jnbe db "jnbe",0
    .code
    mov eax, OFFSET ptr_jnbe  ; jbe
    ret 4
  ; -------------------
  lbl2:
    cmp BYTE PTR [eax+1], "c"
    jne lbl4
    cmp BYTE PTR [eax+2], 0
    jne notfound
  ; -------------------
    .data
      ptr_jnc db "jnc",0
    .code
    mov eax, OFFSET ptr_jnc  ; jc
    ret 4
  ; -------------------
  lbl4:
    cmp BYTE PTR [eax+1], "e"
    jne lbl5
    cmp BYTE PTR [eax+2], 0
    jne notfound
  ; -------------------
    .data
      ptr_jne db "jne",0
    .code
    mov eax, OFFSET ptr_jne  ; je
    ret 4
  ; -------------------
  lbl5:
    cmp BYTE PTR [eax+1], "g"
    jne lbl6
    cmp BYTE PTR [eax+2], 0
    jne lbl7
  ; -------------------
    .data
      ptr_jng db "jng",0
    .code
    mov eax, OFFSET ptr_jng  ; jg
    ret 4
  ; -------------------
  lbl7:
    cmp BYTE PTR [eax+2], "e"
    jne notfound
    cmp BYTE PTR [eax+3], 0
    jne notfound
  ; -------------------
    .data
      ptr_jnge db "jnge",0
    .code
    mov eax, OFFSET ptr_jnge  ; jge
    ret 4
  ; -------------------
  lbl6:
    cmp BYTE PTR [eax+1], "l"
    jne lbl8
    cmp BYTE PTR [eax+2], 0
    jne lbl9
  ; -------------------
    .data
      ptr_jnl db "jnl",0
    .code
    mov eax, OFFSET ptr_jnl  ; jl
    ret 4
  ; -------------------
  lbl9:
    cmp BYTE PTR [eax+2], "e"
    jne notfound
    cmp BYTE PTR [eax+3], 0
    jne notfound
  ; -------------------
    .data
      ptr_jnle db "jnle",0
    .code
    mov eax, OFFSET ptr_jnle  ; jle
    ret 4
  ; -------------------
  lbl8:
    cmp BYTE PTR [eax+1], "n"
    jne lbl10
    cmp BYTE PTR [eax+2], "a"
    jne lbl11
    cmp BYTE PTR [eax+3], 0
    jne lbl12
  ; -------------------
    .data
      ptr_ja db "ja",0
    .code
    mov eax, OFFSET ptr_ja  ; jna
    ret 4
  ; -------------------
  lbl12:
    cmp BYTE PTR [eax+3], "e"
    jne notfound
    cmp BYTE PTR [eax+4], 0
    jne notfound
  ; -------------------
    .data
      ptr_jae db "jae",0
    .code
    mov eax, OFFSET ptr_jae  ; jnae
    ret 4
  ; -------------------
  lbl11:
    cmp BYTE PTR [eax+2], "b"
    jne lbl13
    cmp BYTE PTR [eax+3], 0
    jne lbl14
  ; -------------------
    .data
      ptr_jb db "jb",0
    .code
    mov eax, OFFSET ptr_jb  ; jnb
    ret 4
  ; -------------------
  lbl14:
    cmp BYTE PTR [eax+3], "e"
    jne notfound
    cmp BYTE PTR [eax+4], 0
    jne notfound
  ; -------------------
    .data
      ptr_jbe db "jbe",0
    .code
    mov eax, OFFSET ptr_jbe  ; jnbe
    ret 4
  ; -------------------
  lbl13:
    cmp BYTE PTR [eax+2], "c"
    jne lbl15
    cmp BYTE PTR [eax+3], 0
    jne notfound
  ; -------------------
    .data
      ptr_jc db "jc",0
    .code
    mov eax, OFFSET ptr_jc  ; jnc
    ret 4
  ; -------------------
  lbl15:
    cmp BYTE PTR [eax+2], "e"
    jne lbl16
    cmp BYTE PTR [eax+3], 0
    jne notfound
  ; -------------------
    .data
      ptr_je db "je",0
    .code
    mov eax, OFFSET ptr_je  ; jne
    ret 4
  ; -------------------
  lbl16:
    cmp BYTE PTR [eax+2], "g"
    jne lbl17
    cmp BYTE PTR [eax+3], 0
    jne lbl18
  ; -------------------
    .data
      ptr_jg db "jg",0
    .code
    mov eax, OFFSET ptr_jg  ; jng
    ret 4
  ; -------------------
  lbl18:
    cmp BYTE PTR [eax+3], "e"
    jne notfound
    cmp BYTE PTR [eax+4], 0
    jne notfound
  ; -------------------
    .data
      ptr_jge db "jge",0
    .code
    mov eax, OFFSET ptr_jge  ; jnge
    ret 4
  ; -------------------
  lbl17:
    cmp BYTE PTR [eax+2], "l"
    jne lbl19
    cmp BYTE PTR [eax+3], 0
    jne lbl20
  ; -------------------
    .data
      ptr_jl db "jl",0
    .code
    mov eax, OFFSET ptr_jl  ; jnl
    ret 4
  ; -------------------
  lbl20:
    cmp BYTE PTR [eax+3], "e"
    jne notfound
    cmp BYTE PTR [eax+4], 0
    jne notfound
  ; -------------------
    .data
      ptr_jle db "jle",0
    .code
    mov eax, OFFSET ptr_jle  ; jnle
    ret 4
  ; -------------------
  lbl19:
    cmp BYTE PTR [eax+2], "o"
    jne lbl21
    cmp BYTE PTR [eax+3], 0
    jne notfound
  ; -------------------
    .data
      ptr_jo db "jo",0
    .code
    mov eax, OFFSET ptr_jo  ; jno
    ret 4
  ; -------------------
  lbl21:
    cmp BYTE PTR [eax+2], "p"
    jne lbl22
    cmp BYTE PTR [eax+3], 0
    jne notfound
  ; -------------------
    .data
      ptr_jp db "jp",0
    .code
    mov eax, OFFSET ptr_jp  ; jnp
    ret 4
  ; -------------------
  lbl22:
    cmp BYTE PTR [eax+2], "s"
    jne lbl23
    cmp BYTE PTR [eax+3], 0
    jne notfound
  ; -------------------
    .data
      ptr_js db "js",0
    .code
    mov eax, OFFSET ptr_js  ; jns
    ret 4
  ; -------------------
  lbl23:
    cmp BYTE PTR [eax+2], "z"
    jne notfound
    cmp BYTE PTR [eax+3], 0
    jne notfound
  ; -------------------
    .data
      ptr_jz db "jz",0
    .code
    mov eax, OFFSET ptr_jz  ; jnz
    ret 4
  ; -------------------
  lbl10:
    cmp BYTE PTR [eax+1], "o"
    jne lbl24
    cmp BYTE PTR [eax+2], 0
    jne notfound
  ; -------------------
    .data
      ptr_jno db "jno",0
    .code
    mov eax, OFFSET ptr_jno  ; jo
    ret 4
  ; -------------------
  lbl24:
    cmp BYTE PTR [eax+1], "p"
    jne lbl25
    cmp BYTE PTR [eax+2], 0
    jne lbl26
  ; -------------------
    .data
      ptr_jnp db "jnp",0
    .code
    mov eax, OFFSET ptr_jnp  ; jp
    ret 4
  ; -------------------
  lbl26:
    cmp BYTE PTR [eax+2], "e"
    jne lbl27
    cmp BYTE PTR [eax+3], 0
    jne notfound
  ; -------------------
    .data
      ptr_jpo db "jpo",0
    .code
    mov eax, OFFSET ptr_jpo  ; jpe
    ret 4
  ; -------------------
  lbl27:
    cmp BYTE PTR [eax+2], "o"
    jne notfound
    cmp BYTE PTR [eax+3], 0
    jne notfound
  ; -------------------
    .data
      ptr_jpe db "jpe",0
    .code
    mov eax, OFFSET ptr_jpe  ; jpo
    ret 4
  ; -------------------
  lbl25:
    cmp BYTE PTR [eax+1], "s"
    jne lbl28
    cmp BYTE PTR [eax+2], 0
    jne notfound
  ; -------------------
    .data
      ptr_jns db "jns",0
    .code
    mov eax, OFFSET ptr_jns  ; js
    ret 4
  ; -------------------
  lbl28:
    cmp BYTE PTR [eax+1], "z"
    jne notfound
    cmp BYTE PTR [eax+2], 0
    jne notfound
  ; -------------------
    .data
      ptr_jnz db "jnz",0
    .code
    mov eax, OFFSET ptr_jnz  ; jz
    ret 4
  ; -------------------
   
  notfound:
    xor eax, eax
   
    ret 4

jump_inversion endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
Title: Re: Jump inversion algorithm.
Post by: dedndave on April 03, 2014, 02:24:43 AM
ok - i'm curious   :redface:
where might you use something like that ?
if i ran it on a source file, the program would no longer work   :P
Title: Re: Jump inversion algorithm.
Post by: KeepingRealBusy on April 03, 2014, 03:31:15 AM
Steve,

Dis you post this on the wrong day?

Dave.
Title: Re: Jump inversion algorithm.
Post by: hutch-- on April 03, 2014, 10:09:56 AM
 :biggrin:

Dave_1,

Yo use it in some forms of code optimisers among other things.

Dave_2,

No.  :P
Title: Re: Jump inversion algorithm.
Post by: Grincheux on December 05, 2015, 06:30:16 AM
Which kind of code optimizers do you use? Give the names.
Title: Re: Jump inversion algorithm.
Post by: hutch-- on December 05, 2015, 10:08:04 AM
Ones you write yourself.
Title: Re: Jump inversion algorithm.
Post by: jj2007 on December 05, 2015, 03:40:51 PM
Quote from: Grincheux on December 05, 2015, 06:30:16 AM
Which kind of code optimizers do you use? Give the names.

Hutch, MichaelW, DednDave, qWord, rrr314159, Antariy, Nidud, Siekmanski, KeepingRealBusy, ... the list is long ;)
Title: Re: Jump inversion algorithm.
Post by: guga on December 06, 2015, 08:54:10 AM
Thanks a lot Steve. It is handy to mplement in RosAsm. I was planning to create a correspondency of the JCC during disassembler, but, this reverse operation will be handy for development purposes.

Many thanks
Title: Re: Jump inversion algorithm.
Post by: TouEnMasm on December 06, 2015, 08:45:03 PM

Perhaps a sample of use ?
Title: Re: Jump inversion algorithm.
Post by: hutch-- on December 06, 2015, 10:16:25 PM
Yves,

Its not easy to explain it simply, often when you are working on a long algorithm you get jumps to more jumps in sequence and while you may get it to work, it is very inefficient. The capacity to short circuit a chain of jumps often means inverting a particular conditional jump directly to the end label. This algo does the inversions for you, point a particular jump to it and it will return the inverse as a string.
Title: Re: Jump inversion algorithm.
Post by: dedndave on December 07, 2015, 06:46:12 AM
original code might be clumsy...
        js      label0

        <some code A>
        jmp     label1

label0: <some code B>
        jmp     label2

label1: <some code C>

label2: <some code D>


it can be uncluttered by reversing the sense of the branch...
        jns     label0

        <some code B>
        jmp     label1

label0: <some code A>

        <some code C>

label1: <some code D>


however, it seems like a translation table would be much simpler   :biggrin:
Title: Re: Jump inversion algorithm.
Post by: nidud on December 07, 2015, 07:37:07 AM
deleted