A word about memory
This tutorial was intended to extend people's knowledge on what really goes on "behind the scenes" in scripts. I know this is probably not necessary since pawn is a scripting language, and knowledge of memory is not really needed. However, you'll find time and time again the "superior" programmers are the ones that have knowledge of how computers truly work. Please note, this is only scratching the surface though, and talks about how pawn works with memory, nothing more.
What’s with the .amxx?
I'm going to stray from the path a little bit, because I feel it's good that every AMXX scripter should know this.
When I first began scripting for AMXX, I had no clue what a .amxx file was or how AMX Mod or AMXX got their names. AMX stands for AbstractMachine eXecutive, hopefully now you see where AMX and AMXX got their names. Their are two file types .amx and .amxx. This first one is the "original" file type pawn had. The .amx file type however is not compatible with 32 bit and 64 bit machines simultaneously and requires a special compile for each one. The .amxx file changes that and can be used on both machines with only one compile.
Don't byte off more than you can nybble!
Bits, nybbles, and bytes. If your reading this, you probably have no clue what those are. Lets examine: a bit is just a single digit in a binary number.
Code:
10010010
Code:
We know that 8 bits is a byte, so lets divide by eight: How much on a 32 bit machine? 32 / 8 = 4 So a 32 bit machine can process 4 bytes at a time! How much on a 64 bit machine? 64 / 8 = 8 So a 64 bit machine can process 8 bytes at a time! Now know you know exactly why a 64 machine is faster than a 32 bit machine ;).
Code:
32 bit: 10110010 10000110 11000001 11110001 This is 2,995,175,921 in decimal form. 64 bit: 10110010 10000110 11000001 11110001 10110010 10000110 11000001 11110001 This is 12,864,182,629,456,855,537 in decimal form.
Code:
10110010 10000110 11000001 11110001 It's a positive number!
What's in a variable?
As a programmer, variables are a must in programming, it's like virtual water. But what really is a variable? We know it can hold data, like integers, floats, and boolean types; sadly though, this is the only thing most beginning to intermediate level scripters know.
Pawn revolves around a single data type, the cell which is 4 or 8 bytes depending if the machine is a 32 bit or 64 bit machine. Also, cells are signed.
Code:
new var;
You should know what this does, it declares a variable named var that can hold an integer. Now, this is what really happened, the VM (Virtual Machine) will allocalate one cell. Remember cells are 4 or 8 bytes depending on the machine. Hurray! Now you've probably started to catch on what’s going on "behind the scenes"!
Arrays - How do you fit everything in there?
An array is just a big area of memory that holds a bunch of variables stacked on top of each other. The first element (i.e. a[5] would be the fifth element) really "points" to the beginning of the array.
Here is what one could visualize an array as filled with some values:
But wait, how do you get from here (a[0]) to there (some other element like a[5])? A simple calculation of course! Using the size of a cell, the VM can calculate an offset from the base of the array, to the location of the area of memory you want access to.
Example:
Code:
I want to access element three. a[3] Here is what happens. Lets say the base address is (in hex, don't worry if this looks confusing, the value is 4) 0x4. We are running on a 32 bit machine, so the size of the cell is 4 bytes and remember, we want access to the third element of the array. Math: Formula: b = base c = size of cell e = element you want access to. m = final memory location m = b + (c * e) or using the information I provided in the problem: m = 0x4 + (4 * 3) Answer: m = 0x10 or 16
Here is what you could visualize it as:
Different Data Types
I'm not going to go into this topic to deep.
Pawn only has knowledge of one data type as you should know by now, the cell. But what about floats or booleans? To overcome this, pawn "tags" variables, but they are still treated as cells. Booleans as you know only hold two values, true or false, one or zero. Any number past or below these are invalid. As for floats, they go beyond the length of this tutorial. However, Pawn uses the IEEE standard for floating point arithmetic, which you can learn more about here.
If I made any mistakes in this tutorial or this tutorial is complete crap, please PM me via AMXX.
0 nhận xét:
Post a Comment