This page is going to be about the fundamentals that you have to understand before you can make any sense out of assembly. Most of this stuff you'll learn if you learn to program in C. If this is old or boring stuff to you, feel free to skip this section entirely.
The topics here are going to be a short overview of each section. If you want a more complete explanation, you should find an actual reference, or look it up on the Internet. This is only meant to be a quick and dirty primer.
To work in assembly, you have to be able to read hexadecimal fairly comfortably. Converting to decimal in your mind isn't necessary, but being able to do some simple arithmetic is.
Hex can be denoted in a number of ways, but the two most common are:
- Prefixed with a 0x, eg. 0x1ef7
- Postfixed with a h, eg. 1ef7h
The characters 0 - f represent the decimal numbers 0 - 15:
- 0 = 0
- 1 = 1
- 9 = 9
- a = 10
- b = 11
- c = 12
- d = 13
- e = 14
- f = 15
To convert from hex to decimal, multiply each digit, starting with the right-most, with 160, 161, 162, etc. So in the example of 0x1ef7, the conversion is this:
- (7 * 160) + (f * 161) + (e * 162) + (1 * 163)
- = (7 * 160) + (15 * 161) + (14 * 162) + (1 * 163)
- = (7 * 1) + (15 * 16) + (14 * 256) + (1 * 4096)
- = 7 + 240 + 3584 + 4096
- = 7927
It isn't necessary to do that constantly, that's why we have calculators. But you should be fairly familiar with the numbers 00 - FF (0 - 255), they will come up often and you will spend a lot of time looking them up.
A datatype basically refers to how digits in hex are partitioned off and divided into numbers. Datatypes are typically measures by two factors: the number of bits (or bytes), and whether or not negative numbers are allowed.
The number of bits (or bytes) refers to the length of the number. An 8-bit (or 1-byte) number is made up of two hexadecimal digits. For example, 0x03, 0x34, and 0xFF are all 8-bit, while 0x1234, 0x0001, and 0xFFFF are 16-bit.
The signed or unsigned property refers to whether or not the number can have negative values. If it can, then the maximum number is half of what it could have, with the other half being negatives. The way sign is determined is by looking at the very first bit. If the first bit is a 1, or the first hex digit is 8 - F, then it's negative and the rest of the number, inverted plus one, is used for the magnitude.
For example (use a calculator to convert to binary):
- 0x10 in binary ix 00010000, so it's positive 16
- 0xFF in binary is 11111111, so it's negative. The rest of the number is the 7-bits, 1111111, inverted to 0000000, plus one is 0000001, or -1 in decimal.
- 0x80 in binary is 10000000, so it's negative. The rest of the number is the 7-bits, 0000000, inverted to 1111111, plus one is 10000000, or -128 in decimal.
- 0x7F in binary is 01111111, so it's positive 127.
Although different data lengths are called different things, here are some common ones by their usual name:
- 8-bit (1 byte) = char (or BYTE)
- In hex, can be 0x00 to 0xFF
- Signed: ranges from -128 to 127
- Unsigned: ranges from 0 to 255
- 16-bit (2 bytes) = short int (often referred to as a WORD)
- In hex, can be 0x0000 to 0xFFFF
- Signed: ranges from -32768 to 32767
- Unsigned: ranges from 0 to 65535
- 32-bit (4 bytes) = long int (often referred to as a DWORD or double-WORD)
- In hex, can be 0x00000000 to 0xFFFFFFFF
- Signed: ranges from -2147483648 - 2147483647
- Unsigned: ranges from 0 - 4294967295
- 64-bit (8 bytes) = long long (often referred to as a QWORD or quad-WORD)
- In hex, can be from 0x0000000000000000 - 0xFFFFFFFFFFFFFFFF
- Signed: -9223372036854775808 - 9223372036854775807
- Unsigned: 0 - 18446744073709551615
Feel free to edit this section and post questions, I'll do my best to answer them. But you may need to contact me to let me know that a question exists.