3 Replies - 237 Views - Last Post: 15 March 2019 - 07:39 AM

#1 mistermunch   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-March 19

Understanding dynamic allocation

Posted 14 March 2019 - 12:12 PM

I'm working through understanding some C++ dynamic allocation through the disassembler, and I had some questions.

00071B67  movsx       eax,word ptr [max]  
00071B6B  xor         ecx,ecx  
00071B6D  mov         edx,2  
00071B72  mul         eax,edx 

I understand whats happening and why its happening up to here
00071B74  seto        cl  ;I know this checks for overflow and sets the least sig bit if true
00071B77  neg         ecx  ;i know this takes the 2's comp of that set bit if overflow
00071B79  or          ecx,eax  ;why are we doign this? 
00071B7B  push        ecx  ;why are we doign this? 
00071B7C  call        operator new[] (07137Fh)  ;this is the actual call to allocate mem but how does it work for error checking?
00071B81  add         esp,4  ;why do you add 4 to the stack pointer? because we just pushed ecx?
00071B84  mov         dword ptr [ebp-104h],eax  ;im essentially lost after this?
00071B8A  mov         eax,dword ptr [ebp-104h]  
00071B90  mov         dword ptr [a],eax  


Thank You for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: Understanding dynamic allocation

#2 mistermunch   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-March 19

Re: Understanding dynamic allocation

Posted 14 March 2019 - 12:26 PM

I realize the variables might be a bit ambiguous so here's the entire disassembly via Visual Studio
int main()
{
00071B40  push        ebp  
00071B41  mov         ebp,esp  
00071B43  sub         esp,130h  
00071B49  push        ebx  
00071B4A  push        esi  
00071B4B  push        edi  
00071B4C  lea         edi,[ebp-130h]  
00071B52  mov         ecx,4Ch  
00071B57  mov         eax,0CCCCCCCCh  
00071B5C  rep stos    dword ptr es:[edi]  
    short max = 99999;
00071B5E  mov         eax,0FFFF869Fh  
00071B63  mov         word ptr [max],ax  
    short* a = new short[max];
00071B67  movsx       eax,word ptr [max]  
00071B6B  xor         ecx,ecx  
00071B6D  mov         edx,2  
00071B72  mul         eax,edx  
00071B74  seto        cl  
00071B77  neg         ecx  
00071B79  or          ecx,eax  
00071B7B  push        ecx  
00071B7C  call        operator new[] (07137Fh)  
00071B81  add         esp,4  
00071B84  mov         dword ptr [ebp-104h],eax  
00071B8A  mov         eax,dword ptr [ebp-104h]  
00071B90  mov         dword ptr [a],eax  

    int n = 0;
00071B93  mov         dword ptr [n],0  
    while (cin >> a[n])
00071B9A  mov         eax,dword ptr [n]  
00071B9D  mov         ecx,dword ptr [a]  
00071BA0  lea         edx,[ecx+eax*2]  
00071BA3  mov         esi,esp  
00071BA5  push        edx  
00071BA6  mov         ecx,dword ptr [[email protected]@@3V?$[email protected]?$[email protected]@[email protected]@@[email protected] (07C0A8h)]  
00071BAC  call        dword ptr [__imp_std::basic_istream<char,std::char_traits<char> >::operator>> (07C0B8h)]  
00071BB2  cmp         esi,esp  
00071BB4  call        __RTC_CheckEsp (07119Fh)  
00071BB9  mov         dword ptr [ebp-110h],eax  
00071BBF  mov         eax,dword ptr [ebp-110h]  
00071BC5  mov         ecx,dword ptr [eax]  
00071BC7  mov         edx,dword ptr [ebp-110h]  
00071BCD  add         edx,dword ptr [ecx+4]  
00071BD0  mov         esi,esp  
00071BD2  mov         ecx,edx  
00071BD4  call        dword ptr [__imp_std::ios_base::operator bool (07C0ACh)]  
00071BDA  cmp         esi,esp  
00071BDC  call        __RTC_CheckEsp (07119Fh)  
00071BE1  movzx       eax,al  
00071BE4  test        eax,eax  
00071BE6  je          main+170h (071CB0h)  
    {
        n++;
00071BEC  mov         eax,dword ptr [n]  
00071BEF  add         eax,1  
00071BF2  mov         dword ptr [n],eax  
        if (n >= max)
00071BF5  movsx       eax,word ptr [max]  
00071BF9  cmp         dword ptr [n],eax  
00071BFC  jl          main+16Bh (071CABh)  
        {
            max *= 2;
00071C02  movsx       eax,word ptr [max]  
00071C06  shl         eax,1  
00071C08  mov         word ptr [max],ax  
            short* temp = new short[max];
00071C0C  movsx       eax,word ptr [max]  
00071C10  xor         ecx,ecx  
00071C12  mov         edx,2  
00071C17  mul         eax,edx  
00071C19  seto        cl  
00071C1C  neg         ecx  
00071C1E  or          ecx,eax  
00071C20  push        ecx  
            short* temp = new short[max];
00071C21  call        operator new[] (07137Fh)  
00071C26  add         esp,4  
00071C29  mov         dword ptr [ebp-11Ch],eax  
00071C2F  mov         eax,dword ptr [ebp-11Ch]  
00071C35  mov         dword ptr [ebp-2Ch],eax  
            for (int i = 0; i<n; i++)
00071C38  mov         dword ptr [ebp-38h],0  
00071C3F  jmp         main+10Ah (071C4Ah)  
00071C41  mov         eax,dword ptr [ebp-38h]  
00071C44  add         eax,1  
00071C47  mov         dword ptr [ebp-38h],eax  
00071C4A  mov         eax,dword ptr [ebp-38h]  
00071C4D  cmp         eax,dword ptr [n]  
00071C50  jge         main+128h (071C68h)  
            {
                temp[i] = a[i];
00071C52  mov         eax,dword ptr [ebp-38h]  
00071C55  mov         ecx,dword ptr [ebp-2Ch]  
00071C58  mov         edx,dword ptr [ebp-38h]  
00071C5B  mov         esi,dword ptr [a]  
00071C5E  mov         dx,word ptr [esi+edx*2]  
00071C62  mov         word ptr [ecx+eax*2],dx  
            }
00071C66  jmp         main+101h (071C41h)  
            delete[] a;
00071C68  mov         eax,dword ptr [a]  
00071C6B  mov         dword ptr [ebp-128h],eax  
00071C71  mov         ecx,dword ptr [ebp-128h]  
00071C77  push        ecx  
00071C78  call        operator delete[] (071410h)  
00071C7D  add         esp,4  
00071C80  cmp         dword ptr [ebp-128h],0  
00071C87  jne         main+155h (071C95h)  
00071C89  mov         dword ptr [ebp-130h],0  
00071C93  jmp         main+165h (071CA5h)  
00071C95  mov         dword ptr [a],8123h  
00071C9C  mov         edx,dword ptr [a]  
00071C9F  mov         dword ptr [ebp-130h],edx  
            a = temp;
00071CA5  mov         eax,dword ptr [ebp-2Ch]  
00071CA8  mov         dword ptr [a],eax  
        }
    }
00071CAB  jmp         main+5Ah (071B9Ah)  
    return 0;
00071CB0  xor         eax,eax  
}
00071CB2  pop         edi  
00071CB3  pop         esi  
00071CB4  pop         ebx  
00071CB5  add         esp,130h  
00071CBB  cmp         ebp,esp  
00071CBD  call        __RTC_CheckEsp (07119Fh)  
00071CC2  mov         esp,ebp  
00071CC4  pop         ebp  
00071CC5  ret   

Was This Post Helpful? 0
  • +
  • -

#3 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2312
  • View blog
  • Posts: 4,419
  • Joined: 30-May 10

Re: Understanding dynamic allocation

Posted 15 March 2019 - 02:01 AM

And what is it that you hope to learn?

Given that different compilers generate quite different code (even for the same architecture), any fine detail you learn isn't going to transport very well to other environments. Just changing your compiler flags can upset things. Not to mention, compiler upgrades and patches.

   3:bar.cpp       **** int main()
   4:bar.cpp       **** {
  11              		.loc 1 4 0
  12              		.cfi_startproc
  13 0000 55       		pushq	%rbp
  14              		.cfi_def_cfa_offset 16
  15              		.cfi_offset 6, -16
  16 0001 4889E5   		movq	%rsp, %rbp
  17              		.cfi_def_cfa_register 6
  18 0004 4883EC20 		subq	$32, %rsp
   5:bar.cpp       ****     short max = 99999;
  19              		.loc 1 5 0
  20 0008 66C745E6 		movw	$-31073, -26(%rbp)
  20      9F86
   6:bar.cpp       ****     short* a = new short[max];
  21              		.loc 1 6 0
  22 000e 480FBF45 		movswq	-26(%rbp), %rax
  22      E6
  23 0013 48BA0000 		movabsq	$4575657221408423936, %rdx
  23      00000000 
  23      803F
  24 001d 4839D0   		cmpq	%rdx, %rax
  25 0020 7705     		ja	.L2
  26              		.loc 1 6 0 is_stmt 0 discriminator 1
  27 0022 4801C0   		addq	%rax, %rax
  28 0025 EB07     		jmp	.L3
  29              	.L2:
  30              		.loc 1 6 0 discriminator 2
  31 0027 48C7C0FF 		movq	$-1, %rax
  31      FFFFFF
  32              	.L3:
  33              		.loc 1 6 0 discriminator 4
  34 002e 4889C7   		movq	%rax, %rdi
  35 0031 E8000000 		call	_Znam
  35      00
  36 0036 488945F0 		movq	%rax, -16(%rbp)
   7:bar.cpp       **** 
   8:bar.cpp       ****     int n = 0;
  37              		.loc 1 8 0 is_stmt 1 discriminator 4
  38 003a C745E800 		movl	$0, -24(%rbp)
  38      000000
  39              	.L11:
   9:bar.cpp       ****     while (cin >> a[n])



Aside from the general skill of being able to roughly follow what the compiler produced for the purposes of debugging, the fine detail of say register use is not going to give you profound insight into how high level languages work.

It might be a good idea to start with a valid program.
bar.cpp: In function ‘int main()’:
bar.cpp:5:17: warning: overflow in implicit constant conversion [-Woverflow]
     short max = 99999;


Congrats, you just made a negative number to pass to new[].
Was This Post Helpful? 0
  • +
  • -

#4 mistermunch   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-March 19

Re: Understanding dynamic allocation

Posted 15 March 2019 - 07:39 AM

View PostSalem_c, on 15 March 2019 - 02:01 AM, said:

And what is it that you hope to learn?

Given that different compilers generate quite different code (even for the same architecture), any fine detail you learn isn't going to transport very well to other environments. Just changing your compiler flags can upset things. Not to mention, compiler upgrades and patches.

   3:bar.cpp       **** int main()
   4:bar.cpp       **** {
  11              		.loc 1 4 0
  12              		.cfi_startproc
  13 0000 55       		pushq	%rbp
  14              		.cfi_def_cfa_offset 16
  15              		.cfi_offset 6, -16
  16 0001 4889E5   		movq	%rsp, %rbp
  17              		.cfi_def_cfa_register 6
  18 0004 4883EC20 		subq	$32, %rsp
   5:bar.cpp       ****     short max = 99999;
  19              		.loc 1 5 0
  20 0008 66C745E6 		movw	$-31073, -26(%rbp)
  20      9F86
   6:bar.cpp       ****     short* a = new short[max];
  21              		.loc 1 6 0
  22 000e 480FBF45 		movswq	-26(%rbp), %rax
  22      E6
  23 0013 48BA0000 		movabsq	$4575657221408423936, %rdx
  23      00000000 
  23      803F
  24 001d 4839D0   		cmpq	%rdx, %rax
  25 0020 7705     		ja	.L2
  26              		.loc 1 6 0 is_stmt 0 discriminator 1
  27 0022 4801C0   		addq	%rax, %rax
  28 0025 EB07     		jmp	.L3
  29              	.L2:
  30              		.loc 1 6 0 discriminator 2
  31 0027 48C7C0FF 		movq	$-1, %rax
  31      FFFFFF
  32              	.L3:
  33              		.loc 1 6 0 discriminator 4
  34 002e 4889C7   		movq	%rax, %rdi
  35 0031 E8000000 		call	_Znam
  35      00
  36 0036 488945F0 		movq	%rax, -16(%rbp)
   7:bar.cpp       **** 
   8:bar.cpp       ****     int n = 0;
  37              		.loc 1 8 0 is_stmt 1 discriminator 4
  38 003a C745E800 		movl	$0, -24(%rbp)
  38      000000
  39              	.L11:
   9:bar.cpp       ****     while (cin >> a[n])



Aside from the general skill of being able to roughly follow what the compiler produced for the purposes of debugging, the fine detail of say register use is not going to give you profound insight into how high level languages work.

It might be a good idea to start with a valid program.
bar.cpp: In function ‘int main()’:
bar.cpp:5:17: warning: overflow in implicit constant conversion [-Woverflow]
     short max = 99999;


Congrats, you just made a negative number to pass to new[].


I apologize I think I may have confused you. I’m trying to better understand assembly not some higher level prgrogrammijg language. I used 99999 knowing full well that it wouldnt work to see how the code snippet checked for errors. I didn’t realize that the error schecking would really occur in the call to new[] (I assume that’s what it’s doing). I know different compilers would generate different assembly code but I’m super new so assembly and this seemed like a perfectly reasonable way to see how the assembly instructions are used in higher level programming.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1