Disassembly debugging dead loop and disassembly debugging cycle
The following code appears in "C traps and defects:
# Include <stdio. h> int main (int argc, char * argv []) {int I = 0; int a [10]; for (I = 0; I <= 10; ++ I) {a [I] = 0; // endless loop} return 0 ;}
The author's explanation is: if the compiler used to compile this program allocates memory to the variable by decreasing the memory address, it will assign the value to variable I in the end and fall into an endless loop.
Run the program in VC6.0 and view the disassembly code:
From the results, we can see that the memory allocation result is to allocate memory to the variable in descending order of address:
In the result, the disassembly code of a [I] = 0 is mov dword ptr [ebp + ecx * 4-2Ch], 0
Here, ecx is the value of I, and ebp-2CH = ebp-44 is actually the position of a [0]. I occupies 4 bytes, and a [10] occupies 40 bytes, A total of 44 bytes.
Mov dword ptr [ebp + ecx * 4-2Ch], 0 is mov dword ptr [ebp-2Ch + ecx * 4], 0
That is, on the address of a [0], each time an integer address in unit I is added (4 bytes)
When the last value is I = 10, it becomes mov dword ptr [ebp-4], 0. In fact, it is to assign a value to I. This statement is the disassembly code at the first breakpoint, naturally, I is re-assigned to 0 and falls into an endless loop.
This is the first time I have used disassembly to debug the program. It is a simple record.