-1

I'm new to Assembly language, I'm reading Assembly Language Programming By Ytha Yu, Charles Marut

Anyway, I'm in Chapter 6 and I can't figure out the case of the flags which the processor uses to do conditional jumps. I know what the flags basically do, for example, I know that Overflow flag sets or resets if any overflow occurs or doesn't in the last instruction. But I can't figure out how they play into the conditions of the jumps.

Signed Conditional Jumps Unsigned Conditional Jumps

It would be really helpful if anyone helped me understand the uses of flag registers to implement conditional jumps.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
3N4N
  • 570
  • 5
  • 21
  • 1
    the jump is taken if the corresponding flag is set/not set. Otherwise you move on. – qwr Jun 12 '19 at 17:28
  • @qwr I want to know why those flags are working, e.g. I know `JNE` requires `ZF<>0`, cause `CMP AX,BX` would be zero if `AX` and `BX` are equal. What about the others? – 3N4N Jun 12 '19 at 17:31
  • 2
    @klaus For each flag, the conditions are listed in the right columns. If you want to understand why the flags are set the way they are in the give situations, I recommend you to try some examples on a sheet of paper. – fuz Jun 12 '19 at 17:49
  • 1
    Possible duplicate of [Assembly JLE jmp instruction example](//stackoverflow.com/q/4557279) also [Assembly je, jns, jle which register does it use?](//stackoverflow.com/q/38003988) has a useful answer, but the question is a confusing misunderstanding. – Peter Cordes Jun 12 '19 at 20:18

1 Answers1

4

The general idea is: there are some operations that set or clear individual flags - arithmetic, bitwise, comparisons. You are then free to perform conditional jumps based on the result of those operations.

Let's consider the most basic case - jump if one variable is greater than another. Assuming the variables are in the general purpose registers, and the code is 32-bit, this would go:

cmp eax, ebx
ja AOverB

Now, what does that do? The CMP line calculates the value EAX-EBX, doesn't store it anywhere, but sets the flags. Specifically, the Zero flag is set if the result of the subtraction is zero (i. e. EAX-EBX=0, i. e. EAX=EBX), and cleared if it's not. The Carry flag is set if EAX<EBX (i. e. subtracting them would require a borrow). So the JA command goes: take the jump if the Zero flag is not set and the Carry flag is not set either (that is, neither EAX=EBX nor EAX<EBX). By obvious math, if neither of those flags is set, that means EAX>EBX.

Did that help? Do you get the concepts of carry and borrow, and their close relative overflow?

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281