ARM VERSION 1.2 User Manual

Page 71

Advertising
background image

Writing ARM and Thumb Assembly Language

ARM DUI 0068B

Copyright © 2000, 2001 ARM Limited. All rights reserved.

2-59

Setting up a C-type structure

There are two stages to using structures in C:

1.

Declaring the fields that the structure contains.

2.

Generating the structure in memory and using it.

For example, the following

typedef

statement defines a point structure that contains

three

float

fields named

x

,

y

and

z

, but it does not allocate any memory. The second

statement allocates three structures of type

Point

in memory, named

origin

,

oldloc

, and

newloc

:

typedef struct Point
{
float x,y,z;
} Point;

Point origin,oldloc,newloc;

The following assembly language code is equivalent to the

typedef

statement above:

PointBase RN r11
MAP 0,PointBase
Point_x FIELD 4
Point_y FIELD 4
Point_z FIELD 4

The following assembly language code allocates space in memory. This is equivalent to
the last line of C code:

origin SPACE 12
oldloc SPACE 12
newloc SPACE 12

You must load the base address of the data structure into the base register before you
can use the labels defined in the map. For example:

LDR PointBase,=origin
MOV r0,#0
STR r0,Point_x
MOV r0,#2
STR r0,Point_y
MOV r0,#3
STR r0,Point_z

is equivalent to the C code:

origin.x = 0;
origin.y = 2;
origin.z = 3;

Advertising