c - Assembler debug of undefined expression -


i'm trying better understanding of how compilers produce code undefined expressions e.g. following code:

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

this assembler code generated gcc 4.8.2 (optimisation off -o0 , i’ve inserted own line numbers reference purposes):

(gdb) disassemble main dump of assembler code function main: (1) 0x0000000000000000 <+0>:    push   %rbp (2) 0x0000000000000001 <+1>:    mov    %rsp,%rbp (3) 0x0000000000000004 <+4>:    movl   $0x5,-0x4(%rbp) (4) 0x000000000000000b <+11>:   mov    -0x4(%rbp),%eax (5) 0x000000000000000e <+14>:   lea    0x1(%rax),%edx (6) 0x0000000000000011 <+17>:   mov    %edx,-0x4(%rbp) (7) 0x0000000000000014 <+20>:   mov    %eax,-0x4(%rbp) (8) 0x0000000000000017 <+23>:   mov    $0x0,%eax (9) 0x000000000000001c <+28>:   pop    %rbp (10) 0x000000000000001d <+29>:  retq    end of assembler dump. 

execution of code results in value of i remaining @ value of 5 (verified printf() statement) i.e. i doesn't appear ever incremented. understand different compilers evaluate/compile undefined expressions in differnet ways , may way gcc i.e. different result different compiler.

with respect assembler code, understand:

ignoring line - 1-2 setting of stack/base pointers etc. line 3/4 - how value of 5 assigned i.

can explain happening on line 5-6? looks if i reassigned value of 5 (line 7), increment operation (required post increment operation i++) abandoned/skipped compiler in case?

these 3 lines contain answer:

lea    0x1(%rax),%edx mov    %edx,-0x4(%rbp) mov    %eax,-0x4(%rbp) 

the increment operation isn't skipped. lea increment, taking value %rax , storing incremented value in %edx. %edx stored overwritten next line uses original value %eax.

they key understanding code know how lea works. stands load effective address, while looks pointer dereference, math needed final address of [whatever], , keeps address, instead of value at address. means can used mathematical expression can expressed efficiently using addressing modes, alternative mathematical opcodes. it's used way multiply , add single instruction reason. in particular, in case it's used increment value , move result different register in 1 instruction, inc instead overwrite in-place.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -