-1

I have this struct defined

struct outer {
   int *x;
   struct {
      short s[2];
      int i;
   } inner;
   struct outer *next;
};

Along with this function to initialize the struct

void init_outer(struct outer *ss) {
    ss->inner.s[1] = ss->inner.s[0];
    ss->x = &(ss->inner.i);
    ss->next = ss;
    ss->inner.i = 18;
}

I need to translate init_outer to assembly.

init_outer:             
   movw 8(%rdi), %ax    
   movw %ax, 10(%rdi)   
   leaq 12(%rdi), %rax  
   movq %rax, (%rdi)    
   movq %rdi, 16(%rdi) 
   // assembly code for ss->inner.i = 18 GOES HERE;
   retq                 

I've managed to translate the first 3 lines to assemply, however I'm not sure about the last one. I thought that the last line would be movl 18, 12(%rdi) in assembly, but it's wrong for some reason. Is there something I'm missing?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

1 Answers1

1

Translation from C to assembly is called compilation. It is done by the compiler

https://godbolt.org/z/ndoeeP

init_outer:                             # @init_outer
        movzwl  8(%rdi), %eax
        movw    %ax, 10(%rdi)
        leaq    12(%rdi), %rax
        movq    %rax, (%rdi)
        movq    %rdi, 16(%rdi)
        movl    $18, 12(%rdi)
        retq
0___________
  • 60,014
  • 4
  • 34
  • 74
  • Thanks, I realized that I just missed the $ sign in front of the 18. Also what compiler and what compiler option do you recommend that I use on godbolt? –  Jul 29 '20 at 06:49
  • You should start with the compiler you have installed on your machine, use the same version on godbolt. Then later you can switch between different versions and see the difference in output. As for compiler options `-Ox` is a good start. – Waqar Jul 29 '20 at 07:29
  • Also, checkout the `x86` wiki and this [answer](https://stackoverflow.com/a/38552509/2279422) and this [answer](https://stackoverflow.com/questions/63015986/how-to-generate-godbolt-like-clean-assembly-locally). – Waqar Jul 29 '20 at 07:32
  • @Waqar Are you sure this is not a typo? As far as I'm concerned, there is no `-Ox` option to gcc. – fuz Jul 29 '20 at 08:59
  • by `x` I mean substitute with `1`, `2`, `3`, `s` etc. It is the algebraic 'x'. I realize I should have said it clearly. Personally, I use `-O3` mostly. And sometimes `-O0` to see the exact line by line asm translation without optimizations – Waqar Jul 29 '20 at 09:02
  • 2
    @Waqar: MSVC does have a literal `-Ox` option (which is similar to `-O2`, full optimization), so my first reaction was "MSVC has that, GCC doesn't, and this is AT&T syntax asm". If you mean a wildcard, it's better to use italics like -O*x*, or pick one like `-O1` or `-Og` if you want to use code formatting for something you can literally copy/paste. – Peter Cordes Jul 29 '20 at 14:53