05 - Identificando arrays y estructuras

Experiementando con struct

El siguiente programa usa una estructura (y objeto de esa estructura y su puntero correspondiente). Vamos a compilarlo e identificar en qué quedan convertidas sus partes.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

struct A {
  int x[5];
  char y;
  int z;
};


void test(struct A *q, int f) {
  int i;
  q->y = 'a';
  q->z = 15;
  for (i = 0; i < 5; i++) {
    q->x[i] = i * f;
  }
}

int main() {
  struct A *objA;
  srand(time(NULL));
  objA = (struct A *) malloc (sizeof(struct A));
  test(objA,rand());

  printf("%d\n",objA->z +  objA->x[2] + objA->x[4]);
  return 0;
}

Mientras creaba este ejercicio, me sorprendieron algunas cosas. Vamos a ver si las podemos recrear.

Analicemos

Ahora que tenemos una idea de como se implementa las estructuras en assembly, analicemos el siguiente programa

<bah>:
   0:       push   r12
   2:       test   rsi,rsi
   5:       mov    r12,rdi
   8:       push   rbp
   9:       mov    rbp,rdx
   c:       push   rbx
   d:       mov    rbx,rsi
  10:       je     32 
  12:       nop    WORD PTR [rax+rax*1+0x0]
  18:       mov    rsi,QWORD PTR [rbx]
  1b:       mov    rdi,rbp
  1e:       call   r12
  21:       test   eax,eax
  23:       je     56 
  25:       js     40 
  27:       mov    rbx,QWORD PTR [rbx+0x18]
  2b:       test   rbx,rbx
  2e:       xchg   ax,ax
  30:       jne    18 
  32:       pop    rbx
  33:       pop    rbp
  34:       xor    eax,eax
  36:       pop    r12
  38:       ret
  39:       nop    DWORD PTR [rax+0x0]
  40:       mov    rbx,QWORD PTR [rbx+0x10]
  44:       test   rbx,rbx
  47:       je     32 
  49:       mov    rsi,QWORD PTR [rbx]
  4c:       mov    rdi,rbp
  4f:       call   r12
  52:       test   eax,eax
  54:       jne    25 
  56:       mov    rax,rbx
  59:       pop    rbx
  5a:       pop    rbp
  5b:       pop    r12
  5d:       ret

Preguntas guía:

Main no es el principio

A continuación un callgraph de las funciones que invoca un ejecutable compilado con gcc.

Para que me sirve esa información . . . veamos demo.