I want to access a specific position on memory using Assembly and C.
I've create the following struct:
struct node{
uint64_t x[5];
uint64_t y;
struct node * next;
};
Later, I created a object of that type.
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
struct node{
uint64_t x[5];
uint64_t y;
struct node * next;
};
void foo();
struct node * ptr;
int main(){
struct node* ptr = (struct node *) malloc(sizeof(struct node));
ptr->next = NULL;
foo();
printf("%lu\n", ptr->y);
return 0;
}
Now, on Assembly, I want to change the value of y.
.section .data
.text
.globl foo
.extern ptr
foo:
//access ptr->y
leaq (ptr + 40), %r12
movq $42, %r12
ret
I want %r12 to have the address of ptr->y. I imagined that it would get the correct address, because ptr.x would be first in memory and it weights 8*5 = 40 bytes, however that's not the case.