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.

5 Answers

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

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

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.

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 
2

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.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like