Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22307

Why doesn't clang use memory-destination x86 instructions when I compile with optimization disabled? Are they efficient?

$
0
0

I wrote this simple assembly code, ran it and looked at the memory location using GDB:

    .text

.global _main

_main:
    pushq   %rbp
    movl    $5, -4(%rbp)
    addl    $6, -4(%rbp)
    popq    %rbp
    ret

It's adding 5 to 6 directly in memory and according to GDB it worked. So this is performing math operations directly in memory instead of CPU registers.

Now writing the same thing in C and compiling it to assembly turns out like this:

...  # clang output
    xorl    %eax, %eax
    movl    $0, -4(%rbp)
    movl    $5, -8(%rbp)
    movl    -8(%rbp), %ecx   # load a
    addl    $6, %ecx         # a += 6
    movl    %ecx, -8(%rbp)   # store a
....

It's moving them to a register before adding them together.

So why don't we add directly in memory?

Is it slower? If so, then why is adding directly in memory even allowed, why didn't the assembler complain about my assembly code in the beginning?

Edit: Here is the C code for the second assembly block, I have disabled optimization when compiling.

#include <iostream>

int main(){
 int a = 5;
 a+=6; 
 return 0;
}

Viewing all articles
Browse latest Browse all 22307

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>