Assembly Language Syntax
Assembly Language Syntax
Comments
Assembly language comment begins with a semicolon (;). It may contain
any printable character including blank. It can appear on a line by itself, like −
The fields in the square brackets are optional. A basic instruction has two
parts, the first one is the name of the instruction (or the mnemonic), which is
to be executed, and the second are the operands or the parameters of the
command.
Following are some examples of typical assembly language statements −
Memory Segments
A segmented memory model divides the system memory into groups of
independent segments referenced by pointers located in the segment registers.
Each segment is used to contain a specific type of data. One segment is used to
contain instruction codes, another segment stores the data elements, and a third
segment keeps the program stack.
In the light of the above discussion, we can specify various memory segments as –
• Data segment − It is represented by .data section and the .bss. The .data
section is used to declare the memory region, where data elements are
stored for the program. This section cannot be expanded after the data
elements are declared, and it remains static throughout the program.
The .bss section is also a static memory section that contains buffers for data to
be declared later in the program. This buffer memory is zero-filled.
Assembly - Registers
Processor operations mostly involve processing data. This data can be
stored in memory and accessed from thereon. However, reading data from and
storing data into memory slows down the processor, as it involves complicated
processes of sending the data request across the control bus and into the
memory storage unit and getting the data through the same channel.
To speed up the processor operations, the processor includes some
internal memory storage locations, called registers.
The registers store data elements for processing without having to access
the memory. A limited number of registers are built into the processor chip.
Processor Registers
There are ten 32-bit and six 16-bit processor registers in IA-32 architecture.
The registers are grouped into three categories −
The general registers are further divided into the following groups −
Data Registers
Four 32-bit data registers are used for arithmetic, logical, and other
operations. These 32-bit registers can be used in three ways −
• Lower halves of the 32-bit registers can be used as four 16-bit data registers:
AX, BX, CX and DX.
• Lower and higher halves of the above-mentioned four 16-bit registers can be
used as eight 8-bit data registers: AH, AL, BH, BL, CH, CL, DH, and DL.
• Instruction Pointer (IP) − The 16-bit IP register stores the offset address of
the next instruction to be executed. IP in association with the CS register
(as CS:IP) gives the complete address of the current instruction in the
code segment.
• Stack Pointer (SP) − The 16-bit SP register provides the offset value within
the program stack. SP in association with the SS register (SS:SP) refers to
be current position of data or address within the program stack.
• Base Pointer (BP) − The 16-bit BP register mainly helps in referencing the
parameter variables passed to a subroutine. The address in SS register is
combined with the offset in BP to get the location of the parameter. BP can
also be combined with DI and SI as base register for special addressing.
Index Registers
The 32-bit index registers, ESI and EDI, and their 16-bit rightmost portions. SI and
DI, are used for indexed addressing and sometimes used in addition and
subtraction. There are two sets of index pointers −
Control Registers
The 32-bit instruction pointer register and the 32-bit flags register combined are
considered as the control registers.
Many instructions involve comparisons and mathematical calculations and
change the status of the flags and some other conditional instructions test the
value of these status flags to take the control flow to other location.
The common flag bits are:
• Overflow Flag (OF) − It indicates the overflow of a high-order bit (leftmost
bit) of data after a signed arithmetic operation.
• Trap Flag (TF) − It allows setting the operation of the processor in single-
step mode. The DEBUG program we used sets the trap flag, so we could
step through the execution one instruction at a time.
• Sign Flag (SF) − It shows the sign of the result of an arithmetic operation.
This flag is set according to the sign of a data item following the arithmetic
operation. The sign is indicated by the high-order of leftmost bit. A positive
result clears the value of SF to 0 and negative result sets it to 1.
• Zero Flag (ZF) − It indicates the result of an arithmetic or comparison
operation. A nonzero result clears the zero flag to 0, and a zero result sets
it to 1.
• Auxiliary Carry Flag (AF) − It contains the carry from bit 3 to bit 4
following an arithmetic operation; used for specialized arithmetic. The AF
is set when a 1-byte arithmetic operation causes a carry from bit 3 into bit
4.
• Parity Flag (PF) − It indicates the total number of 1-bits in the result
obtained from an arithmetic operation. An even number of 1-bits clears
the parity flag to 0 and an odd number of 1-bits sets the parity flag to 1.
Segment Registers
Segments are specific areas defined in a program for containing data, code and
stack. There are three main segments −
• Data Segment − It contains data, constants and work areas. A 16-bit Data
Segment register or DS register stores the starting address of the data
segment.
section .text
global _start ;must be declared for linker (gcc)
section .data
msg db 'Displaying 9 stars',0xa ;a message
len equ $ - msg ;length of message
s2 times 9 db '*'
When the above code is compiled and executed, it produces the following result
−
• Store the arguments to the system call in the registers EBX, ECX, etc.
There are six registers that store the arguments of the system call used. These
are the EBX, ECX, EDX, ESI, EDI, and EBP. These registers take the consecutive
arguments, starting with the EBX register. If there are more than six arguments,
then the memory location of the first argument is stored in the EBX register.
The following code snippet shows the use of the system call sys_exit –
The following code snippet shows the use of the system call sys_write −
; Exit code
mov eax, 1
mov ebx, 0
int 80h
When the above code is compiled and executed, it produces the following result
• Register addressing
• Immediate addressing
• Memory addressing
Register Addressing
In this addressing mode, a register contains the operand. Depending upon the
instruction, the register may be the first operand, the second operand or both.
For example,
Direct-Offset Addressing
This addressing mode uses the arithmetic operators to modify an address. For
example, look at the following definitions that define tables of data −
The following operations access data from the tables in the memory into
registers –
variable.
The MOV instruction may have one of the following five forms –
The MOV instruction causes ambiguity at times. For example, look at the
statements:
It is not clear whether you want to move a byte equivalent or word equivalent of
the number 110. In such cases, it is wise to use a type specifier.
Following table shows some of the common type specifiers −
Example
The following program illustrates some of the concepts discussed above. It stores
a name 'Zara Ali' in the data section of the memory, then changes its value to
another name 'Nuha Ali' programmatically and displays both the names.
section .text
global _start ;must be declared for linker (ld)
_start: ;tell linker entry point
section .data
name db 'Zara Ali '
When the above code is compiled and executed, it produces the following result –
Assembly - Variables
NASM provides various define directives for reserving storage space for variables. The define
assembler directive is used for allocation of storage space. It can be used to reserve as well as
initialize one or more bytes.
Allocating Storage Space for Initialized Data
The syntax for storage allocation statement for initialized data is −
Where, variable-name is the identifier for each storage space. The assembler associates an
offset value for each variable name defined in the data segment.
There are five basic forms of the define directive –
When the above code is compiled and executed, it produces the following result
–
Multiple Definitions
You can have multiple data definition statements in a program. For example −
The assembler allocates contiguous memory for multiple variable
definitions. Multiple Initializations
The TIMES directive allows multiple initializations to the same value. For example,
an array named marks of size 9 can be defined and initialized to zero using the
following statement –
The TIMES directive is useful in defining arrays and tables. The following program
displays 9 asterisks on the screen –
When the above code is compiled and executed, it produces the following result
–
Assembly - Constants
There are several directives provided by NASM that define constants. We have
already used the EQU directive in previous chapters. We will particularly discuss
three directives −
For example,
You can then use this constant value in your code, like –
section .data
msg1 db 'Hello, programmers!',0xA,0xD
len1 equ $ - msg1
When the above code is compiled and executed, it produces the following result
• Register to register
• Memory to register
• Register to memory
segment .data
segment .bss
num1 resb 2
num2 resb 2
res resb 1
section .text
global _start ;must be declared for using gcc
exit:
When the above code is compiled and executed, it produces the following
result –
Syntax
The syntax for the MUL/IMUL instructions is as follows –
Multiplicand in both cases will be in an accumulator, depending upon the size of
the multiplicand and the multiplier and the generated product is also stored in
two registers depending upon the size of the operands. Following section
explains MUL instructions with three different cases –
Example
Example
The following example multiplies 3 with 2, and displays the result −
section.text
global _start ;must be declared for using gcc
mov al,'3'
sub al, '0'
mov [res], al
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov ecx,res
mov edx, 1
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
section .data
msg db "The result is:", 0xA,0xD
len equ $- msg
segment .bss
res resb 1
When the above code is compiled and executed, it produces the following result –
Syntax
The format for the DIV/IDIV instruction –
The dividend is in an accumulator. Both the instructions can work with 8-bit, 16-bit or 32- bit
operands. The operation affects all six status flags. Following section explains three cases of
division with different operand size –
Example
The following example divides 8 with 2. The dividend 8 is stored in the 16-bit AX register
and the divisor 2 is stored in the 8-bit BL register.
section.text
global _start ;must be declared for using gcc
mov [res], ax
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov ecx,res
mov edx, 1
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
section .data
msg db "The result is:", 0xA,0xD
len equ $- msg
segment .bss
res resb 1
When the above code is compiled and executed, it produces the following result –
The processor instruction set provides the instructions AND, OR, XOR, TEST, and NOT Boolean
logic, which tests, sets, and clears the bits according to the need of the program. The format
for these instructions −
The first operand in all the cases could be either in register or in memory. The second operand could
be either in register/memory or an immediate (constant) value. However, memory-to-memory
operations are not possible. These instructions compare or match bits of the operands and set the
CF, OF, PF, SF and ZF flags.
The AND instruction is used for supporting logical expressions by performing bitwise AND operation.
The bitwise AND operation returns 1, if the matching bits from both the operands are 1, otherwise it
returns 0. For example −
The AND operation can be used for clearing one or more bits. For example, say the BL register
contains 0011 1010. If you need to clear the high-order bits to zero, you AND it with 0FH.
Let's take up another example. If you want to check whether a given number is odd or even, a simple
test would be to check the least significant bit of the number. If this is 1, the number is odd, else the
number is even.
Example:
section .text
global _start ;must be declared for using gcc
evnn:
outprog:
section .data
even_msg db 'Even Number!' ;message showing even number
len1 equ $ - even_msg
Similarly to clear the entire register you can AND it with 00H.
The OR Instruction
The OR instruction is used for supporting logical expression by performing bitwise OR operation. The bitwise OR
operator returns 1, if the matching bits from either or both operands are one. It returns 0, if both the bits are zero.
For example,
The OR operation can be used for setting one or more bits. For example, let us assume the AL register contains 0011
1010, you need to set the four low-order bits, you can OR it with a value 0000 1111, i.e., FH.
Example
The following example demonstrates the OR instruction. Let us store the value 5 and 3 in the AL and the BL registers,
respectively, then the instruction,
mov [result], al
mov eax, 4
mov ebx, 1
mov ecx, result
mov edx, 1
int 0x80
outprog:
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .bss
result resb 1
When the above code is compiled and executed, it produces the following result –
The XOR instruction implements the bitwise XOR operation. The XOR operation sets the resultant bit to 1, if and only
if the bits from the operands are different. If the bits from the operands are same (both 0 or both 1), the resultant
bit is cleared to 0.
For example,
XORing an operand with itself changes the operand to 0. This is used to clear a register.
The TEST instruction works same as the AND operation, but unlike AND instruction, it does not change the first
operand. So, if we need to check whether a number in a register is even or odd, we can also do this using the TEST
instruction without changing the original number.
The NOT instruction implements the bitwise NOT operation. NOT operation reverses the bits in an operand. The
operand could be either in a register or in the memory.
For example,
Assembly – Conditions
Conditional execution in assembly language is accomplished by several looping and branching instructions. These
instructions can change the flow of control in a program. Conditional execution is observed in two scenarios −
CMP Instruction
The CMP instruction compares two operands. It is generally used in conditional execution. This instruction basically
subtracts one operand from the other for comparing whether the operands are equal or not. It does not disturb the
destination or source operands. It is used along with the conditional jump instruction for decision making.
Syntax
CMP compares two numeric data fields. The destination operand could be either in register or in memory. The
source operand could be a constant (immediate) data, register or memory.
Example:
CMP is often used for comparing whether a counter value has reached the number of times a loop needs to be run.
Consider the following typical condition –
Unconditional Jump
As mentioned earlier, this is performed by the JMP instruction. Conditional execution often involves a transfer of
control to the address of an instruction that does not follow the currently executing instruction. Transfer of control
may be forward, to execute a new set of instructions or backward, to re-execute the same steps.
Syntax
The JMP instruction provides a label name where the flow of control is transferred immediately. The syntax of the
JMP instruction is −
Example:
Conditional Jump
If some specified condition is satisfied in conditional jump, the control flow is transferred to a target instruction.
There are numerous conditional jump instructions depending upon the condition and data.
Following are the conditional jump instructions used on signed data used for arithmetic operations −
Following are the conditional jump instructions used on unsigned data used for logical operations –
The following conditional jump instructions have special uses and check the value of flags –
Example:
The following program displays the largest of three variables. The variables are double-digit variables. The three
variables num1, num2 and num3 have values 47, 22 and 31, respectively −
section .text
global _start ;must be declared for using gcc
check_third_num:
_exit:
mov ecx,largest
mov edx, 2
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax, 1
int 80h
section .data
segment .bss
largest resb 2
When the above code is compiled and executed, it produces the following result –