[net.lang.c] Help!

brt@pyuxvv.UUCP (B Reytblat) (03/20/84)

$$$$$$$$$	<- bribe to the line-eater

This is a rather long submission consisting of questions, theories
and examples:

	1. I'm running C programs in a STAND-ALONE environment.
	Problem:
		How does one bind an INITIALIZED variable to a specific
	address? What I want to do is some thing like this:

		struct FOO{ ... } name;
		(struct FOO *)0x80 = &name

	Needless to say, cc barfs on the 0x80 part.

	2. I have attempted to do this by splitting the program in two
	source files:

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

	file2:
		static struct FOO name;
		extern struct FOO *p_name;

	and then binding the .data section of  file1 to the address in
	question using an allocation tool similar to ld(1). As you can see,
	p_name must be accessible inside file2. Also, in physical memory, 
	p_name is surrounded by other things, so I can't put name and p_name
	into the same source file (or else they will go into the same
	.data section, and end up in contiguous memory locations.) 

	Unfortunately, that didn't work either. It seems (and this is pure
	speculation based on a few hours of hacking different versions of
	this) that cc(1) understands (&name) to be a compile-time constant
	rather than a reference to a variable.  I haven't looked at the
	code to see if this is really true (and if I did, I wouldn't know
	where to look :-).

	3. I'm sure some of you have run into this sort of thing before.
	Am I completely off base? What have I missed? Are there any 
	"nice" solutions to the original problem of bindidng an
	initialized variable explicitly? Or is this another one
	of those things that C cannot do?

			Thanks for help in advance,
			B.Reytblat
			...!pyuxvv!brt
			(201)-981-2044 (office)

"... I'm a hacker,
	she's a hacker,
	   he's a hacker,
		Wouldn't you like to be a hacker too?
     Be a hacker,
	Try a little hackin'
     Be a hacker,
	Try a little hackin' ..."

gnu@sun.uucp (John Gilmore) (03/21/84)

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.

brt@pyuxvv.UUCP (B Reytblat) (03/23/84)

^
|
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@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)

sllu@ucla-cs.UUCP (10/31/85)

"..., line 341: compiler error: parameter stack overflow"

I encountered the above error while compiling (cc) SPICE3.0 on a SUN station.
Has anyone encountered similar problem? If yes how did you solve it?

Has anyone successfully installed SPICE3.0 on a SUN workstation?

Thanks in advance.

Shih-Lien Lu // UCLA Computer Science Department // +1 213-825-2266
	3689 Boelter Hall // Los Angeles, California 90024 // USA
	ARPA:   sllu@UCLA-CS.ARPA  -or-  sllu@CS.UCLA.EDU
	UUCP:   ...!(ihnp4,ucbvax)!ucla-cs!sllu
-- 
Shih-Lien Lu // UCLA Computer Science Department // +1 213-825-2266
	3680 Boelter Hall // Los Angeles, California 90024 // USA
	ARPA:   sllu@UCLA-CS.ARPA  -or-  sllu@CS.UCLA.EDU
	UUCP:   ...!(ihnp4,ucbvax)!ucla-cs!sllu

DPHARP01%ULKYVX.BITNET@wiscvm.ARPA (02/22/86)

I am looking for an implementation of either the DES or RSA
encryption algorithims written in C.  Any flavor of C will do, although
VAX C would be nice if I had a choice.  Any help on where I might
look, etc. would be greatly appreciated.

David Harpe
University of Louisville

BITNET: DPHARP01@ULKYVX
Phone:  (502) 588-6303