Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a conditional jump in assembly without a `cmp` first?

Tags:

x86

assembly

So I'm reading through some assembly source code for learning purposes and came across something very strange (or I may just be a newb):

.ver:
    mov al, [redoxfs.header + Header.version +  bx]
    mov ah, [.version + bx]
    cmp al, ah
    jne .ver_err
    inc bx
    jl .ver

So in this sub-label we have two jump instructions.

However, about the last jump instruction jl. Correct me if I'm wrong, but shouldn't there be a cmp before the jump since it's conditional?

I initially thought it was based on cmp al, ah, but jne jumps if not equal anyway.

Am I missing something?

like image 527
UndercoverCoder Avatar asked Jan 07 '18 16:01

UndercoverCoder


People also ask

What is the difference between the conditional jump and conditional set instructions?

Conditional jump This is performed by a set of jump instructions j<condition> depending upon the condition. The conditional instructions transfer the control by breaking the sequential flow and they do it by changing the offset value in IP.

Which instruction is used to create a unconditional jump?

1.1 Jump Instruction. JMP (Jump) unconditionally transfers control to the target location. JMP is a one-way transfer of execution; it does not save a return address on the stack. The JMP instruction always performs the same basic function of transferring control from the current location to a new location.


1 Answers

Consider these 3 kinds of instructions:

  • All of the conditional jumps (like jne, jl, and many more) will jump based on the current setting of one or more of the bits in the FLAGS register.
  • Besides the cmp instruction, there are many more instructions that will modify some of these bits in the FLAGS register (like test, add, and many more).
  • And then there are lots of instructions that don't modify any of the flags (like mov, push, and many more).

Examples

cmp al, ah
jne .ver_err

The jne .ver_err jumps based on the flagbits set by the most recent flags modifying instruction which is cmp al, ah in this case.

inc bx
jl .ver

The jl .ver jumps based on the flagbits set by the most recent flags modifying instruction which is inc bx in this case.

inc bx
lea si, [si+1]
jl .ver

Since this interjected lea instruction does not modify any flags, the jl .ver instruction still jumps based on the flagbits set by the inc bx instruction because that's still the most recent flags modifying instruction.

like image 168
Sep Roland Avatar answered Sep 28 '22 16:09

Sep Roland