Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

How to write ANY value

 
 
aleksa
Guest
Posts: n/a
 
      02-21-2011
On Feb 21, 8:45*pm, Sherm Pendley <sherm.pend...@gmail.com> wrote:
> aleksa <aleks...@gmail.com> writes:
> > On Feb 21, 8:05*pm, Keith Thompson <ks...@mib.org> wrote:
> >> * * #define ARBITRARY_DATA 42
> >> * * *p = ARBITRARY_DATA;

>
> > I was actually trying to avoid that approach,
> > because it requires the CPU to load a register with a
> > specific value, which isn't really necessary.

>
> Not necessarily - all of the assembler languages I'm familiar with have
> an instruction to store a literal value, and I assume any decent compiler
> would emit such an instruction if given the above code.


Yes, I've forgotten about that...
But that is usually a 1 byte longer instruction
 
Reply With Quote
 
 
 
 
aleksa
Guest
Posts: n/a
 
      02-21-2011
On Feb 21, 8:45*pm, Sherm Pendley <sherm.pend...@gmail.com> wrote:
> aleksa <aleks...@gmail.com> writes:
> > On Feb 21, 8:05*pm, Keith Thompson <ks...@mib.org> wrote:
> >> * * #define ARBITRARY_DATA 42
> >> * * *p = ARBITRARY_DATA;

>
> > I was actually trying to avoid that approach,
> > because it requires the CPU to load a register with a
> > specific value, which isn't really necessary.

>
> Not necessarily - all of the assembler languages I'm familiar with have
> an instruction to store a literal value, and I assume any decent compiler
> would emit such an instruction if given the above code.


Yes, I've forgotten about that...
But that is usually a 1 byte longer instruction

And I'm not sure if ARM can do that, especially if its
base + offset = data
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-21-2011
On 2/21/2011 2:36 PM, aleksa wrote:
> On Feb 21, 8:05 pm, Keith Thompson<ks...@mib.org> wrote:
>> #define ARBITRARY_DATA 42
>> *p = ARBITRARY_DATA;

>
> I was actually trying to avoid that approach,
> because it requires the CPU to load a register with a
> specific value, which isn't really necessary.
>
> As I said to Jens:
> "Writing whatever value is stored at that moment in whatever CPU
> register",
> without the need to initialize it first.


There's no way to do this in C. There may be ways to do it in
a particular implementation of C, with a particular set of compiler
options, under a particular set of conditions that enable or prevent
particular optimizations, if you're a left-handed seventh son of a
seventh son programming in an abandoned churchyard by the light of
the second full Moon of an odd-numbered month.

(In short, if you're counting the individual instructions that
the machine executes to carry out the intent of your code, C is not
the way to express that intent. Wrong tool; use another.)

--
Eric Sosman
d
 
Reply With Quote
 
Anders Wegge Keller
Guest
Posts: n/a
 
      02-21-2011
Keith Thompson <kst-> writes:

> Yes, but I'd be more comfortable with something like this:
>
> #define ARBITRARY_DATA 42
> *p = ARBITRARY_DATA;


It just occurred to me, that noone have mentioned the volatile
keyword yet. Especially this case, where the same value is written
repeatedly, is warranting a volatile declaration of p. Otherwise te
compile may end up optimizing any but the first write into oblibion.

--
/Wegge

Leder efter redundant peering af dk.*,linux.debian.*
 
Reply With Quote
 
aleksa
Guest
Posts: n/a
 
      02-21-2011
On Feb 21, 9:36*pm, Anders Wegge Keller <we...@wegge.dk> wrote:

> *It just occurred to me, that noone have mentioned the volatile
> keyword yet.


Hardware registers are to be defined as volatile, yes.
(not that it changes the original question, though)
 
Reply With Quote
 
aleksa
Guest
Posts: n/a
 
      02-21-2011
On Feb 21, 6:42*pm, Ben Pfaff <b...@cs.stanford.edu> 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)
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-21-2011
aleksa <> writes:
> On Feb 21, 6:42Â*pm, Ben Pfaff <b...@cs.stanford.edu> 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?

--
Keith Thompson (The_Other_Keith) kst- <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-21-2011
On Feb 21, 10:14*pm, Keith Thompson <ks...@mib.org> wrote:

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


The first one.
 
Reply With Quote
 
Sjouke Burry
Guest
Posts: n/a
 
      02-21-2011
Keith Thompson wrote:
> aleksa <> writes:
>> On Feb 21, 6:42 pm, Ben Pfaff <b...@cs.stanford.edu> 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.......
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      02-21-2011
Sjouke Burry <> writes:

> Keith Thompson wrote:
>> aleksa <> writes:
>>> On Feb 21, 6:42 pm, Ben Pfaff <b...@cs.stanford.edu> 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.
--
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
 
 
 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57