views:

2027

answers:

4

Hello everyone! 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.

Thanks

+1  A: 

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.

QAH
Edsger Dijkstra must've hated assembler; it's full of GOTO's :-)
Wouter van Nifterick
It doesn't have to follow a `CMP` instruction, although that's the most common case. As others have said, `Jcc` instructions can follow any instruction that sets `FLAGS`. This includes almost all arithmetic instructions, and some others.
bcat
+1  A: 

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
mrduclaw
if RET_SUCCESS appears before RET_FAIL you don't need the second jump.
asveikau
Thanks, this was just copy-pasta from an ungraded student's assignment though.
mrduclaw
+8  A: 

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".
asveikau
+1  A: 

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