Structs


Structs are simply descriptions of memory blocks. For example, in C you can say:

 struct person {  char firstname[40];  char lastname[40];  int age; }; 

This doesn't do anything by itself, except give you ways of intelligently using 84 bytes of data. You can do basically the same thing using .equ directives in assembly language. Like this:

  .equ PERSON_SIZE, 84  .equ PERSON_FIRSTNAME_OFFSET, 0  .equ PERSON_LASTNAME_OFFSET, 40  .equ PERSON_AGE_OFFSET, 80 

When you declare a variable of this type, all you are doing is reserving 84 bytes of space. So, if you have this in C:

 void foo() {  struct person p;  /* Do stuff here */ } 

In assembly language you would have:

 foo:  #Standard header beginning  pushl %ebp  movl %esp, %ebp  #Reserve our local variable  subl $PERSON_SIZE, %esp  #This is the variable's offset from %ebp  .equ P_VAR, 0 - PERSON_SIZE  #Do Stuff Here  #Standard function ending  movl %ebp, %esp  popl %ebp  ret 

To access structure members, you just have to use base pointer addressing with the offsets defined above. For example, in C you could set the person's age like this:

  p.age  =  30; 

In assembly language it would look like this:

  movl $30, P_VAR + PERSON_AGE_OFFSET(%ebp) 




Programming from the Ground Up
Programming from the Ground Up
ISBN: 0975283847
EAN: 2147483647
Year: 2006
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net