ShellFl0w – Assembly: Th3_Rubb!t_Jump

ALERT: If there are grammatical errors or logical errors, please let us know, thank you.

Introduction

Well, we are almost at the end of the Assembly section of this chapter: C_Ass3mbly.sh, in this article I will go into some concepts that have been expressed previously and then we will discuss the control structures, how they work and when to insert them.
At the end of this chapter, there will be videos that retract these notions and there will probably also be exclusive ones.

Control Structures

Thanks to high-level languages such as C/C+++, Java and Python we know what are the control structures or conditional, in assembly we will not meet with them, but with its ancestors (the goto for example), the logic that defines the use of these is given by the programmer and depends on it its proper functioning, a bit like all the language Assembly.

Comparisons

The control structures reason on the basis of the comparison of data, So the subtraction between two operands of which the result of this operation determines a result extrapolated from the FLAGS register (Remember, the result is not memorized anywhere), this contains in it many variations that can be taken into account according to the received result.

For integers without sign:

There are 2 flags (bits in the FLAGS register) which are important: the Zero Flag (ZF) and Carry Flag (CF). The Zero Flag is set if the resulting difference is 0. The Carry Flag is used as subtraction loan.
cmp left, right: thanks to the result we can see that flag returns.

if left = right  (Quindi non c'è differenza) allora ZF è impostato.
if left < right allora CF è impostato.
if left > right allora niente è impostato perchè non c'è nessun resto.

Now, we can see some pratics examples:

cmp 10, 20    ; Imposta un Carry Flag perchè vright è maggiore di vleft
cmp 10, 10    ; Imposta un Zero Flag perchè la sottrazione tra questi valori è 0, quindi sono uguali

For integers with sign:
There are three important flags: The Zero Flag (ZF), OverFlow Flag (OF) and Sign Flag (SF):

  • The Zero Flag remains the same as in the previous example.
  • The Overflow Flag is set when there is an OverFlow or UnderFlow case.
  • The Sign Flag instead if the result of the operation is negative.

Let’s see several practical examples here too:

cmp 10, 10   ; Sets a Zero Flag because the subtraction between these values is 0, so they are equal.
cmp 10, 5    ; Sign Flag
cmp 10, 15   ; OverFlow

Why cmp 10, 10 returns 0? Well because clearly 10 – 10 = 0, so if it is 0 then it is a Zero flag. While cmp 10, 5 then 5 – 10 returns -5 then negative number then Sign Flag.

We take cmp as a reference point, but it is not the only one we can use for comparisons.

JZ -> ZF IS SET
JNZ -> ZF NOT SET
JO - OF IS SET
JNO -> OF NOT SET
JS -> SF IS SET
JNS -> SF NOT SET
JC -> CF IS SET
JNC -> CF NOT SET
JP -> PF IS SET
JNP -> PF NOT SET

With jumps we can decide which part of the code to skip to and if we want to create iterations, just to give an example we see code:

mov eax, 0
loop_start:
        add eax, 1
        cmp eax, 10
        jz _ok
        loop loop_start
_ok:
    ...

Now we have seen what are the conditioned jumps, namely those jumps that given a cmp will perform a certain action, here is an example of jump non-conditioned jumps.

jmp whereyouwant

These are the “opcodes” that we can use when we have to specify the jump condition, in the previous code we have set that if in the comparison we had obtained a Zero Flag, then we would have obtained a jump, taking the same code, we can have fun in the:

JE jump if vleft = vright
JNE jump if vleft != vright
JL, JNGE jump if vleft < vright
JB, JNAE jump if vleft < vright
JLE, JNG jump if vleft ≤ vright
JBE, JNA jump if vleft ≤ vright
JG, JNLE jump if vleft > vright
JA, JNBE jump if vleft > vright
JGE, JNL jump if vleft ≥ vright
JAE, JNB jump if vleft ≥ vright

As difficult as it may seem, Assembly is not a difficult language but the crucial part of this is the organization, in fact if you really learn how this language works and in general how a computer works, it is not impossible to devote yourself to this without too many problems, as always I recommend to deepen these topics.



Leave a comment