0

Given the following code:

int x=4;
int g=2;
int z=x/g;

as far as I know, the value '4' is stored in the memory in a place belong to x and '2' is stored in g's place in the memory. Now, when the CPU gets the z=x/g command, first of all he gets the value of x and g from the memory, then he calculates the result, and stores it in z. But what happens as the following code runs:

int x=4;
int z=x/2;

After the CPU gets '4', how can he get the '2'? does a CPU command can hold Data rather then addresses and opcode?

Brian Cain
  • 14,403
  • 3
  • 50
  • 88
izac89
  • 3,790
  • 7
  • 30
  • 46
  • 1
    You might try [looking at the produced assembly](http://stackoverflow.com/questions/137038/how-do-you-get-assembler-output-from-c-c-source-in-gcc). – Waleed Khan Apr 03 '13 at 18:26
  • 3
    Remind me again - what does `\\` do in a C program? – 500 - Internal Server Error Apr 03 '13 at 18:27
  • Depends a lot on the compiler - they could be straight loaded into registers, pushed/popped off of the stack, etc. - best bet is to look at the assembly, as others have mentioned. – JerKimball Apr 03 '13 at 18:28
  • @500-InternalServerError the first rule of C is that you don't talk about that operator \*wink\* – Nik Bougalis Apr 03 '13 at 18:28
  • What exactly are you asking? Whether "a cpu" (what kind? it matters) can hold immediate arguments in math operations? It's a common feature yes (though for example x86 can't have an immediate divisor). Or whether the compiler actually uses that capability? – harold Apr 03 '13 at 18:29

2 Answers2

2

You could illuminate yourself by coding this and looking at the disassembly. Regardless - the 2 is stored in a register, as well as the 4. Then the operation is performed.

BreadicalMD
  • 336
  • 1
  • 16
0

Many CPU commands can have embedded data. This is called "an immediate operand". Rarely more than one piece of data, though. The details vary wildly by CPU architecture and by individual command.

Since the commands itself reside in memory, technically, having a data item embedded in a CPU command still counts as "data in memory". For the record, addresses are a kind of immediate operand, too. To the CPU, it's just another number.

Specifically dividing by two is normally implemented via a right shift by 1 operation (either bitwise or arithmetic, depends). On all CPU's that I know the shift command can store the shift-by operand as an immediate.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281