I have a short piece of (x86) assembly that I am trying to figure out what it does.
...
6: 81 ec 00 01 00 00 sub $0x100, %esp
c: 31 c9 xor %ecx , %ecx
e: 88 0c 0c mov %cl , (%esp, %ecx, 1)
11: fe c1 inc %cl
13: 75 f9 jne 0xe
....
It looks like its looping though until the "JNE" evaluates to false, i.e. the zero flag = 0. (possibly its putting the numbers 1, 2, 3 ... into the stack??)
From my short investigation into assembly (im new at this) it seam you set the zero flag by doing a compare operation (CMP), but I dont see a compare operation.
So, under what conditions will it break out of this loop?
Zero flag (ZF) - the zero flag is set(1) when the result of an arithmetic operation is zero. Sign flag (SF) - the sign flag is set(1) when the result of an arithmetic operation has a 1 in the most significant bit (msb).
– Zero flag (set when the result of an operation is zero). – Carry flag (set when the result of unsigned arithmetic is too large for the destination operand or when subtraction requires a borrow). – Sign flag (set when the high bit of the destination operand is set indicating a negative result).
Since the result would be 0 , but we don't change the destination operand in a CMP instruction, the zero flag is set to 1 (since it's true).
The MOV instruction never affects the flags. Whenever the destination operand equals Zero, the Zero flag is set. A flag is set when it equals 1. A flag is clear when it equals 0.
inc
sets ZF if the value of cl
after the increment is zero. Your loop is doing this:
sub $0x100, %esp // unsigned char array[256];
xor %ecx , %ecx // unsigned char cl = 0;
mov %cl , (%esp, %ecx, 1) // e: array[cl] = cl;
inc %cl // cl += 1;
jne 0xe // if (cl != 0) goto e;
The loop terminates when cl
is incremented from 255 and wraps around to 0, setting ZF.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With