Disassembly debugging dead loop and disassembly debugging cycle

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.