The Netwide Assembler
The Netwide Assembler
The Netwide Assembler
80x86 assembler.
Supports Linux file formats.
: Byte structure
7 6 5 4 3 2 1 0
:Example
You want to store 0x1AB3 (hex number( in the memory.
This number has two bytes: 1A and B3.
: It would be stored this way B3 0
1A 1 bytes of
memory
memory block 2
.Note: when read a stored data from the memory, it comes in the source order
:Registers
Floating-point registers: ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7. 5
.Note: the registers above are the basic. There exist more registers
:Lets zoom in
AH AL
mov ah, 0x13 11001000 00000000
AH AL
mov ax, 0x13 00000000 11001000
:Status Flags
(.Flag is a bit (of the Flags Register.
The status flags provide some information about the result of.
.the last (usually arithmetic) instruction that was executed
This information can be used by conditional instructions (such a
JUPMcc and CMOVcc) as well as by some of the other
(.instructions (such as ADC
SAHF: set the low byte of the flags word according to the .2
.contents of the AH register
SALC: set AL to zero if the carry flag is clear, or to 0xFF if. 3
.it is set
.STC: sets the carry flag. 4
.CLC: clears the carry flag. 5
Note: this is not a complete set of the flag instructions. You can find more in the
.NASM tutorial
:Basic assembly instructions
Each NASM standard source line contains a combination of the
:4 fields
label: (pseudo) instruction operands ;
comment
Either required or forbidden
optional fields
by an instruction
:Notes
backslash (\) uses as the line continuation character: if a line ends with. 1
backslash, the next line is considered to be a part of the backslash-ended line.
2. no restrictions on white space within a line.
.3. a colon after a label is optional
:Examples
mov ax, 2 ; moves constant 2 to the register ax. 1
2. buffer: resb 64 ; reserves 64 bytes
:Move instructions
B.4.156: MOV – move data
mov r/m8,reg8
(copies content of 8-bit register (source) to 8-bit register or 8-bit memory
unit (destination) )
mov reg32,imm32
(copies content of 32-bit immediate (constant) to 32-bit register)
* for all the possible variants of operands look at NASM manual, B.4.156
In all forms of the MOV instruction, the two operands are the same size-
Examples:
mov EAX, 0x2334AAFF
mov word [buffer], ax
* Note: NASM don’t remember the types of variables you declare.
Whereas MASM will remember, on seeing var dw 0, that you declared var as a
word-size variable, and will then be able to fill in the ambiguity in the size of the
instruction mov var,2, NASM will deliberately remember nothing about the symbol
.var except where it begins, and so you must explicitly code mov word [var],2
(:Move instructions (2
B.4.181 MOVSX, MOVZX: move data with sign or zero
extend
movsx reg16,r/m8
(sign-extends its source (second) operand to the length of its
destination (first) operand, and copies the result into the destination
(operand
movzx reg32,r/m8
(does the same, but zero-extends rather than sign-extending)
* for all the possible variants of operands look at NASM manual, B.4.181
1…111 0…100
Examples:
movsx EAX, AX 0…000
(if AX has 10…0b value, EAX would have 0…100
( value
Examples:
add AX, BX
Examples:
(add AX, BX (AX gets a value of AX+BX+CF
(:Basic arithmetical instructions (2
Examples:
sub AX, BX
Examples:
(sbb AX, BX (AX gets a value of AX-BX-CF
(:Basic arithmetical instructions (3
Examples:
inc AX
B.4.58 DEC: decrement integer
dec reg16
((subtracts 1 from its operand
does not affect the carry flag; affects all the other flags according to*
the result
for all the possible variants of operands look at NASM manual, B.4.58*
Examples:
[dec byte [buffer
:Basic logical instructions
Examples:
((neg AL (if AL = (11111110), it becomes (00000010
Example:
((or AL, BL (if AL = (11111100), BL= (00000010) => AL would be (11111110
Example:
((and AL, BL (if AL = (11111100), BL= (00000010) => AL would be (11111100
:Compare instruction
B.4.24 CMP: compare integers
cmp r/m32,imm8
(performs a ‘mental’ subtraction of its second operand from its first
operand, and affects the flags as if the subtraction had taken place,
(but does not store the result of the subtraction anywhere
for all the possible variants of operands look at NASM manual, B.4.24*
Example:
cmp AL, BL (if AL = (11111100), BL= (00000010) => ZF would be 1)
( (if AL = (11111100), BL= (11111100) => ZF would be 0
(:Labels definition (basic
Each instruction of the code has its offset (address from the.
(.beginning of the address space
If we want to refer to the specific instruction in the code, we.
should mark it with a label:
my_loop1:
add ax, ax
….
label can be with or without colon-
- an instruction that follows it can be at the same or the next line
- a code can’t contain two different non-local (as above) labels
with the same name
:Loop definition
B.4.142 LOOP, LOOPE, LOOPZ, LOOPNE, LOOPNZ: loop with
counter
for all the possible variants of operands look at NASM manual, B.4.142*
Example:
mov ax, 1
mov cx, 3
my_ loop:
add ax, ax
loop my_ loop, cx
Note: counter register can be either CX or ECX - if one is not specified explicitly,
.the BITS setting dictates which is used
LOOPE (or its synonym LOOPZ( adds the additional condition that it only jumps if the
counter is nonzero and the zero flag is set. Similarly, LOOPNE (and LOOPNZ( jumps only
.if the counter is nonzero and the zero flag is clear