Embedded Development Compilers
by Don Hair and Cesar Quiroz


Listing One
packed struct {
    unsigned char field1:4;
    unsigned char field2:3;
    unsigned short field3:1;
    unsigned short field4:5;
    unsigned short field5:2;
    unsigned short field6:4;
    unsigned short field7:4;
} s;
  ...
s.field5 = 3;


Listing Two
(a)
packed struct {
    char c;
    int i;
    char c1;
    short s;
} s;
 ...
s.i = 0x11223344;

(b)
move.l  #$11223344,_s+1 ; move 4 bytes from s.i at an odd address

(c)
move.b  #$11,_s+1   ; move first byte of s.i at an odd address
move.w  #$2233,_s+2 ; move next 2 bytes of s.i at an even address
move.b  #$44,_s+4   ; move last byte from s.i at an even address

(d)
move.b  #$11,_s+1   ; move first byte of s.i at an odd address
move.b  #$22,_s+2   ; move next byte of s.i at an even address
move.b  #$33,_s+3   ; move next byte of s.i at an odd address
move.b  #$44,_s+4   ; move last byte from s.i at an even address


Listing Three
void func (packed struct S *);
 ...
packed struct S {
    int i1;
} s1;
 ...
packed struct {
    char c;
    packed struct S s2;
} s3;
 ...
func (&s3.s2);
func (&s1);


Listing Four
(a)
char c;
int i;
short s;
float f;

(b)
        .sect   .bss
        .align  4
        .globl  c
c:
        .space 1
        .align  4
        .globl  i
i:
        .space 4
        .align  4
        .globl  s
s:
        .space 2
        .align  4
        .globl  f
f:
        .space 4


Listing Five
extern int f( int );
static int g( int x ) 
{
    int y = f( x );
    return f( y );
}
int h( int a, int b )
{
    int t;
    if (a < b) {
        int z = b - a;
        t = 2*z;
    } else {
        int w = a*a - b;
        t = w*w;
    }
    return g( t )+1;    
}

Listing Six
int in_century( int year ) { return year % 100; }
void print_date( int yy, int mm, int dd )
{
    printf( "%d-%d-%d", in_century( yy ), mm, dd );
}


Listing Seven
int j, k;
func() {
    register int i;
    ...
    j = 1;
    asm(" move.l `k`,`i`");
    asm(" move.l `i`,`j`");
    j++;
}


Listing Eight
func() {
    register int i, j, k, l, m;
    ...
    asm(" move.l #$100000,d0");
    asm(" movec d0,vbr");
    ...
    for (i=0; i<10; i++)
                asm(" ...");
}


3


