Author Topic: Jump inversion algorithm.  (Read 5860 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7800
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Jump inversion algorithm.
« 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

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: Jump inversion algorithm.
« Reply #1 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

KeepingRealBusy

  • Member
  • ***
  • Posts: 426
Re: Jump inversion algorithm.
« Reply #2 on: April 03, 2014, 03:31:15 AM »
Steve,

Dis you post this on the wrong day?

Dave.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7800
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Jump inversion algorithm.
« Reply #3 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
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Grincheux

  • Member
  • ***
  • Posts: 330
  • Never be pleased, Always improve
    • Asm for fun
Re: Jump inversion algorithm.
« Reply #4 on: December 05, 2015, 06:30:16 AM »
Which kind of code optimizers do you use? Give the names.
Kenavo (Bye)
----------------------
Asm for Fun
My Links
"La garde meurt mais ne rend pas"
Cambronne à Waterloo

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7800
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Jump inversion algorithm.
« Reply #5 on: December 05, 2015, 10:08:04 AM »
Ones you write yourself.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 10855
  • Assembler is fun ;-)
    • MasmBasic
Re: Jump inversion algorithm.
« Reply #6 on: December 05, 2015, 03:40:51 PM »
Which kind of code optimizers do you use? Give the names.

Hutch, MichaelW, DednDave, qWord, rrr314159, Antariy, Nidud, Siekmanski, KeepingRealBusy, ... the list is long ;)

guga

  • Member
  • *****
  • Posts: 1377
  • Assembly is a state of art.
    • RosAsm
Re: Jump inversion algorithm.
« Reply #7 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
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com

TouEnMasm

  • Member
  • *****
  • Posts: 1506
    • EditMasm
Re: Jump inversion algorithm.
« Reply #8 on: December 06, 2015, 08:45:03 PM »

Perhaps a sample of use ?
Fa is a musical note to play with CL

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7800
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Jump inversion algorithm.
« Reply #9 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: Jump inversion algorithm.
« Reply #10 on: December 07, 2015, 06:46:12 AM »
original code might be clumsy...
Code: [Select]
        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...
Code: [Select]
        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:

nidud

  • Member
  • *****
  • Posts: 2013
    • https://github.com/nidud/asmc
Re: Jump inversion algorithm.
« Reply #11 on: December 07, 2015, 07:37:07 AM »
This is the inverter from hll.c used in JWASM
Code: [Select]
; invert a Jump:
; - Jx -> JNx (x = e|z|c|s|p|o )
; - JNx -> Jx (x = e|z|c|s|p|o )
; - Ja -> Jbe, Jae -> Jb
; - Jb -> Jae, Jbe -> Ja
; - Jg -> Jle, Jge -> Jl
; - Jl -> Jge, Jle -> Jg
; added in v2.11:
; - jmp -> 0
; - 0 -> jmp

InvertJump PROC
mov edx,eax
.if BYTE PTR [eax] == 0 ; v2.11: convert 0 to "jmp"
strcpy( eax, "jmp " )
ret
.endif
inc edx
mov ax,[edx]
.switch al
  .case 'e' ; je --> jne
  .case 'z'
  .case 'c'
  .case 's'
  .case 'p'
  .case 'o'
mov ah,al
mov al,'n'
mov [edx],ax
ret
  .case 'n'
mov al,ah
mov ah,' '
mov [edx],ax
ret
  .case 'a'
mov BYTE PTR [edx],'b'
inc edx
.break
  .case 'b'
mov BYTE PTR [edx],'a'
inc edx
.break
  .case 'g'
mov BYTE PTR [edx],'l'
inc edx
.break
  .case 'l'
mov BYTE PTR [edx],'g'
inc edx
.break
  .default
;
; v2.11: convert "jmp" to 0
;
.if BYTE PTR [edx] == 'm'
mov BYTE PTR [edx-1],0
.endif
ret
.endsw
.if BYTE PTR [edx] == 'e'
mov BYTE PTR [edx],' '
.else
mov BYTE PTR [edx],'e'
.endif
ret
InvertJump ENDP