5

Here is the code of loop that I'm trying to understand the disassembly of it:

#include<stdio.h>
#include <iostream>

using namespace std;

int main() {
   int i, arr[50], num;

   printf("\nEnter no of elements :");
   cin >> num;

   //Reading values into Array
   printf("\nEnter the values :");
   for (i = 0; i < num; i++)
    cin >> arr[i];

   return 0;
}

And this is the disassembly: enter image description here

Can you explain me the highlighted part? what is Var_D8 is used for? Why compiler shifted left the edx?

  • SHL EDX,2 is basically multiplying EDX's value by 4. – Dominik Antal Jun 5 '15 at 8:15
  • @DominikAntal for what purpose? – Vlad Jun 5 '15 at 8:19
  • I am not sure. However I suspect that this might be some compiler optimization/alignment for the array. Can you tell us the compiler and it's parameters you used? Will the SHL instruction be present if you recompile this code with arr beeing a byte array? You could also attach a dynamic debugger (Ollydbg) and see what is happening exactly by stepping trough your code. – Dominik Antal Jun 5 '15 at 9:07
  • The key to understanding this is the lea opcode - eax gets the address, not the content, of that stack variable. This is the start of your array. As an integer has 4 bytes on your machine, counter gets multiplied by 4, which is the same as left shifting it by 2 - but the left shift is faster on many processors, which is why it's chosen over the multiply. – Guntram Blohm supports Monica Jun 5 '15 at 9:21
  • @DominikAntal dev c++ – Vlad Jun 5 '15 at 9:26
8

var_D8 is your int arr[50].

You can recognize it quickly solely by its name : 50 * sizeof(int) = 200 = 0xC8. The next variable on the stack is numb_of_elements which is positionned on -0x10 on the stack, thus we have some memory between -0xD8 and -0x10 that corresponds to the int array.

Here are some explanations about the following instructions :

lea eax, [ebp+var_D8]  ; Get the address of the first element of the array.
mov edx, [ebp+Counter] ; Get the current element index.
shl edx, 2             ; Since the size of each element of the array is 4, multiply the index by 4
add eax, edx           ; &arr[i] = The address of the current element
mov [esp], eax         ; Move it on the stack so it can be written by std::cin
  • Great explanation, What is the additional 16 bits between 0x10 and 0xD8 used for? – Vlad Jun 5 '15 at 9:38
  • 1
    @Vlad: what 16 bits? A statement that possibly needs reminding you that not any value here is in "bits" – but maybe you confused it with bytes. Still: how did you come to the conclusion that there is any additional space left between 0x10 and 0xD8? That space is exactly the amount needed for the 50 integers that your original code says there should be, nothing more and nothing less. – usr2564301 Jun 5 '15 at 10:42
5

The var_d8 is the base position of arr in the stack, the size of type int in your machine is 4, so the position of arr[i] in the stack is calculated by:

var_d8 + 4 * i

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.