Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to write ANY value

Reply
Thread Tools

How to write ANY value

 
 
Sjouke Burry
Guest
Posts: n/a
 
      02-21-2011
Ben Pfaff wrote:
> Sjouke Burry <(E-Mail Removed)> writes:
>
>> Keith Thompson wrote:
>>> aleksa <(E-Mail Removed)> writes:
>>>> On Feb 21, 6:42 pm, Ben Pfaff <(E-Mail Removed)> wrote:
>>>>> This may work:
>>>>>
>>>>> unsigned char x;
>>>>>
>>>>> *p = x;
>>>> It doesn't as I would like.
>>>> My compiler stores 0 in reg, then reg to memory.
>>>> (as opposed to just storing a reg to memory)
>>> Does your compiler generate code to store 0, or does the register
>>> just happen to contain 0?
>>>

>> It might be useful to give x some value.......

>
> The whole point was to not give x a value; please read back to
> the beginning of the thread.

So *p gets random data from who knows what location? Right,
I did not know about that valuable mechanism......
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-21-2011
On 02/22/11 10:59 AM, Sjouke Burry wrote:
> Ben Pfaff wrote:
>> Sjouke Burry <(E-Mail Removed)> writes:
>>
>>> Keith Thompson wrote:
>>>> aleksa <(E-Mail Removed)> writes:
>>>>> On Feb 21, 6:42 pm, Ben Pfaff <(E-Mail Removed)> wrote:
>>>>>> This may work:
>>>>>>
>>>>>> unsigned char x;
>>>>>>
>>>>>> *p = x;
>>>>> It doesn't as I would like.
>>>>> My compiler stores 0 in reg, then reg to memory.
>>>>> (as opposed to just storing a reg to memory)
>>>> Does your compiler generate code to store 0, or does the register
>>>> just happen to contain 0?
>>>>
>>> It might be useful to give x some value.......

>>
>> The whole point was to not give x a value; please read back to
>> the beginning of the thread.

> So *p gets random data from who knows what location? Right,
> I did not know about that valuable mechanism......


Did you read the OP?

"Some memory-mapped devices require a write to a specific
memory address in order to do something.
The data written is of no importance."

Hardware watchdogs are a common example of this.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      02-21-2011
Ian Collins <(E-Mail Removed)> writes:

> Did you read the OP?
>
> "Some memory-mapped devices require a write to a specific
> memory address in order to do something.
> The data written is of no importance."
>
> Hardware watchdogs are a common example of this.


Some 16-color EGA/VGA display modes were another case. Here's
the comment that I put into the Linux 16-color framebuffer driver
many years ago when I wrote it:

/* The VGA's weird architecture often requires that we read a byte and
write a byte to the same location. It doesn't matter *what* byte
we write, however. This is because all the action goes on behind
the scenes in the VGA's 32-bit latch register, and reading and writing
video memory just invokes latch behavior.

To avoid race conditions (is this necessary?), reading and writing
the memory byte should be done with a single instruction. One
suitable instruction is the x86 bitwise OR. The following
read-modify-write routine should optimize to one such bitwise
OR. */

(It seems that someone modified the code and didn't update the
comment, though, since the code no longer uses a single
instruction to do it. I guess it wasn't necessary.)
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
James Waldby
Guest
Posts: n/a
 
      02-21-2011
On Mon, 21 Feb 2011 22:38:15 +0100, Sjouke Burry wrote:
> Keith Thompson wrote:
>> aleksa <aleksazr@gm..> writes:
>>> On Feb 21, 6:42 pm, Ben Pfaff wrote:
>>>> This may work:
>>>> unsigned char x;
>>>>
>>>> *p = x;
>>> It doesn't as I would like.
>>> My compiler stores 0 in reg, then reg to memory. (as
>>> opposed to just storing a reg to memory)

>>
>> Does your compiler generate code to store 0, or does
>> the register just happen to contain 0?
>>

> It might be useful to give x some value.......


The OP doesn't care what value is written, and just
wants a write to occur to a specific address. This is
a common paradigm in embedded systems, where you may
need to strobe an address via a read or write, and the
data involved is don't-care.

I have a 525-line C program that runs on an AT90S2313
(2K program memory, 128 byte SRAM) clocked at 500KHz
and if making changes occasionally have to look at the
assembly listing to figure out where I can save a few
cycles -- so I can understand the OP's concern about
unnecessarily loading a register. It's a bit more
difficult to understand the expectation of doing so
in a portable way.

--
jiw
 
Reply With Quote
 
Edward A. Falk
Guest
Posts: n/a
 
      02-22-2011
In article <(E-Mail Removed)>,
aleksa <(E-Mail Removed)> wrote:
>On Feb 21, 6:42*pm, Ben Pfaff <(E-Mail Removed)> wrote:
>> This may work:
>>
>> * * unsigned char x;
>>
>> * * *p = x;

>
>It doesn't as I would like.
>My compiler stores 0 in reg, then reg to memory.
>(as opposed to just storing a reg to memory)


My guess is that the compiler is generating code to initialize x.

I'd just leave it alone; we're talking like a billionth of a second
here.

By the way, more importantly:

volatile unsigned char *p;

--
-Ed Falk, http://www.velocityreviews.com/forums/(E-Mail Removed)
http://thespamdiaries.blogspot.com/
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-22-2011
aleksa <(E-Mail Removed)> writes:
> Some memory-mapped devices require a write to a specific
> memory address in order to do something.
> The data written is of no importance.
>
> How can I instruct the compiler to write
> whatever value it finds handy in that moment?
>
> P.S.
> That won't make the code much better, I'm just curious.


This might do what you want:

volatile register int junk;
volatile int *p = /* ... */;
*p = junk;

But there are no guarantees. You're trying to generate specific machine
code. A compiler's job is to generate machine code that has a specified
effect.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
aleksa
Guest
Posts: n/a
 
      02-22-2011
On Feb 21, 11:35*pm, James Waldby <(E-Mail Removed)> wrote:

> It's a bit more difficult to understand the
> expectation of doing so in a portable way.


I didn't know if this is doable in portable C,
so I've asked here to be sure.

Since it looks like it is not,
I could post it somewhere else.
 
Reply With Quote
 
aleksa
Guest
Posts: n/a
 
      02-22-2011
On Feb 22, 10:00*am, aleksa <(E-Mail Removed)> wrote:

> Since it looks like it is not,
> I could post it somewhere else.


I've wrote this before seeing that
there is a next page in google.

Gordon Burditt's "last assignment statement"
and Keith Thompson's "register" are good ideas.

Thanks, everyone.
 
Reply With Quote
 
aleksa
Guest
Posts: n/a
 
      02-22-2011
On Feb 21, 6:27*pm, aleksa <(E-Mail Removed)> wrote:
> Some memory-mapped devices require a write to a specific
> memory address in order to do something.
> The data written is of no importance.
>
> How can I instruct the compiler to write
> whatever value it finds handy in that moment?
>
> P.S.
> That won't make the code much better, I'm just curious.


Andreas B. has an excellent idea - storing the pointer to itself!
http://embdev.net/topic/209508

*p = p;

or

BYTE *adr = ...; // or WORD / DWORD
*adr = adr;

Tested it on ARM and am quite sure it works on IA32:
mov [eax],al
mov [eax],ax
mov [eax],eax
 
Reply With Quote
 
aleksa
Guest
Posts: n/a
 
      02-22-2011
On Feb 22, 12:42*pm, aleksa <(E-Mail Removed)> wrote:

> mov [eax],al
> mov [eax],ax
> mov [eax],eax


I'm more interested in ARM and
didn't think much about IA32.

IA32 could just store immediate to
direct address - one instruction only.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
How to use Response.write to write to a specific area on a aspx pa =?Utf-8?B?QWJlbCBDaGFu?= ASP .Net 6 05-03-2006 10:16 PM
DVD Write speed - Not able to write at 8 speed Vincent Wonnacott Computer Support 1 09-16-2004 03:14 PM
How to open a new console and write something out write printf() Peter Hansen C++ 1 08-24-2003 11:49 AM



Advertisments