Does anyone know of a good link that explains the inner workings of the pc , explaining how the Stack , Cpu , registers and what not work together .
An easy explanation if possible.
A very good explanation can be found in Computer Organization and design. It uses the MIPS processer which is much easier to use than intel. Pretty much all processors have a stack with the convention that it grows downward. This seems backward, abnormal or crazy but that's the way they do it. In software(with no error checking) it would look something like
Code:
int stack[MAX_SIZE];
int top = MAX_SIZE;
void push(int item)
{
stack[--top] = item;
}
int pop()
{
return stack[top++];
}
As you can see, the stack grows downward into lower memory address. Every time you push, top is subtracted one word(just 1 index in this case) and then the new top of the stack is assigned to the item. The pop does this basically in reverse.
int main(void)
{
int num1 = 1;
int num2 = 2;
int num3 = 3;
int num4 = 4;
// this outputs
cout << &num1 << '\n' // 006AFF4
<< &num2 << '\n' // 006AFF0
<< &num3 << '\n' // 006AFEC
<< &num4 << '\n'; // 006AFE8
return(0);
}
Main is a function so it's variables are local to it. What most compilers will do is write something like
Code:
int main(void)
{
// make room for 4 stack variables with word size of 1
// a=>stack[top]
// b=>stack[top + 1]
// c =>stack[top + 2]
// d=>stack[top+3]
top -= 4;
// assign a, b, c, d to some values...
// several calls to operation<<
// The usual operation is that the variables are
// pushed on to the stack right to left and then the callee
// is able to look at the stack and get the varibables
// printf is easier to use
push(stack[top + 3]);
push(stack[top + 2]);
push(stack[top + 1]);
push(stack[top]);
// this would be a constant string stored in the data
// section somewhere
push("a = %d, b = %d, c = %d, d = %d");
// this would push the current instruction
// pointer + WORD_SIZE onto the stack and then jump to
// the location of the global printf routine.
// Then when printf returns it would jump to the
// current instruction pointer + WORD_SIZE on the stack
call printf;
return 0;
}