Sunday, June 1, 2014

A word about memory - Một vài điều về bộ nhớ



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
The bold 1 is a bit. Now, in the early days of computers, machines could only send eight bits at a time. So naturally, everything is calculated around that. Now that we have that down, we can define what a byte is. A byte is an eight bit binary number (Note: the size of a byte is unstandardized, but it's safe to assume that they are eight bits on all PC's). A nybble is simply half of a byte, or four bits. Whoever came up with these names liked puns. Let's practice. How many bytes can a 32 bit and a 64 bit machine process at a time? Math time:

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 ;).
For reference this what numbers look like in binary (these are unsigned, we will get into them later):
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.
You may have realized by know that both of these forms have a limit to how high they can go. After they've gone past their max numbers, they "wrap around" to the their max negative numbers. This is where we get into positive and negative numbers. How does a computer tell when a number is positive or negative? By signed digits. There are two types of numbers, signed and unsigned. Signed means that the number can be positive or negative. Unsigned means that the numbers must be positive, and the computer doesn't check for the signed digit. The signed digit is merely the first digit in the first byte of the number.

Code:
10110010 10000110 11000001 11110001
It's a positive number!
Follow up: you may want to read this tutorial on how to calculate and understand binary numbers

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
Multi-Dimensional arrays are calculated the same way just, the calculation is done every time you access an element of a deminsion (I don't feel like explaining this).

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