Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 269 Vote(s) - 3.58 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does the compiler shift left by 0?

#1
After I wrote this simple program, when I went to the disassembly mode in Visual Studio I noticed something strange: the compiler added an instruction to shift left by 0 bits.

Why does it do that?

This is the C++ code:

#include <iostream>

using namespace std;

int main(int argc, char **argv) {


if (argc != 3)
return 0;

if (strcmp(argv[1], "-r") == 0) {
printf("test");
}

return 0;
}

This is the assembly code:

...
return 0;
00131C94 xor eax,eax
00131C96 jmp main+57h (0131CC7h)

if (strcmp(argv[1], "-r") == 0) {
00131C98 push offset string "-r" (0138B30h)
00131C9D mov eax,4
00131CA2 shl eax,0 <------------------------- HERE
00131CA5 mov ecx,dword ptr [argv]
00131CA8 mov edx,dword ptr [ecx+eax]
00131CAB push edx
00131CAC call _strcmp (01313D9h)
00131CB1 add esp,8
00131CB4 test eax,eax
00131CB6 jne main+55h (0131CC5h)
printf("test");
00131CB8 push offset string "test" (0138BD0h)
00131CBD call _printf (01313E8h)
00131CC2 add esp,4
...
Reply

#2
Notice that this is used in the evaluation of `argv[1]`.

In general, `argv[N]` needs to be translated by the compiler to `*((char**) ((char*) argv + N * sizeof *argv))`: each pointer is `sizeof *argv` bytes after the next. When `N` is not known at compile-time, the multiplication needs to be there, and `shl` is the normal way of doing this.<sup>*</sup>

Since `N` is known at compile-time, but you've not enabled optimisations, I would have guessed this would compile to

00131C9D mov eax,1
00131CA2 shl eax,2

Apparently Visual Studio is able to simplify this to what you're seeing even when optimisations are disabled, but without optimisations, it still isn't able to get rid of the `shl` entirely.

<sub><sup>*</sup> In this specific case, `shl` wouldn't be needed even if `N` is not known at compile-time: `[ecx+eax*4]` can be accessed with a single instruction. That would be another optimisation that would normally be performed.</sub>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through