18

How do you use the jump family of instructions?

This is what they've got:

JL label
"It" jumps if it is less than or if it is not larger than or equal to.

My question is what is it in this sentence? Say I have a variable in ebx and I want to jump to label there: if ebx is <= 10.

Specifically I'm interested in using the x86 jump family of instructions

bobobobo
  • 64,917
  • 62
  • 258
  • 363
  • Related, possible duplicate: [Assembly - JG/JNLE/JL/JNGE after CMP](https://stackoverflow.com/q/9617877) is a good canonical Q&A for the way branch condition mnemonics make sense following a `cmp`. (Or `test` works as a compare against zero) – Peter Cordes Nov 05 '21 at 06:16

5 Answers5

17

The jump itself checks the flags in the EFL register. These are usually set with TEST or CMP(or as a side effect of many other instructions).

CMP ebx,10
JLE there
  • CMP corresponds to calculating the difference of the operands, updating the flags and discarding the result. Typically used for greater/smaller checks
  • TEST corresponds to calculating the binary AND of the operands, updating the flags and discarding the result. Typically used for equality checks.

See also: The art of assembly language on CMP

As a sidenote: You should get the Intel reference manuals. In particular the two part "Intel® 64 and IA-32 Architectures Software Developer's Manual Volume 2: Instruction Set Reference" which describes all x86 instructions.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • 2
    In this example, does JLE jump when 10 is less than or equal to ebx, or does it jump when ebx is less than or equal to 10? – Anderson Green Feb 20 '13 at 18:04
  • 1
    @AndersonGreen It jumps when the ebx's contents are <= 10. – d0rmLife Mar 18 '13 at 21:06
  • 2
    @AndersonGreen: Intel chose the mnemonics so `cmp x,y` / `jcc` being taken lines up with `x <= y`. https://www.felixcloutier.com/x86/jcc. The easiest one to remind yourself of is that `jb` is the same instruction as `jc`, jump if CF=1. (This is one of the worst parts of AT&T syntax: the reversed operands to `cmp` mean the semantic meanings for jcc mnemonics are backwards from the operand order in the source.) – Peter Cordes Jan 25 '21 at 16:37
  • 1
    I don't think `test` is "Typically used for equality checks." The only typical cases I know are to test specific bits or masks, or to do `test` with the same register twice to check it for zero or check its sign bit. "Equality checks" sounds much more like a case for `cmp`. – ecm May 14 '22 at 19:48
13

JLE instruction conducts two tests:

  • Signed Flag (SF) != Overflow Flag (OF)
  • Zero flag (ZF) == 1

If Zero flags is 1 and Signed Flag and Overflow Flag are not equal, then the short relative jump will be executed.

Maybe just a word how CMP instruction works. CMP instruction is like SUB (subtract), but the destination register will not be updated after exsecution. So the following code will perform the same result like CMP ebx, 10. CMP and SUB instruction affect to flags: Carry, Parity, Auxiliary, Zero, Sign and Overflow flags.

push   ebx            //store ebx value to stack
sub    ebx, 10
pop    ebx            //restore ebx value from stack
Community
  • 1
  • 1
GJ.
  • 10,810
  • 2
  • 45
  • 62
4

The x86 assembly uses a system of bit-flags that represent the result of comparisons. The conditional jump instructions use these flags when deciding whether to perform the jump or not.

In your case you'd use the following two instructions:

cmp ebx, 10     ; compare EBX and 10
jle label       ; jump if EBX is "less than or equal" to 10
…
label:
…
Bruce Dawson
  • 3,284
  • 29
  • 38
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
2

JB - work with unsigned numbers (Jump Below) <

JL - work with signed numbers

mov bx,0     // BX := 0
cmp bx,FF    // 0 < -1 or 0 < 255 (Jump Flag and Sign Flag will change)
jl  butter   // if you use JL jump will not occurs, cus 0 > -1
jb  butter   // if you use JB jump will occurs, cus 0 < 255
0

Here is a simple example of an if/else that stores to one of two different locations, depending on ebx <= 10. The mov store instructions are the if and else blocks.

.model small
.code
start : 
    mov ebx , 20    ( we insert 20 in ebx ) 
    CMP ebx , 10    ( we compare 20 with 10 ) 
    JBE there       ( Jump if below or equal to there ) 
    mov [1020h] , ebx   ( else we move ebx to [1020h] )
    JMP exit     ; don't fall into the else part
there:
    mov [1030h] , ebx ( If ebx <= 10 we move ebx to [1030h] ) 
    ;JMP exit     ; redundant: execution falls through to exit on its own

exit:      ( define a label that other instructions can jump to)
    mov ax, 4c00h
    int 21h             ; 16-bit DOS exit system call
end start 
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Load Jin
  • 15
  • 1
  • Format your code (with triple backticks) if you want anyone to be able to make sense of your answer. Also, this question already has several answers, but I guess you're trying to show a complete `if() else` with the other necessary branching? That would be somewhat useful, I guess I'll format your code for you. In future, format it yourself. – Peter Cordes May 14 '22 at 20:11