Assembly language je jump function
Solution 1
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 ...
Solution 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".
Solution 3
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.
Solution 4
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
QAH
Updated on June 24, 2020Comments
-
QAH almost 4 years
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.
-
Wouter van Nifterick over 14 yearsEdsger Dijkstra must've hated assembler; it's full of GOTO's :-)
-
bcat over 14 yearsIt 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 setsFLAGS
. This includes almost all arithmetic instructions, and some others. -
asveikau over 14 yearsif RET_SUCCESS appears before RET_FAIL you don't need the second jump.
-
mrduclaw over 14 yearsThanks, this was just copy-pasta from an ungraded student's assignment though.
-
Anderson Green over 11 yearsWhich assembler syntax is being used here?
-
Koray Tugay over 9 yearsCan it check any other register than EAX?