Question

How can I translate this bit of assembly to C code?

Assembly:

foo:
   dd 1
   dd 2
   dd 3
   dd baa - foo
  dw 5
  dw 6
  ; ....  
baa:
  db 00
  db 00
 ;...

C

struct foo
{
  int a;
  int b;
  int c;
  int d;
  short e;
  shot f; 
};

struct baa
{
  int a;
  int b;
};

and then:

foo f;
baa b;

f.a = 1;
f.b = 2;
f.c = 3;
f.d = ?? // how do I translate this?
f.e = 5;
f.f  = 6;

b.a = 0;
b.b = 0;
Was it helpful?

Solution

The intention here is (probably!) to calculate the size of the foo structure.

In 'bare' assembly (without any fancy tricks like structures), the only way to do this without manually counting is to determine difference between the addresses of the first and last byte of the structure, plus one. Or, in this case, the difference between the address of the first byte of the structure and the address of the first byte past the structure.

In C, you have the sizeof operator, which determines (at compile time) the size of an instance of a type. This captures the intention much better. In your case, you'd say

struct foo f;

// ...

f.d = sizeof(struct foo);

or alternatively,

struct foo f;

// ...

f.d = sizeof(f);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top