0

C Code:

void charTypes(char a)
{
    return ;
}

void intType(int a)
{
    return ;
}

Assembly:

0000000000000000 <charTypes>:
   0:   f3 0f 1e fa             endbr64 
   4:   55                      push   rbp
   5:   48 89 e5                mov    rbp,rsp
   8:   89 f8                   mov    eax,edi
   a:   88 45 fc                mov    BYTE PTR [rbp-0x4],al
   d:   90                      nop
   e:   5d                      pop    rbp
   f:   c3                      ret    

0000000000000010 <intType>:
  10:   f3 0f 1e fa             endbr64 
  14:   55                      push   rbp
  15:   48 89 e5                mov    rbp,rsp
  18:   89 7d fc                mov    DWORD PTR [rbp-0x4],edi
  1b:   90                      nop
  1c:   5d                      pop    rbp
  1d:   c3                      ret  

In case of char parameter, Why do we need to assign edi to eax? What prevents this?

DWORD PTR [rbp-0x4],dil
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Sreeraj Chundayil
  • 5,548
  • 3
  • 29
  • 68
  • *Why do we need to assign edi to eax?* We don't, but you told the compiler not to optimize so it didn't. This is just some detail of compiler internals and how it represents 8-bit stuff internally. And since you compiled without optimization, those internals end up producing some actual wasted asm instructions. Like I said in answering your previous question, un-optimized code is usually not interesting to look at. Crap like this is one of the reasons for that. – Peter Cordes Apr 26 '20 at 01:52
  • @PeterCordes: What would be a best optimization level as a beginner in disassembly? I don't want to miss learning actual operation. – Sreeraj Chundayil Apr 26 '20 at 02:05
  • I'd recommend `-Og` or `-O1 -fno-inline-functions` at least, and most of the time `-O2 -fno-tree-vectorize -fno-unroll-loops`. There isn't a way that avoids optimizing away constants and stuff, so you have to write functions that take args and return a result or modify an array or something else that can't just optimize down to `return 1234`. [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) has some suggestions, and a link to Matt Godbolt's talk. Using https://godbolt.org/ to match up source lines with asm helps a lot. – Peter Cordes Apr 26 '20 at 02:09
  • @PeterCordes: With these optimizations I found out that the function preamble is missing. So are the optimized code really executable by computer? – Sreeraj Chundayil Apr 27 '20 at 08:56
  • Yeah, `-fomit-frame-pointer` is enabled at most optimization levels. Those instructions in each function that set up RBP as a frame pointer and then tear down the stack frame aren't special or necessary, RBP is just a normal register. There's no magic here. [x86\_64 : is stack frame pointer almost useless?](https://stackoverflow.com/q/31417784). If you compile a whole program and single-step the asm with a debugger (like gdb `layout reg`), you'll see the instructions that really execute. – Peter Cordes Apr 27 '20 at 09:01

0 Answers0