Sie sind auf Seite 1von 2

Gerhard-Mercator University Duisburg

Prof. Dr.-Ing. W. Geisselhardt, Dr.-Ing. H.-D. Hmmer

Fac. 5 IIMT, IT Page D1

Exercises for Course: Computer based Systems


structure of a C program
declarations: include a user file macrodefinition function prototypes declaration of variables definitions executables: block a block a block b (embedded) block b (embedded) block b end block c (embedded) block a #include io900.h #define TRUE 0x01 extern int cal_func(char *, int); void test(); extern int variable; extern char var; char a, b, c; int table[] = {0x1122, 0xa22, 0xc1fa}; void main() { if (a == 0x2b) { var = 0x3f; b = c; } else var = 0xff; }

preprocessor directives in C programs


#include identifier #define identifier xxxx #undef identifier #ifdef identifier #ifndef identifier #else #endif #pragma xxxx for TOPAS #include <stdio.h> #define EXT extern #undef EXT #idef EXT #ifndef EXT include a system (standard) file defines EXT, it is substituted by extern EXT is unknown from here conditional compilation: if EXT is defined cond. comp.: if EXT is not defined cond. comp.: else branch cond. comp.: end of if construct action dependent on implementation forces link to io-page address

#pragma io IOP0 0x00

variables and arrays in C programs


void [unsigned] char [unsigned] int array declaration pointer declaration pointer dereference pointer reference pointer to array access to array elements structures (records) void function(void); char abc = 0x3F; int xyz = 455; int arr[50]; int *pnt; pnt = &xyz; *pnt = 0; pnt = &arr[0]; xyz = arr[2] ; xyz = *(pnt+2); struct coord { unsigned char x; unsigned char y; } myco; union alter { unions struct coord MyCo; (multi-type access) unsigned int MyWd; } uni; no input to, no output from the function abc is 8 bit var.; assign 3F hex to it xyz is 16 bit var; assign 455 decimal arr is an array of 50 integer values declares a pointer to integers pointer gets addr. of variable xyz if pnt points to xyz, xyz gets zero pnt points to first array element normal array element access pointer access if pnt is set to first elem. myco is like a record; it can be assigned by e.g. myco.x = 16; myco.y = 2; uni.MyCo.x = 16; uni.MyWd = 528;

Figure 14: C-Language Basics (1 / 2)

Gerhard-Mercator University Duisburg

Prof. Dr.-Ing. W. Geisselhardt, Dr.-Ing. H.-D. Hmmer

Fac. 5 IIMT, IT Page D2

Exercises for Course: Computer based Systems


specialities of TLCS 900 C-compiler
#pragma io port0 0x00 unsigned char port0; void func(void) { unsigned char indata; data = port0; ... } unsigned char __io(0x00) port0; struct bfield { unsigned char B07:1; ... unsigned char B00:1; } register; register.B01 = 1; union ioport { struct bfield BIT; byte REG; }

#pragma io <I/O name> <address> : port0 is address 0x00 now variable port0 is defined as a byte function func defines a variable indata as byte read data from port0 (SFR for I/O) to variable indata

__io(<address>) <I/O name> : same effect as first two statements above, but only for local use defines a bitfield from MSB downwards; the number behind the colon designates the no of bits (here 1) to access only single bits defines variable register of this structure assigns a 1 to bit no 1 in variable register enables a variable e.g. WDCR of type ioport to be accessed either by WDCR.REG in the range of 0 to 255 or by WDCR.BIT.B00 to WDCR.BIT.B07 with 0 or 1

peculiarities of C statements
a++, ++a, a--, --a a == b a != b a <= b, a >= b a < b, a > b a<<1, a>>3 a += 7 (a == 3 && b == 7) (a==0) ? a = 5 : a = b; increments/ decrements a (before, after) equality unequality less than / greater than or equal less than / greater than but caution because: shifts a 1 position left rsp. 3 positions right Identical to a = a + 7 log. AND; caution: (a=3&&b=7) sets a to 3 and b to 7 conditioned expression; if a equ. zero then ... else ...

program control
if (condition) { statements ; } else statements; while (condition) { statements } do { statements; } while (condition);

for (init; condition; stmt) { statements } switch (expression) { case const1: blocka; case const2: blockb; break; default: blockd; } break; continue;

for (i=0; i<20; i++) { statements }

tries out all cases, unless no break !

terminates forward, e.g. to the end of a loop terminates backward, e.g. directly back to beginning of a loop

Figure 14: C-Language Basics (2 / 2)

Das könnte Ihnen auch gefallen