Hi, can someone give me some example code of how to do condition branching stuff using the cmp and jz instructions etc , a really simple one, i'm new to this.

Pseudo code:

If (VARIABLE == 0)
Do something ...
Variable = 1
Else
Do something else
Variable = 0


That kind of thing.

Thanks,

Simon.
Posted on 2002-11-01 10:18:55 by SNDHearn


mov eax, variable
cmp eax, 0 ; If
jz A0
cmp eax,1 ;ElseIf
jz A1
;Else
dec eax
jmp A3 ;Jump to Endif

A0:
inc eax
jmp A3

A1:
sub eax ,14

A3:
Posted on 2002-11-01 10:29:38 by JimmyClif
HI SNDHeran,

you probably mean the bcn (branch on condition) of some processors.



.data
variable DD 0 ;First initialize the variable uin the data section

.code
xor eax, eax ;Going to compare against a register
cmp [variable],eax
jz DoSomething
DoSomethingElse:
nop ;Do here whatever
xor eax,eax
mov [variable],eax ;Of course, you coul write it directly...
DoSomething:
nop ;Do something
inc [variable] ;As it was 0, now is 1


Hope it helps.
slop
Posted on 2002-11-01 10:32:45 by slop
There are two mnemonics which are identical, jz & je (also jnz, jne). They represent "jump if zero", and "jump if equal". As I said, they are the same instruction, but using one or the other can give readability....
; compare eax to some number

cmp eax, some_number
; jump if they are equal, to some location
je some_location

This is eminantly more sensible, as you are comparing eax to a number.

The thing to remember is that cmp is a subtraction, where the result is not stored, only modifying the flags. So (eax - some_number) == 0 is what is really being checked.

Any instruction which sets the flags in any way can be used to determine whether or not to take a branch.
; example #1

shl eax, 1
jc some_location

; example #2
test eax, 1
jnz some_location

These two examples will jump if the first bit is set,
#1 will shift eax left by 1, putting the removed bit in the carry flag, hence "jump if carry" will be taken if the bottom bit was set

#2 will and eax with 1 (not storing the result), and jump if the calculation would have been a non-zero value.

For more details, have a look through the opcodes help file, looking at the flags modified section, and the different jump conditions (there are loads, one for each of the flags pretty much)!

Mirno
Posted on 2002-11-01 10:59:39 by Mirno
Interestingly enough, just about every conditional jump mnemonic has one or more twins for the sake of readability.

JAE = JNB = JNC
JB = JC = JNAE
JBE = JNA
JA = JNBE
JE = JZ
JNE = JNZ
JGE = JNL
JG = JNLE
JL = JNGE
JLE = JNG
JNP = JPO
JP = JPE

TheSvin made a nice reference tool to help people understand conditions.
Posted on 2002-11-01 11:59:55 by iblis
My quick reference on branching (I felt I needed to write it when I met the x86 for the first time):



;Branch:
;
;unsigned <= JBE JNA .. (C|Z)=1
;unsigned < JB JNAE JC .. C=1
;unsigned > JA JNBE .. (C|Z)=0
;unsigned >= JAE JNB JNC .. C=0

;signed <= JLE JNG .. ((S^O)|Z)=1
;signed < JL JNGE .. (S^O)=1
;signed > JG JNLE .. ((S^O)|Z)=0
;signed >= JGE JNL .. (S^O)=0

;equal JE JZ .. Z=1
;not equal JNE JNZ .. Z=0

;zero JZ JE .. Z=1
;not zero JNZ JNE .. Z=0

;sign JS .. S=1
;not sign JNS .. S=0

;carry JC .. C=1
;not carry JNC .. C=0

;overflow JO .. O=1
;not overflow JNO .. O=0

;parity (even) JP JPE .. P=1
;not parity (odd) JNP JPO .. P=0

;ecx=0 JECXZ .. ECX=0
;cx=0 JCXZ .. CX=0


;to invert the condition (i.e. "NOT"), change:
; >= to <
; > to <=
; = to <>
; <> to =
; < to >=
; <= to >

;to swap the Dst<>Src operands, change:
; >= to <=
; > to <
; = to =
; <> to <>
; < to >
; <= to >=
Posted on 2002-11-01 16:13:06 by Maverick
cool response, thanks guys.

Simon.
Posted on 2002-11-01 16:53:05 by SNDHearn