NASM, or the Netwide Assembler, is the assembler that will be used to convert your assembly language code to binary executables. Below is a description of a few NASM features you will find useful. For more detailed information on these or other NASM features see the NASM Manual (852 KB, requires Acrobat Reader; use "acroread" in UNIX and Linux).
label: instruction operands ; commentFor example you may have the following lines in a piece of assembly:
MyFunction: add ax, bx ; add bx to axAs this example shows, labels do not have to be typed on the same line as an instruction. The address of the next instruction will be assumed for the label. Also, with NASM, only the labels are case sensitive.
decimal: 100 hexadecimal: 0a2h (The 0 is needed before numbers beginning with letter) hexadecimal: 0xa2 octal: 777q binary: 10010011b
db value ; Allocate a byte sized value dw value ; Allocate a word sized value dd value ; Allocate a dword sized valueFor example, to allocate a word sized value initialized to the value 3 that can be referred to by the name MyValue, one could use the following lines of assembly:
MyValue: dw 3This value could then be accessed with instructions like:
mov ax, [MyValue] ; Load MyValue into ax mov [MyValue], 2 ; Store 2 into MyValueThe times pseudo-instruction causes the assembler to repeat an instruction or pseudo-instruction. Its syntax is:
times numRep instruction operandsFor example, to declare 20 bytes of data, initialized to 0, you could use the following line of assembly:
times 20 db 0Strings can be declared in several ways. The following are valid NASM examples:
db "abcd",0 ; Same as "abcd" in C (with NULL termination) db 'a',"b",'cd',0 ; Same as previous db '"quotes"',"and 'apostrophes'.",0 ; The string: "quotes" and 'apostrophes'.
org 0x100The align directive allows programmers to align their code to word, dword, or larger boundaries in memory. To align to a word boundary, the following line of assembly could be used:
align 2 ; Align to nearest 2-byte boundaryThis will cause an unused byte to be inserted if the address of the next instruction or data would have been odd. The parameter given to align must be a power of 2. Code and data alignment are important in ensuring memory performance.