Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly Language loops

Tags:

x86

assembly

I'm bumping into a little bit of troubles trying to get my loop to work properly. Here's what I need to do

Add 3 to i1 and store the result into i1 Multiply i2 by 3 and store the result into i2

Terminate the loop if either i1 becomes greater than 100 OR if both the following are true: more than 15 iterations of the loop have occurred AND i2 has reached a value of at least 999999.

It's not finished yet, but I was wondering is it possible to use 2 CMP in a loop? Here's what I have so far:

        {
unsigned long i1;
unsigned long i2;
unsigned long i3;
unsigned long i4;
_asm
    {
    mov     i1, 1
    mov     i2, 1
    mov     eax, i1
    mov     ebx, i2
    mov     ecx, 3
Start:
    add     eax, ecx
    cmp     eax, 100
    jnz     Start
    jge     Done


Start2:
    imult   ebx, ecx
    cmp     ebx, 999999



Done:
    mov     i1, eax
    }
    cout << "results are "  << (unsigned long) i1 << ", "
                            << (unsigned long) i2 << ", "
                            << (unsigned long) i3 << ", "
                            << (unsigned long) i4 << endl;

}
like image 418
Big Blue Avatar asked Jan 20 '26 21:01

Big Blue


1 Answers

You have a cmp in two different procedures (not loops, as @harold mentions). You'll probably want some kind of jump after the cmp in Start2, otherwise you'll just fall back to Done. But, based on the 999999 immediate, it looks like that is the intent. So, you can perform a jump back to the "loop" if you have not yet reached that value. Otherwise, if you reach that value, proceed to Done.

like image 103
Jamal Avatar answered Jan 23 '26 08:01

Jamal