Tuesday 21 July 2015

Understanding loops in assembly !

The concept of loop is important for both programmer and hackers.
For programmer, you want to execute some code 50 times then instead of writing that code 50 times, you can do it  by writing that code only one time and repeat the execution 50 times with the help of loop.
Simply we can say, A loop allow us to execute a statement multiple times in our program.
For hacker, you want to crack Facebook password ;-) and you have a word-list of 100 passwords, you can do it by attempt manually. In high level language C/C++,the way of cracking password is by writing a loop program which simply attempt password 100 times:
    for(int i=0;i<100, i++);
The most widely use of loops in hackers worlds is to password cracking, network cracking, brute force attack.

                                                  

                   

for loop: for loop is used when you want to loop for a certain number of iterations. for loops start counting at a beginning value, test the value for some condition,execute the statement, and increment the value for the next iteration.
In C, basic format is as follows:
for(int i=0;i<100, i++)
{
    /* Code goes here */
}
rest_of_program( );
In the first part, initialization is done first.The second part is the test or condition that controls the loop. Here our loop will run 100 times. Then next is the increment step. Here i is increment by 1 after execute the statement.

This code can be translated into assembly language as such:
loop_initialize:
    movl $0, %ecx
loop_begin:
    cmp    %ecx, $100
       je    rest_of_program
    #
    #Code goes Here
    #
    inc     %ecx
        jmp   loop_begin
rest_of_program:
    #Continues on to here

The %ecx register can be used as a counter register. First we initialize the %ecx register to zero. After initialization, we check if %ecx register is zero or not. if %ecx is equal to 100, then our program jump to
rest_of_program.As you can see %ecx is not equal to 100 because we have initialized the %ecx register to 0 , execution of code starts. Then we increment the %ecx register, jump to loop 'loop_begin'  untill %ecx becomes equal to 100 .

While Loop:if you use while loop in your program that means you want to execute a set of instructions in a loop while a condition is true. It tests the conditions before executing the loop body. In C,The format is as follows:
    while(<conditional test>){
        <statement>;
    }
The while loop checks whether the  conditional test is true or not. If it is true, statement or code inside the body of while loop is executed,that is, code inside the braces { } are executed. Then again the test expression is checked whether test expression is true or not. This process continues until the test expression becomes false. For Example:
while(a < b)
{
    /*Code goes here */
}
/* Finished looping */

In Assembly, this can be rendered like this:
loop_begin:
    movl a, %eax
    movl b, %ebx
    cmpl %eax, %ebx
    jge loop_end
loop_body:
    #Code goes here
    jmp loop_begin
loop_end:
    #Finished looping

Here we first move the a and b in the %eax and %ebx register respectively. Then we compare the %eax and %ebx i.e. a and b.Actually, cmpl Compares two integers. It does this by subtracting the first operand from the second. It discards the results, but sets the flags accordingly. In the next line, we use jge instruction to check if %eax is equal or grater then the %ebx register.Or in simple words, we are checking if a is equal or grater then b. if it is so, jump to label 'loop_end'. If it is not so, then go to label loop_body.

Do While loop: It is almost similar to while and for loop. But the difference is in do...while loop, code is executed at first then the condition is checked whereas in for and while loop, loop test expression is checked at first the beginning of code.
In C,The general syntax of do..while is:
do{
    statement
}
while(expression)
{

}
Let us take a look at the following C code:
do
 {
   x++;
 } while(x != 10);

This code produces this assembler code:
 mov eax, $x
 beginning:
     inc eax
     cmp eax, 0x0A ;0x0A = 10
     jne beginning
     mov $x, eax

Well that's all for assembly loops. 

if you like this post or have any question, please feel free to comment !

No comments:

Post a Comment

Blogger Widget