A Program
There is a book entitled Algorithms + Data Structures = Programs by Nichols Wirth.
Essentially, that describes every program in every language. The code section implements
the algorithm, and the data sections implement the data structures.
Sections (Segments) of a program
Some of the sections of a program include:
.Model | The only thing we will use is FLAT, because it gives us all 4GB of memory |
.Stack | This is the amount of stack space you want to reserve. 1KB is the default |
.Data | Actually, we can have .DATA, .DATA?, .CONST, .FARDATA, and .FARDATA?
You can get away with using only the .DATA |
.Code | This is your algoritm! |
You can have more than one .DATA and one .CODE segment. The linker will get it figured out for you.
Instructions/Code
An instruction or a line of code has up to four parts:
- label
- mnemonic
- operand(s)
- comment
All four are optional, depending on what you are doing. A line of code can
consist of just a label, just a mnemonic, or just a comment. The operand(s)
can not be present without a mnemonic.
Label
Labels are identifiers. They are used to identify data and variables or locations in the code.
They allow us to refer to locations and data in a symbolic or meaningful way. That is why you
should use care in selecting your labels. It also helps document your code.
Mnemonics
"Mnemonic" is defined by Merriam-Webster OnLine Search as "assisting or intended to assist memory".
These are normally an abbreviation of the instruction in a form that is suppose to help you remember
what the instruction is and does. MOV is for "Move" or transfer data from one point to another.
JMP is to jump to location in the program. Some people (including authors of books) incorrectly
call them "opcodes", but opcodes are numeric version that only the computer really understands.
The MOV instruction can be a number of different opcodes, depending on the addressing mode, etc.
Operands
Operands are the extra information that the instruction needs to do that instruction. Of course,
not all instructions use operands. Some instructions have one operand, others have two operands.
Operands have no meaning without an instruction mnemonic, and
can not exist in isolation.
When an instruction has two operands, they are in the format of:
destination, source
although, the first one can also be a source as well as a destination. The destination and the source
must be the same size!
If you want to put a copy of the data in the EAX register into the EBX, it is:
mov EBX, EAX
EAX is the source and EBX is the destination.
If I want to add the values in the EAX and EBX register together and store the results in
EAX, it would be in the form of:
add EAX, EBX
NOTE: The destination must be one of the two registers holding the data. The Intel
chip does not allow you to put the result into a third register. You would have to do the
addition and then have another instruction to move the sum to the third register.
Comments
The comments do not affect the performance of the program. They exist to communicate.
This is where you tell the reader what you are doing. If others cannot understand your
code, your program is worthless. Comments should add value to the code, not
just repeat verbatim what the instruction does!
Bad Comment
inc EAX ;increment the EAX register
Good Comment
inc EAX ;increment the number of students processed
Data
Declaring data can be interesting. You must keep track of the constraints of your data.
You should always know the minimum and maximum values a data item can have.
Today, the tendency is to make everything a double word, because it is actually faster.
Intrinsic Data Types
These are some of the built-in data types:
Size | Name1 | Name2 |
8-bit | BYTE | db |
sbyte | |
16-bit | WORD | dw |
sword | |
32-bit | DWORD | dd |
sdword | |
64-bit | QWORD | dq |
Notes:
- This shows a TYPE, but can be used as an initializer. TYPE must be used when the instruction could
apply to any time, such as INC DWORD [nrStudents]. Without the TYPE, this could refer to an 8-bit, 16-bit,
or 32-bit memory location.
- This is an initializer, used in the .DATA and .DATA? segments