Assembly Language for Beginners

(nextflipdebug2) #1

3.18. C++


call printf_chk
mov eax, [esi+4] ; operator--: get ->prev pointer
mov edx, [eax+0Ch]
mov [esp+0Ch], edx
mov eax, [eax+8]
mov dword ptr [esp+4], offset a1stElementDD ; "1st element: %d %d\n"
mov dword ptr [esp], 1
mov [esp+8], eax
call printf_chk
mov dword ptr [esp], offset aRemovingLastEl ; "removing last element..."
call puts
mov esi, [esp+14h]
mov [esp], esi
call _ZNSt8
detail15_List_node_base9_M_unhookEv ; std::
detail::_List_node_base::⤦
Ç_M_unhook(void)
mov [esp], esi ; void
call _ZdlPv ; operator delete(void
)
mov [esp], ebx
call _Z13dump_List_valPj ; dump_List_val(uint *)
mov [esp], ebx
call _ZNSt10_List_baseI1aSaIS0_EE8_M_clearEv ; std::_List_base<a,std::allocator>::⤦
Ç_M_clear(void)
lea esp, [ebp-8]
xor eax, eax
pop ebx
pop esi
pop ebp
retn
main endp


Listing 3.110: The whole output


  • empty list:
    ptr=0x0028fe90 _Next=0x0028fe90 _Prev=0x0028fe90 x=3 y=0

  • 3-elements list:
    ptr=0x000349a0 _Next=0x00034988 _Prev=0x0028fe90 x=3 y=4
    ptr=0x00034988 _Next=0x00034b40 _Prev=0x000349a0 x=1 y=2
    ptr=0x00034b40 _Next=0x0028fe90 _Prev=0x00034988 x=5 y=6
    ptr=0x0028fe90 _Next=0x000349a0 _Prev=0x00034b40 x=5 y=6
    node at .begin:
    ptr=0x000349a0 _Next=0x00034988 _Prev=0x0028fe90 x=3 y=4
    node at .end:
    ptr=0x0028fe90 _Next=0x000349a0 _Prev=0x00034b40 x=5 y=6

  • let's count from the beginning:
    1st element: 3 4
    2nd element: 1 2
    3rd element: 5 6
    element at .end(): 5 6

  • let's count from the end:
    element at .end(): 5 6
    3rd element: 5 6
    2nd element: 1 2
    1st element: 3 4
    removing last element...
    ptr=0x000349a0 _Next=0x00034988 _Prev=0x0028fe90 x=3 y=4
    ptr=0x00034988 _Next=0x0028fe90 _Prev=0x000349a0 x=1 y=2
    ptr=0x0028fe90 _Next=0x000349a0 _Prev=0x00034988 x=5 y=6


MSVC


MSVC’s implementation (2012) is just the same, but it also stores the current size of the list.


This implies that the .size() method is very fast (O(1)): it just reads one value from memory.


On the other hand, the size variable must be updated at each insertion/deletion.


MSVC’s implementation is also slightly different in the way it arranges the nodes:

← Previous
Free download pdf