QAH
QAH

Reputation: 4216

Assembly language je jump function

I am trying to find online the usage of the assembly language function "je". I read that je means jump if equal and that is exactly what I want. What is the actual usage of this function, or in other words, how to I type this function to check a value and jump if it is equal to something?

Please let me know.

BTW, I am using NASM if that makes a difference.

Upvotes: 28

Views: 135795

Answers (5)

wljackhero
wljackhero

Reputation: 171

I have to say je func is to test if zero flag is set and then jump to somewhere else or continue to the next instruction that follows.

test cx, cx
je   some_label

The test instruction just does a bitwise AND of the two operands, and set the FLAG according to the AND result. The je instruction then uses the ZERO flag to decide to jump or continue.

The code above is used to check if cx is zero or not.

  • If cx is zero, test will set zero flag, then je will cause to jump to some place;
  • If cx is not zero, test will not set zero flag, je will continue to execute the instruction that follows.

NOTE: je is not to test equal, but to test the ZERO flag which is set by some instruction before this.

Upvotes: 17

mrduclaw
mrduclaw

Reputation: 4035

You'll precede the je with a cmp (or test or equivalent) usually, which sets a flag in the EFLAGS register. Here's a link to a simple echo server in NASM that might help in general. Ignore the annoying Google ads.

An example usage for je might be:

    cmp eax, ebx
    je  RET_FAIL
    jmp RET_SUCCESS

RET_FAIL:
    push 1
    pop  eax
    ret

RET_SUCCESS:
    push 0
    pop eax
    ret

Upvotes: 2

bcat
bcat

Reputation: 8941

Let's say you want to check if EAX is equal to 5, and perform different actions based on the result of that comparison. An if-statement, in other words.

  ; ... some code ...

  cmp eax, 5
  je .if_true
  ; Code to run if comparison is false goes here.
  jmp short .end_if
.if_true:
  ; Code to run if comparison is true goes here.
.end_if:

  ; ... some code ...

Upvotes: 25

asveikau
asveikau

Reputation: 40284

This will jump if the "equal flag" (also known as the "zero flag") in the FLAGS register is set. This gets set as a result of arithmetic operations, or instructions like TEST and CMP.

For example: (if memory serves me right this is correct :-)

cmp eax, ebx    ; Subtract EBX from EAX -- the result is discarded
                ; but the FLAGS register is set according to the result.
je .SomeLabel   ; Jump to some label if the result is zero (ie. they are equal).
                ; This is also the same instruction as "jz".

Upvotes: 15

QAH
QAH

Reputation: 4216

Well, I finally found my answer. :P Basically you call je label_to_jump_to after a cmp call.

If cmp shows that the two values are equal, je will jump to the specified label. If not, it will keep execution flowing.

Upvotes: 0

Related Questions