User avatar
morphy_richards
Posts: 1603
Joined: Mon Mar 05, 2012 3:26 pm
Location: Epping Forest
Contact: Website

Data or Instructions?

Mon Mar 16, 2015 12:19 pm

Just a general nitpick really ...
From GCSE CS spec:
How the computer knows if it is reading instructions or data
I always thought an instruction came first in bytecode / machinecode / opcode. In 8086 assembler isn't the instruction always the first byte is the instruction, usually you then have data or addresses but that format depends on what the instruction / first byte was. In ARM, instructions are 32 bits but that's still the first 32 bits.

So the processor knows what data is instruction and what is ... data ... because the first pre-defined size of "chunk" is always the instruction. You don't just randomly feed a load of data to a processor without telling it what it has to do with it?

But this, from the GCSE spec. companion ...
Both instructions and data are stored as bit patterns in memory. It would be advantageous if the CPU was able to differentiate between the two [...] In some systems, there is a software flag which allocates certain locations on memory as ‘instruction only’ locations and other areas on memory marked as ‘data only’ locations. In other systems a hardware flag is used to differentiate between the two. This is where an additional bit may be allocated to each bit pattern to flag whether it is data or instruction.
(and that's all it has to say on the matter)
... just seems wrong to me. :? (Not to mention wasteful)

User avatar
PeterO
Posts: 5727
Joined: Sun Jul 22, 2012 4:14 pm

Re: Data or Instructions?

Mon Mar 16, 2015 12:27 pm

Addresses and literal/immediate values in instructions are NOT considered to be data in this context and are just part of the instruction as they are read as part of the "fetch" phase.

Any memory accessed during the "execute" phase is the data that is being talked about.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
morphy_richards
Posts: 1603
Joined: Mon Mar 05, 2012 3:26 pm
Location: Epping Forest
Contact: Website

Re: Data or Instructions?

Mon Mar 16, 2015 12:31 pm

Thanks :)
I'm still a little unclear.
So ...
The Fetch stage is pure instruction.
Anything accessed during Execute is data.

I'm struggling to see where hardware or software flags come into play?

User avatar
PeterO
Posts: 5727
Joined: Sun Jul 22, 2012 4:14 pm

Re: Data or Instructions?

Mon Mar 16, 2015 12:36 pm

Flags in the CPU may determine if the I and D address spaces are the same or separate.

You could imagine a CPU that had two operating modes:
A) One 32K bytes address space shared between I and D
B) 16K bytes of Instructions and 16K bytes of Data.

In case B the 16K I space could be ROM and the 16K D space RAM.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
morphy_richards
Posts: 1603
Joined: Mon Mar 05, 2012 3:26 pm
Location: Epping Forest
Contact: Website

Re: Data or Instructions?

Mon Mar 16, 2015 12:40 pm

Right, that makes sense.

Thanks.

User avatar
DougieLawson
Posts: 38504
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Data or Instructions?

Mon Mar 16, 2015 12:50 pm

Isn't it fair to assume that every thing is data because code is just data bytes with a special meaning that only becomes an instruction when the processor status word points to it and the CPU fetches those byte(s) at the PSW and attempts to execute them.

It's down to the bootstrap loader or loader in the running OS to get the code in the right memory block and update the PSW and register stack pointers to set it running.

The amusing failure happens when your PSW points to the wrong place and tries to execute a string. That's kept me amused with very many postmortem memory dumps for many years.

Whether the data is in volatile (RAM) or fixed storage (ROM) when the processor tries to fetch it doesn't really matter as long as the fetch address isn't outside the addressing range (another cause of very many dumps that I've seen).

Some processors have hardware fencing (called protect keys in S/370) to prevent data and code being mixed and to prevent code storage being written by a non-privileged programs as an alternative to ROM.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

jdb
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2299
Joined: Thu Jul 11, 2013 2:37 pm

Re: Data or Instructions?

Mon Mar 16, 2015 12:57 pm

The answer is always "it depends".

For a Harvard architecture, the instructions and "data" are distinct and separate and this is enforced by the architecture. E.g. PIC chips - the program code executes from a segment of flash, data storage is in EEPROM or a small SRAM.

At the innermost layer, ARM processors such as the ARM11 or Cortex-A7 have a Harvard-like CPU communicating with instruction and data caches - I-cache/D-cache. Each has separate data pathways in/out of the processor logic. The I-cache operates solely on addresses generated by the program counter/branch predictor/instruction prefetch, the D-cache on any loads and stores that the processor does.

At the next outer layer, memory is unified at the L2 cache. Data or instructions can be present in these cache lines. This is where polymorphic/self-modifying code needs to be careful: the L1 I-cache needs to be invalidated and the L1 D-cache flushed to ensure that the instructions that the CPU fetches are valid.

With the L2 cache and outer memory system (SDRAM or even disk storage) having no hardware differentiation between instructions and data, this is where the MMU comes into play to enforce a certain level of security.

In a paged memory management system, including the one implemented on ARMv6/ARMv7, page tables describe address space in the current virtual memory layout (typically 4k chunks). The page tables also contain information such as "no-execute" "read-only" and some cache behaviour flags. This protects against, at a page granularity level, "data" pages being executed as instructions or secure sections of code being modified.
Rockets are loud.
https://astro-pi.org

User avatar
PeterO
Posts: 5727
Joined: Sun Jul 22, 2012 4:14 pm

Re: Data or Instructions?

Mon Mar 16, 2015 1:07 pm

The OP asked about GCSE CS level information. I'm sure we are all impressed by your vast knowledge, but it is somewhat off-topic !
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 38504
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Data or Instructions?

Mon Mar 16, 2015 1:13 pm

jdb wrote: In a paged memory management system, including the one implemented on ARMv6/ARMv7, page tables describe address space in the current virtual memory layout (typically 4k chunks). The page tables also contain information such as "no-execute" "read-only" and some cache behaviour flags. This protects against, at a page granularity level, "data" pages being executed as instructions or secure sections of code being modified.
IBM's system/370 and its descendants (S/390 & zSeries) have dynamic address translation (virtual addresses are mapped to real storage addresses) and page tables (originally it was 4K pages for 24-bit & 31-bit machines, now it's 1M pages for 64-bit). The protect key stuff (to keep data and code apart) is done in there in much the same way.

There's lots of lovely similarity between ARM and S/370, the ARM folks clearly studied Gene Amdahl's work extensively before designing their chips. Bare metal coding on the ARM isn't as alien as I expected when I looked at that.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
DougieLawson
Posts: 38504
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Data or Instructions?

Mon Mar 16, 2015 1:16 pm

PeterO wrote:The OP asked about GCSE CS level information. I'm sure we are all impressed by your vast knowledge, but it is somewhat off-topic !
PeterO
But Mr Richards didn't specify how dumbed down his GCSE syllabus needs to be.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
morphy_richards
Posts: 1603
Joined: Mon Mar 05, 2012 3:26 pm
Location: Epping Forest
Contact: Website

Re: Data or Instructions?

Mon Mar 16, 2015 1:42 pm

I have a few very smart children in my class, their comparative intellect would apparently have dwarfed mine when I was at a comparable level to them. I expect them to go far and I'm hoping to equip them with a higher level of understanding so that they can just 'work it out' themselves.

But still it's still the basic concept I'm trying to get right in my head!

Say I load a compiled program (./OneAddOne) , part of it, the following data might be stored somewhere in some RAM.

Code: Select all

add	r2, r2, r3
I would have though that instruction could be stored pretty much anywhere in memory, even though it's an instruction. At this stage it's just some data as indistinguishable from say ... part of an image I right click copied in firefox.

What identifies the start is the first instruction at the start of the program file, the sequence of instructions that follow it and the ./ to execute it.

The instruction is loaded into IAS.

The Controller decodes "add" to a bit pattern by accessing a specific location (perhaps ROM) that is somehow prevented from being mixed with other data, perhaps with a flag. I imagine this as a kind of look-up.

The bit pattern is executed by the ALU, the counter is incremented and allows the location of the next instruction to be got.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Data or Instructions?

Mon Mar 16, 2015 1:48 pm

Within the realm of GCSE, I suspect the answer is "it doesn't". Certainly in vanilla Von Neumann architecture (ARM, x86) there is no separation of instructions and data, unlike Harvard architecture (AVR, PIC) where they are on totally distinct buses and have different word sizes.

Maybe that is what you need to be talking about.

Otherwise I would be wandering around the other points this thread has made and wondering which one the examiner had in mind. The Fetch/Execute cycle clearly differentiates, but it doesn't seem to match the question too well -- "an instruction is what you read when you want an instruction". Page protection bits are to stop rogues modifying programs, but they do mark some memory as program or data. The companion paragraph seems to reference these, but these flags are for security, not to let the computer know where its program and data are located. I can't see how putting another bit in there would be of any use whatsoever or, indeed, what would be "advantageous" about drawing the distinction in the first place. Those of us who programmed 8-bit micros and PDP11s have fond memories of treating instructions as data.

I have never, ever come across any architecture that marked each individual word as program or data.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Data or Instructions?

Mon Mar 16, 2015 2:41 pm

An instruction such as add r2, r2, r3 exists as a number in memory.It is not an instruction, it is not decoded to a bit pattern; it is already a bit pattern.

Let's drop the ARM for a moment and talk about a PDP11, and to celebrate the moment, let's also talk in octal.

It's a two address architecture, so your example looks like this:

Code: Select all

ADD R3, R2
And it is assembled (compiled) into the number 060302. The PDP11 had such a simple machine code that you could read it easily in many cases. Here the 06 means add, the 03 means R3 and the 02 means R2.

That bit pattern 060302 is just a number, it is indistinguishable from data, say the decimal number 24770, or the Unicode character 惂. The only thing that distinguishes it as an instruction is that the computer executes it.

Let's get perverse.

Code: Select all

00000 062703
00002 062703
00004 001337
We can imagine location zero holds an add instruction. It adds ... something to R3. 27 says take register 7 and look at the address it points to. Once you have that value, increment register R7.
Register R7 just happens to be the program counter, and it is currently pointing at location 00002. So we add the value 060327 to R3, and then increment the program counter so it is now looking at location 00004, just in time to execute the next instruction, which is a Branch If Equal (as determined by the leading 001) to this location plus octal 337.

But we can look at those numbers in a different way. Instead of starting at location zero, let's jump directly to location 2. We know what 060327 does; it adds R3 to whatever comes next, in this case location 00004, which contains 1337.

Either scenario is equally likely, neither of them are right or wrong. In the first case location 2 was data and location 3 was program, in the second case it was the other way round.

You may complain that my program would have been written as

Code: Select all

ADD #062703, R3
BEQ label
and therefore the immediate value is program, not data. However the computer does not know what I wrote, and to it register R7 is just another register. I could have written

Code: Select all

ADD [R7]+, R3
.WORD 062703
BEQ label
and produced exactly the same memory contents. And if I had written

Code: Select all

ADD [R5]+, R3
BEQ label
you would have had no difficulty in saying that whatever register R5 pointed to was data.

[Ref: http://www.village.org/pdp-11/faq.pages/PDPinst.html]

PS. In the case of the PDP-11 there is no microcode to confuse you; it was all TTL logic chips -- quad NAND gates and so forth. Microcode is not at issue in this question, and if it exists it is stored in a totally distinct ROM inside the CPU that is not shown on the Von Neumann diagram. Back in 1980 I didn't encounter the concept until 1st year degree level.

User avatar
morphy_richards
Posts: 1603
Joined: Mon Mar 05, 2012 3:26 pm
Location: Epping Forest
Contact: Website

Re: Data or Instructions?

Mon Mar 16, 2015 2:46 pm

The only thing that distinguishes it as an instruction is that the computer executes it.
That is exactly what I thought, although you have put it beautifully.

I will have to contact the exam board as I cannot reconcile this information with the specification.

User avatar
morphy_richards
Posts: 1603
Joined: Mon Mar 05, 2012 3:26 pm
Location: Epping Forest
Contact: Website

Re: Data or Instructions?

Mon Mar 16, 2015 2:52 pm

Just as an aside ...
rurwin wrote:An instruction such as add r2, r2, r3 exists as a number in memory.It is not an instruction, it is not decoded to a bit pattern; it is already a bit pattern.
Of course, it must be, silly me!

So the job of the decoder in the control unit is to convert that bit pattern to a suitable control signal that activates the relevant circuitry of the ALU?

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Data or Instructions?

Mon Mar 16, 2015 2:55 pm

Yes

User avatar
morphy_richards
Posts: 1603
Joined: Mon Mar 05, 2012 3:26 pm
Location: Epping Forest
Contact: Website

Re: Data or Instructions?

Mon Mar 16, 2015 2:56 pm

:)

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Data or Instructions?

Mon Mar 16, 2015 2:59 pm

You may find this interesting: http://en.wikipedia.org/wiki/74181

User avatar
morphy_richards
Posts: 1603
Joined: Mon Mar 05, 2012 3:26 pm
Location: Epping Forest
Contact: Website

Re: Data or Instructions?

Mon Mar 16, 2015 3:06 pm

It says this is no longer in production but if I can find an equivalent IC this could make a great standalone independent project for able students , it would be fantastic to actually teach people to build a computer and when I say build I mean BUILD.
Image

User avatar
DougieLawson
Posts: 38504
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Data or Instructions?

Mon Mar 16, 2015 3:22 pm

morphy_richards wrote:It says this is no longer in production but if I can find an equivalent IC this could make a great standalone independent project for able students , it would be fantastic to actually teach people to build a computer and when I say build I mean BUILD.
Image
Take a look at Computerphile on YouTube https://www.youtube.com/channel/UC9-y-6 ... 29I7JiwpnA you will enjoy watching Prof David Brailsford's lectures on there.

You'll also want to subscribe as they've just started a series about the development of the Sinclair MK14 to the Acorn System One to the BBC Micro and the ARM processor https://www.youtube.com/watch?v=izy6h_vvSxU
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

hippy
Posts: 7137
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Data or Instructions?

Mon Mar 16, 2015 3:58 pm

I did a quick search and turned up this, and the full text seems to put things into better context -

http://www.fairwaterhighschool.co.uk/wp ... ersion.pdf
How the computer knows if it is reading instructions or data

Both instructions and data are stored as bit patterns in memory. It would be advantageous if the CPU was able to differentiate between the two, since the outcome may not be desirable if, for instance, data was processed as an instruction or vice-versa.

In some systems, there is a software flag which allocates certain locations on memory as ‘instruction only’ locations and other areas on memory marked as ‘data only’ locations. In other systems a hardware flag is used to differentiate between the two. This is where an additional bit may be allocated to each bit pattern to flag whether it is data or instruction.

INTERESTING FACT

Hackers often exploit the fact that the CPU can be tricked into executing data as an instruction. This is sometimes called a buffer overflow attack.

User avatar
morphy_richards
Posts: 1603
Joined: Mon Mar 05, 2012 3:26 pm
Location: Epping Forest
Contact: Website

Re: Data or Instructions?

Mon Mar 16, 2015 4:14 pm

While we are on the subject of this book, if you look at page 11 , I also have a bit of an issue with the diagram showing instructions being executed on a dual core processor.
It fairness it does say
In theory,a dual-core CPU should be able to process instructions twice as fast as a single-core CPU. However, this isn't always the case as sometimes Instruction 2 may need the result of Instruction 1 before it can be processed.

On the whole though, a computer running many programs at the same time will run faster on a multi-core processor than on a single‐core processor.
But the diagram is wrong.
I was under the impression that at the very least one 'process' (lets call it instructions 1 to 10) would run on one core.
You could have your OS kernel running on core 1 and your prime number calculator (or whatever) running on core 2 but you couldn't have instructions from your prime number calculator running on both core 1 and core 2 unless you carefully programmed it to work that way.

Students are expected to discuss factors affecting performance and I've taught them to be able to discuss multiple core systems as a method that could improve performance if it is running many simultaneous processes or if the software has been designed to use threading to make use of multiple cores.

plugwash
Forum Moderator
Forum Moderator
Posts: 3578
Joined: Wed Dec 28, 2011 11:45 pm

Re: Data or Instructions?

Mon Mar 16, 2015 4:37 pm

morphy_richards wrote:While we are on the subject of this book, if you look at page 11 , I also have a bit of an issue with the diagram showing instructions being executed on a dual core processor.
It fairness it does say
In theory,a dual-core CPU should be able to process instructions twice as fast as a single-core CPU. However, this isn't always the case as sometimes Instruction 2 may need the result of Instruction 1 before it can be processed.
I think they are confusing a dual core processor (which processes two totally seperate instruction streams) with a "dual issue" processor that can move more than one instruction from the same stream through the pipeline at the same time.

User avatar
clive
Forum Moderator
Forum Moderator
Posts: 1013
Joined: Tue Feb 07, 2012 8:19 pm

Re: Data or Instructions?

Tue Mar 17, 2015 11:52 pm

Ring the exam board (WJEC?) . Always :)

But basically for GCSE we're looking at The Little Man Computer to explore registers, op codes, operands, addressing etc. At the moment no GCSE spec includes computer architecture.

User avatar
morphy_richards
Posts: 1603
Joined: Mon Mar 05, 2012 3:26 pm
Location: Epping Forest
Contact: Website

Re: Data or Instructions?

Wed Mar 18, 2015 8:09 am

LMC is good, lots of fun accompanying cartoons but its just a GUI to a processor simulation. (nothing wrong with that)
I'm thinking this is something that could be made available further down the school (no time for such frivolity at GCSE) Perhaps it could be a kit containing a big breadboard, LED displays, ALU, memory, other assorted bits and some information.

It's not something I would teach in class, rather give it to some children to work on at lunchtime.

But its just pie in the sky at the moment.

Return to “Off topic discussion”