help! (summary & solution)

B Reytblat brt at pyuxvv.UUCP
Fri Mar 23 11:42:01 AEST 1984


^
|
comon, eat that blank line, punk. make my day.



Thanks to all who answered my question.
The responses seem to fall into two categories:

************************************************************************
	1. Initialization takes place at RUN-time, i.e. AFTER
the program has started executing:
------------------------------------------------------------------------
What's wrong with:

	struct FOO {...} name, **addr;

	**addr = (struct FOO **)0x80;
	*addr  = &name;

I think that this gives the effect that you want, that is, storing the
address of name at location 0x80.

					Tony Hansen
					pegasus!hansen
------------------------------------------------------------------------
I might have misunderstood, but won't this do what you want?

struct FOO { int x; } ;
struct FOO *xp = (struct FOO *)0x80;

main(){
	struct FOO name;
	xp = &name; }
			{harpo,houxm,ihnp4}!pyuxss!aaw
			Aaron Werman
------------------------------------------------------------------------
Try
struct a {
	int a_x;
};

main () 
{
	((struct a *)100)->a_x = 10;
}

which produces the following assembler output:

	.file	"a.c"
	.data
	.text
	.align	4
	.globl	_main
_main:
	.word	.R1
	jbr 	.L13
.L14:
	movl	$10,100			/* this is the actual access */
	ret#0
	.set	.R1,0x0
.L13:
	jbr 	.L14
	.data

Of course, you ought to parameterise it:

#define DEV_ADDR 100
#define DEV_REGS (struct a *) DEV_ADDR

then use
	DEV_REGS->a_x = /* whatever */

			John Hutchinson
			hou5g!jrh
			AGS Computers at AT&T ISL, Holmdel.


************************************************************************
	2. The initialization takes place at COMPILE-time. AND
	   the initialized pointer is bound to an address outside
	   the .data section of the program.  Unfortunately,
	   as the submission below indicates, IT CAN'T BE DONE in C :

From: gnu at sun.uucp (John Gilmore)
Subject: Re: help! (much shorter)

You can't assign an absolute address to a piece of text or data that
comes out of the C compiler.

You can make an absolute address look like a struct (or other variable),
then do a structure assignment at runtime to initialize it if you want.
For example:

#define	DISKCTLR	(*(struct diskregs *)0xFFF304)
struct diskregs diskinit = {...};

	DISKCTLR = diskinit;
	DISKCTLR.command = DISK_RESET;
	...

Note that in the structure assignment, you can't control the ordering
of the storage references, so if it's an I/O device (not real memory),
you're probably better off doing it by hand.  Most devices are finicky
about what registers you write in what order anyway.

**********************************************************************

	Solution, you might ask? I've settled on the two file approach:

file1:
.......................................
extern	struct FOO name;
	struct FOO *p_name = &name;
.......................................

file2:
.......................................
extern	struct *p_name;
	struct FOO {...} name = {@@@};

	other code. references to both name & p_name.
.......................................

	and then bind the .data section of file1 to the absolute
 address at LINK-EDIT-time.

			Thanks to all,

			B.Reytblat
			...!pyuxvv!brt
			(201)-981-2044 (office)



More information about the Comp.lang.c mailing list