Originally Posted by
Maz
Now I am just interested in finding out how type is defined. What makes something a type? So lets not argue then, could you educate me, and show/explain me definition of type in C?
At the risk of hijacking this thread even more.....(MODS may split these conversations out if they want too)
So, what is a type? That is a very good question. The answer is there are in fact 3 categories of types for the C language: primitive, composite-built in, and composite user(I will discuss primitive here, all others can be later if required).
Primitive Type
A primitive type for any language is something that the parser can directly translate into machine code. It is "primitive" because it is directly built into the parser logic (usually a series of function calls) without recursion. The "without recursion" part makes it a primitive. For instance, lets look at the primitive type of "int x". ^
Now, when we write something like: "int x = 5;" in C, the parser can directly translate that into machine code. In the data section(specifically .BSS section*), a variable named x would be created of type int (for 32-bit architecture) this would be a double word. For NASM syntax, declaration of X in the .BSS would be:
Code:
x resd 1; reserves 1 double word- 32 bits(sizeof(int) on 32 bit OS).
^ It is important to note that the job of finding and declaring variable names in their appropriate sections is often the task of the lexical scanner and not the parser.
*BSS was actually not an initial construct of a program. It was added in the 1950's by an IBM assembler and actually stands for "Block Started by Symbol".
Now since the declaration of x is taken care of, we now need to look at the assignment. In this case we declare x to be 5. So now the parser needs to handle this, and luckily since an int is a primitive we can convert this directly to machine code*:
Code:
LEA EDX, [X]; load address of x to EDX register
MOV EAX, 5; place 5 into EAX reg (32-bit)
MOX [EDX], EAX; place 5 into x
*Note, since this is a primitive we do not need any loop statements (which means the parser did not use any recursion) to complete this expression.
This is what is going on "under the hood" as you like to say with the compiler. Now, if you would like to discuss composite types ask the mods to split out this thread since the OP isn't done. All a built in composite type is a primitive, that is recursively built by the compiler. (This does involve stack usage and the stack pointer, along with looping for optimization).
*Extra Info:*
As for function calls, they are handled by the 'call' statement in NASM assembly. Note based on parser design for languages like C the return value from a function is stored in the EAX register on x86 machines.
Pointers:=(if you get it feel free to PM me)These are constructs which denote the manner in which the parser deals with the register. Did you note the use of the square brackets '[' ']'? If they are present that means indirect addressing in NASM, which means place whatever is going there into the memory address that is stored by that register. So without '[' ']' means direct and with means indirect when it comes to registers**.
**Note the instruction LEA has its own requirements which are beyond the topic of this post.
EDIT:Added clarification based on Laser's response.