Re: "Mastering C Pointers"....

From: Alan Connor (zzzzzz_at_xxx.yyy)
Date: 11/03/03


Date: Mon, 03 Nov 2003 01:59:06 GMT

On Sun, 02 Nov 2003 21:30:37 GMT, Roose <nospam@nospam.nospam> wrote:
>
>
>> I do wonder why K&R didn't find it important enough to include a reference
>> to it in the index to the 2nd Edition....
>
> Not to be redundant, but this is probably because it is not common in
> colloquial usage among C programmers. It probably exists to give compiler
> writers a convenient term for a general idea, which the regular C programmer
> needn't be concerned with.
>
>> Whereas Roose's explanation of "object" wasn't quite as useful as the one
>> above, I was very much attracted to his ideas about anchoring the abstract
>> in the real world of cpu registers and such.
>
> I'm glad you found it useful.
>
>> Perhaps it wouldn't be a bad idea to learn a little assembly language at
>> this point?
>
> That might help, but you don't even need to learn a specific machine. You
> can have a quite simplified model of what assembly language is and it will
> be helpful in C programming. After all, it is different for each machine,
> so you don't want to have _too_ specific an idea.
>
> Basically you have a CPU which has a clock say every microsecond if you're
> on a gighertz machine.

Wouldn't that be "nanosecond"?

  A program is a stream of instructions (pretend they
> are stored in 32 bits in a binary format). Pretend it is a big array of
> bytes. You start at the beginning, and just drag through the stream. They
> are executed as fast as possible on these discrete clock ticks. Some
> instructions take more cycles than others. Adds are cheap (say they take 1
> cycle), multiplies a little slower (2 cycles), divides slower (4 cycles).
> These are numbers I pulled out of my ass, of course it's different on every
> machine.

One assumes that you washed them before posting :-|

  You also have all the bitwise operators in C as single
> instructions, more or less. You have floating point and integer
> instructions. Character strings are arrays of integers (ASCII). Pointers
> are integers as well.
>
> There are instructions to load a word from memory into a CPU register, and
> vice versa, to store a word from a CPU register to memory. Note that memory
> accesses are much more expensive than arithmetic instructions (pretend 50
> cycles on average). All the instructions only work on CPU registers. So to
> add to integers in memory, you would have to:
>
> 1. load them both from memory to CPU registers
> 2. execute the add instruction and specify those two registers
> 3. store the result back to memory
>

So memory is just dumb storage. I didn't realize that.

> Memory is just a big array of bytes. If you have 1 gig of memory, pretend
> it is addressed 0 .. 0x3FFFFFFF. This is 2^30-1, or 1 gig. 0xFFFFFFFF is
> 2^32-1, which means 4 gigs. If you have heard Apple's stupid advertisements
> that you can have more than 4 gigs of RAM in their PCs, that is because they
> use more than 32 bits for pointers (i.e. the G5's 64-bit).

Don't get THAT at all, but that's okay for now.

>

In your previous post you recommended that I learn hexadecimal (which I
have a slight handle on: 0-9-F with one character to describe identify
a nibble.

By this you mean learning to convert it to decimal, right?

> Also, you know that if's and goto's can be substituted for all for and while
> loops.

I do?

> Loops are just a convenient abstraction. On the machine, they are
> all implemented in terms of if's and goto's (called jumps in assembly
> language, you jump from one instruction to another than is not sequential).
> So basically the CPU drags through the instructions sequentially, until it
> hits a jump instruction.
>

Sounds like the b command in sed. (oops! The b command in sed must be
like the jump instruction in C :-)

> If you want to know about I/O, you'll have to learn about interrupts and
> such,

>and I'll stop here because that is more than enough to absorb.

Well, I knew THAT at least.

>
> (Note: nitpickers will be flamed. I _know_ for a fact these things are not
> all true on every machine, but is a convenient model, in the interest of
> giving something _concrete_, again)
>

Singe the hair off their balls: I'm getting a lot out of this.

It's really hard to learn the basics of anything if you get too bogged down
in exactness.

>> May I also point out that quoting the ANSI standard to a novice is about
>> as useful as replying in Swahili :-)
>
> Indeed.
>
> Roose
>
>

I read this carefully, twice, consulting K&R for basic definitions.

At present I see each piece of hardware as having a control panel with
switches and dials (card) with the program deciding which switch to flip
or dial to turn, in what order...

Thanks again Roose. There is altogether too little practical talk in
the programming world, which makes it very hard for novices.

-- 
Alan C    this post ends with    w
                                 q


Relevant Pages

  • Re: "Mastering C Pointers"....
    ... A program is a stream of instructions (pretend they ... cycle), multiplies a little slower (2 cycles), divides slower. ... to store a word from a CPU register to memory. ... All the instructions only work on CPU registers. ...
    (comp.lang.c)
  • Re: Cancel error without goto
    ... saved you some memory, even if it saved only half a dozen bytes. ... days, at least on the processors I used, most instructions were two or ... three bytes (an opcode followed by an address or by some data) and in some ... entering hex codes thru a calculator style programming device. ...
    (microsoft.public.vb.general.discussion)
  • Re: Looking For - 65x02 CPU Simulator Demo!
    ... start at and it executed the machine code and displayed how the instructions and data flowed from memory, through the various CPU registers and out. ...
    (comp.sys.apple2)
  • Re: Requesting advice how to clean up C code for validating string represents integer
    ... technical definition of a programming language) which in ordinary ... usage has a "wide variety of exact meanings in many walks of life", ... whether some random set of memory cells in a C core image, ... north-west relative to the rest of the Bay Area. ...
    (comp.lang.c)
  • Re: MAKEINTRESOURCE in win32asm
    ... > practical use, as there is no reason, in Asm Programming ... (which would also require a second read from memory to complete), ... haven't noticed it...you must have had some address pointer, ... ooh, "messages"...the value zero can mean "window created", the value ...
    (alt.lang.asm)