CLASS 17

Data Structures ( continued )

Structures

struct example  {
    int a, b;     ! 8 bytes
    char c;       ! 1 byte, 1 wasted
    short d, e;   ! 4 bytes, 2 wasted
    int f, g;     ! 8 bytes
}

Total memory requirement for one structure of type example = 24 bytes.

Hence, example should have 24 bytes.

define(example, -24)

The corresponding save statement will be :

save %sp, -88, %sp

The best method is to define all the offsets for the structure, and load the content of the first element in a register, use it to access the other elements. For e.g.

define(offset_a, 0)
define(offset_b, 4)
define(offset_c, 8)
define(offset_d, 10)
define(offset_e, 12)
define(offset_f, 16)
define(offset_g, 20)

set %fp + example, %l0    ! pointer to the first element of structure
Now, to access the individual elements of this structure:
ld   [%l0 + offset_a], %o0   ! %o0 = a
ldub [%l0 + offset_c], %o0   ! %o0 = c
ldsh [%l0 + offset_d], %o0   ! %o0 = d
and so on....

Now, if in the code, there is a declaration like..

example  e1, e2;

Then all we need to do is allocate 24 * 2 bytes of memory.

save %sp, (-64 - 48) & -8, %sp
==> save %sp, -112, %sp

Now, load the base values of both the structures in registers, which can further be defined.

define(e1, -24)
define(e2, -48)

Now, load base of e1 into %l0, base of e2 into %l1

set %fp + e1, %l0
set %fp + e2, %l1

Now, the commands for accessing the elements of the structure are:

ld [%l0 + offset_a], %o0     ! %o0 = e1.a
ld [%l1 + offset_a], %o1     ! %o1 = e2.a
ldub [%l0 + offset_c], %o2   ! %o2 = e1.c
and so on.......

Nesting Of Structures

Here is an example of a nested structure:
struct date 
{
  short day, month, year ; 
} ;

struct person 
{  
  struct date birth_date ; 
  int age ; 
  char sex ; 
} ;

main()
{
   struct date d1, d2 ; 
   struct person p1, p2 ; 

   d1.day = 13 ; 
   d1.month = 5 ; 
   d2.year = 1973 ; 

   p1.birth_date = d1 ; 
   p2.sex = 'M' ; 
   p1.age = 23 ; 
}
Now, we need a 8 bytes of the structure "date" as it contains only 3 short variables.

Further, for the structure "person", we need 16 bytes, and the offsets from the starting address of the structure are given below:

define(offset_date_day, 0)
define(offset_date_month, 2)
define(offset_date_year, 4)
define(offset_age, 8)
define(offset_sex, 12)
A better way of implementing nested structures is given below. Here, each of the nested structures has an offset from the main structure, and each of the fields of the nested structure have an offset specified from the start of the nested structure. Let us take the previous example to clarify things:
struct date d1, d2 ;
struct person p1, p2 ;
Here, we have the offsets defined as follows:
define(d1, -8)
define(d2, -16)
define(p1, -32)
define(p2, -48)
define(birth_date, 0)
define(date_day, 0)
define(date_month, 2)
define(date_year, 4)
define(age, 8)
define(sex, 12)

	.global _main
_main:  save    %sp, -112, %sp   !(64 + 48 = 112, -112 & -8 = -112)

	! d1.day = 13
	mov 	13, %o0
	sth 	%o0, [%fp + d1 + date_day] 

	! d1.month = 5 
	mov 	5, %o0	
	sth 	%o0, [%fp + d1 + date_month] 

	! d2.year = 1973 
	mov 	1973, %o0
	sth 	%o0, [%fp + d2 + date_year] 

 	! p1.birth_date = d1 
 	! p1.birth_date.day = d1.day 
	lduh	[%fp + d1 + date_day], %o0
	sth 	%o0, [%fp + p1 + birth_date + date_day] 

	! p1.birth_date.month = d1.month 
	lduh	[%fp + d1 + date_month], %o0 
	sth 	%o0, [%fp + p1 + birth_date + date_month] 

  	! p1.birth_date.year = d1.year 
	lduh	[%fp + d1 + date_year], %o0
	sth 	%o0, [%fp + p1 + birth_date + date_year] 

	! p2.sex = 'M' 
	mov 	'M', %o0
	stb 	%o0, [%fp + p2 + sex]

	! p1.age = 23 
        mov 	23, %o0
	st 	%o0, [%fp + p1 + age] 
 
	mov 	1, %g1
	ta	0

For class 18 notes, click here

For more information, contact me at tvohra@mtu.edu