Graphics Programs Reference
In-Depth Information
This loop iterates ECX from 0 to 2 , making a call to dup2 each time. With
a more complete understanding of the flags used by the cmp instruction, this
loop can be shrunk even further. The status flags set by the cmp instruction are
also set by most other instructions, describing the attributes of the instruction's
result. These flags are carry flag (CF), parity flag (PF), adjust flag (AF), over-
flow flag (OF), zero flag (ZF), and sign flag (SF). The last two flags are the
most useful and the easiest to understand. The zero flag is set to true if the
result is zero, otherwise it is false. The sign flag is simply the most significant
bit of the result, which is true if the result is negative and false otherwise.
This means that, after any instruction with a negative result, the sign flag
becomes true and the zero flag becomes false.
Abbreviation
Name
Description
ZF
zero flag
True if the result is zero.
SF
sign flag
True if the result is negative (equal to the most significant bit of result).
The cmp (compare) instruction is actually just a sub (subtract) instruction
that throws away the results, only affecting the status flags. The jle (jump if
less than or equal to) instruction is actually checking the zero and sign flags.
If either of these flags is true, then the destination (first) operand is less than
or equal to the source (second) operand. The other conditional jump instruc-
tions work in a similar way, and there are still more conditional jump
instructions that directly check individual status flags:
Instruction Description
jz <target> Jump to target if the zero flag is set.
jnz <target> Jump if the zero flag is not set.
js <target> Jump if the sign flag is set.
jns <target> Jump is the sign flag is not set.
With this knowledge, the cmp (compare) instruction can be removed
entirely if the loop's order is reversed. Starting from 2 and counting down,
the sign flag can be checked to loop until 0 . The shortened loop is shown
below, with the changes shown in bold.
; dup2(connected socket, {all three standard I/O file descriptors})
mov ebx, eax ; Move socket FD in ebx.
xor eax, eax ; Zero eax.
push BYTE 0x2 ; ecx starts at 2.
pop ecx
dup_loop:
mov BYTE al, 0x3F ; dup2 syscall #63
int 0x80 ; dup2(c, 0)
dec ecx ; Count down to 0.
jns dup_loop ; If the sign flag is not set, ecx is not negative.
 
Search WWH ::




Custom Search