Wing0 :
i7Baby :
Schooling in programming can help - a lot. Meanwhile you can research 'programming basics' using a search engine. eg Flowcharting, DataFlow Diagrams, etc.
Cool thanks. One more question though, the language I'm learning is kforth, and the way it works is there's a data stack. And in the code the values that come from equations etc are put on the top of the data stack. when you do something like add, the values are taken from the stack and added. And you can run through the program step by step.
ex.
{1 1 + R0!}
1 is put on the stack, then the next one is put on the stack, then they are added together, and the answer is put on the stack, then that is stored in register 0.
Do all languages work in a way similar to this?
It depends.
What you're describing is how most computational calculators work. Many programming languages use something conceptually similar called a call stack. Many microarchitectures have instructions designed for efficient call stack operation.
Each thread in a process has a call stack that controls the flow of data s the thread enters and returns from function calls. At a minimum, the call stack contains the following:
1. A return address pointer, which is the base address of the next instruction in the calling function to be executed once the called function completes
2. The value of the calling function's frame pointer, which is the base address of the stack frame of the for the calling function (this is used to roll back the stack to the proper position)
3. A region of space for the calling function to pass parameters into the called function
4. A region of space for the called function's local variables
Imagine the following
C++:
int foo()
{
int a = 0, b = 1, c = 2, d = 3, e, f;
e = bar(a,b,c,d);
f = baz();
}
In the example above, there are three functions, foo(), bar(), and baz(). Only foo() is described, and only bar() takes parameters.
In the example, the functions bar() and baz() are called by the function foo(). foo() is at some point called by another function.
When foo() is called, the frame pointer (also called the base pointer) points to a location in memory in which the current function's local variables start. The function's parameters and local variables can all be computed by adding or subtracting an offset to the frame pointer, so this makes addressing them easy. In the example above, foo()'s frame pointer may point to the address of the integer
a.
a can be computed by adding an offset of 0 to the frame pointer,
b can be computed by subtracting an offset of sizeof(int) to the frame pointer (call stacks typically start at the top of the address space and increase downward),
c can be computed by subtracting an offset of 2*sizeof(int) and so on.
When foo calls bar() it first pushes the return address onto the stack and adjusts the stack pointer accordingly. The return address will probably be an instruction to start calling baz(). Then, it pushes the caller's frame pointer onto the stack and adjusts the stack pointer accordingly. Then, it will push the values of a,b,c, and d onto the stack, and adjust the stack pointer accordingly. Then, it will set the frame pointer to the value of the stack pointer (such that they are coincident), and jump to the beginning of bar()'s code. One of the first things that bar() will do is adjust the stack pointer by the size of bar()'s frame. As a result, all local variables, no matter how many there are, are created at once.
When bar() is finished, the process is reversed. The stack pointer is rolled back to the frame pointer (destroying the callee's frame), the caller's frame pointer is restored from the stack to the frame pointer register (restoring the caller's frame), and program jumps to the instruction stored in the return address (exiting the function).
There are minor variations in calling conventions between ABIs and architectures, but the process is more or less the same.