Search code examples
cvisual-c++assemblyx86ida

Interpretation of C Code in IDA Pro Needed


I'm using IDA Pro to disassemble the following C code: However looking at the disassembly below it seems to me incomplete. The data is never initialized (as per C code) even though it does appear to be loaded into the stack however the procedure (nullsub_1) that is located at 00401040 makes no use of the data ? Am I making a correct assessment or am I missing something ??? I have used Visual C++ 6/2005 to compile the C code.

#include <stdio.h>
#include <windows.h>

struct a
{
    char s[10];
    BYTE b;
    int i;
};

a al;
void init(a);
void main()
{
    init(al);
};

void init(a c)
{
    for(int j = 0; j < 10; j++) c.s[j] = 'A';
    c.b = 10;
    c.i = 10000;
};


.text:00401000 ; int __cdecl main(int argc,const char **argv,const char *envp)
.text:00401000 _main           proc near               ; CODE XREF: start+AFp
.text:00401000
.text:00401000     argc            = dword ptr  4
.text:00401000     argv            = dword ptr  8
.text:00401000     envp            = dword ptr  0Ch
.text:00401000
.text:00401000                 mov     ecx, dword_4084C0
.text:00401006                 mov     edx, dword_4084C4
.text:0040100C                 sub     esp, 10h
.text:0040100F                 mov     eax, esp
.text:00401011                 mov     [eax], ecx
.text:00401013                 mov     ecx, dword_4084C8
.text:00401019                 mov     [eax+4], edx
.text:0040101C                 mov     edx, dword_4084CC
.text:00401022                 mov     [eax+8], ecx
.text:00401025                 mov     [eax+0Ch], edx
.text:00401028                 call    nullsub_1
.text:0040102D                 add     esp, 10h
.text:00401030                 retn
.text:00401030 _main           endp
.text:00401030
.text:00401030 ;     
.text:00401031                 align 10h
.text:00401040
.text:00401040
.text:00401040
.text:00401040 nullsub_1       proc near               ; CODE XREF: _main+28p
.text:00401040                 retn
.text:00401040 nullsub_1       endp

Solution

  • Your source code has no side effects other than just writing to memory. The compiler eliminates those writes as useless.

    You may have better luck if you compile it in Debug mode (instead of Release) or turn off some compiler optimizations.

    Alternatively, accesses to variables defined as volatile will be preserved, so you can add volatile in your code.