RISC Machine Data Structure Word Alignment Problems?

Peter da Silva peter at ficc.uu.net
Mon Feb 12 02:37:37 AEST 1990


Use structs internally.

Provide functions to read and write each structure, that do the needed
conversions. Never touch the external format internally.

For example:

	Analog accumulator:

		| flags  | val.lo   val.hi |
		+--------+--------+--------+
		| BYTE 0 | BYTE 1 | BYTE 2 |

	struct accumulator {
		char flags;
		int value;
	};

	read_accumulator(addr, info)
	char *addr;
	struct accumulator *info;
	{
		info->flags = addr[0];
		info->value = addr[2];
		info->value = (info->value << 8) | addr[1];
	}

	write_accumulator(addr, info)
	char *addr;
	struct accumulator *info;
	{
		*addr++ = info->flags;
		*addr++ = info & 0xFF;
		*addr   = (inf >> 8) & 0xFF;
	}
-- 
 _--_|\  Peter da Silva. +1 713 274 5180. <peter at ficc.uu.net>.
/      \
\_.--._/ Xenix Support -- it's not just a job, it's an adventure!
      v  "Have you hugged your wolf today?" `-_-'



More information about the Comp.lang.c mailing list