Raised This Month: $32 Target: $400
 8% 

A word about memory


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Zenith77
Veteran Member
Join Date: Aug 2005
Old 08-27-2006 , 13:34   A word about memory
Reply With Quote #1

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 Abstract Machine 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.
__________________
Quote:
Originally Posted by phorelyph View Post
your retatred

Last edited by Zenith77; 05-08-2007 at 23:04.
Zenith77 is offline
Zenith77
Veteran Member
Join Date: Aug 2005
Old 08-27-2006 , 13:58   Re: A word about memory
Reply With Quote #2

Yes it is my first tutorial, thank you .
__________________
Quote:
Originally Posted by phorelyph View Post
your retatred
Zenith77 is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 08-27-2006 , 15:46   Re: A word about memory
Reply With Quote #3

You're wrong about the VM processing tags differently. Tags are basically just a futile attempt by the compiler to make things other than cells. Once compiled, there are no tags - everything is processed by the VM as a cell.

EDIT: Also, bools CAN hold any number in Pawn, they just get a tag mismatch or something to that effect. Here is a proof of concept script:

Code:
#include <amxmodx> public plugin_init() {     new bool:mybool = bool:3     log_amx("mybool [%d]",_:mybool) }
__________________

Last edited by Hawk552; 08-27-2006 at 15:50.
Hawk552 is offline
Send a message via AIM to Hawk552
Zenith77
Veteran Member
Join Date: Aug 2005
Old 08-27-2006 , 16:37   Re: A word about memory
Reply With Quote #4

Quote:
If I made any mistakes in this tutorial or this tutorial is complete crap, please PM me via AMXX.


And yes I know about the boolean thing, thats why I said it would "invalid", because you can cast them to get around them, like you did.
__________________
Quote:
Originally Posted by phorelyph View Post
your retatred
Zenith77 is offline
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 08-27-2006 , 16:49   Re: A word about memory
Reply With Quote #5

You are wrong about the sizes of bytes, nybbles, and word sizes (how much a CPU can process/store at one time).

The size of a byte is non-standardized. Many archetectures use 4 bit bytes (stores one hexidecimal value) and 6 bit bytes (makes it easier to do some operations). The nybble, which is based on the byte, is also non-standardized.

Any code relying on the byte being 8 bits isn't portable to CPU's that use 4/6 bit bytes, which a lot of smaller appliances (cell phones for instance) and such use.

On the topic of word sizes (a word is how big a register is in the CPU, and in effect, the largest peice of data that the CPU can process in one OP code), you are mistaken, though your assumption is not nessasarily a bad one. A 32-bit processor can work on more than 32-bits at a time; it just does it in 32-bit chunks. The defining quantity than, is the size of the registers, which are generally the same size as the processor, but which can be larger or smaller.

For instance, some of the game consoles like Saturn, N64, and PSX had variable word sizes, and could thus process big floating points with as much ease, though a little less speed, than if they had a bigger, more expensive processor size.

No offense or anything; the assumption that a byte is 8 bits is pretty much standard with PC's, and the other part wasn't too far off. Just technicalities really, nice job.
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
Zenith77
Veteran Member
Join Date: Aug 2005
Old 08-27-2006 , 17:20   Re: A word about memory
Reply With Quote #6

Suzuka, I didn't right this tutorial for cell phones or game consoles . Although, I really had no clue about that stuff.

Quote:
A 32-bit processor can work on more than 32-bits at a time; it just does it in 32-bit chunks.
There is no difference. Chunks/Time. Both have the same meaning in this context.

I'm not trying to argue back, just trying to clear some stuff up, since I'm like 1/4 the programmer you are :/.
__________________
Quote:
Originally Posted by phorelyph View Post
your retatred
Zenith77 is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:30.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode