Disassembly Debug Dead Loop

Source: Internet
Author: User

In "C Traps and defects", the following code appears:

#include <stdio.h>int main (int argc, char *argv[]) {int i = 0;int a[10];for (i=0; i<=10; ++i) {a[i] = 0;//Dead Loop}retu RN 0;}

The author's explanation is that if the compiler used to compile the program allocates memory to the variable in a way that decrements the memory address, the final assignment is to the variable I and into the dead loop.


Run the program in VC6.0 to see the disassembly code:



From the results, it is known that the allocation of memory results in decreasing the address of the variable to allocate memory:



Results, A[i] = 0 Disassembly code is mov dword ptr [EBP+ECX*4-2CH], 0

where ECX is the value of I, ebp-2ch = ebp-44, is actually a[0], I is 4 bytes, a[10] is 40 bytes, a total of 44 bytes.

mov dword ptr [EBP+ECX*4-2CH], 0 is also mov dword ptr [ebp-2ch+ecx*4], 0

Is the address of the a[0], plus the integer address of I units (4 bytes) each time

When the last value i = 10, it becomes the Mov dword ptr [ebp-4], 0, is actually to assign to I, this statement is the first breakpoint position disassembly code, natural, I was re-assigned to 0, into the dead loop.


This is the first time you have used disassembly to debug a program, simply record it.


Disassembly Debug Dead Loop

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.