You are on page 1of 13

 What is ASSEMBLY LANGUAGE ?

An assembly language is a low-level programming language for microprocessors


and other programmable devices. It is not just a single language, but rather a group
of languages. An assembly language implements a symbolic representation of the
machine code needed to program a given CPU architecture. Assembly language is
also known as assembly code. The term is often also used synonymously with
2GL. Each assembly language is specific to a particular computer architecture and
operating system. In contrast, most high-level programming languages are
generally portable across multiple architectures but require require interpreting or
compiling. Assembly language may also be called symbolic machine code.
Assembly language usually has one statement per machine instruction, but
assembler directives, macros and symbolic labels of program and memory
locations are often also supported. Assembly code is converted into executable
machine code by a utility program referred to as an assembler. The conversion
process is referred to as assembly, or assembling the source code.

 Advantages of Assembly Language

 Having an understanding of assembly language makes one aware of −


 How programs interface with OS, processor, and BIOS;
 How data is represented in memory and other external devices;
 How the processor accesses and executes instruction;
 How instructions access and process data;
 How a program accesses external devices.
 It requires less memory and execution time;
 It allows hardware-specific complex jobs in an easier way;
 It is suitable for time-critical jobs;
 It is most suitable for writing interrupt service routines and other memory
resident programs.

1.
 Syntax of Assembly Language Statements:-

Assembly language uses a mnemonic to represent each low-level machine


instruction or opcode, typically also each architectural register, flag, etc. Many
operations require one or more operands in order to form a complete instruction.
Most assemblers permit named constants, registers, and labels for program and
memory locations, and can calculate expressions for operands. Thus, the
programmers are freed from tedious repetitive calculations and assembler
programs are much more readable than machine code. Depending on the
architecture, these elements may also be combined for specific instructions or
addressing modes using offsets or other data as well as fixed addresses. Many
assemblers offer additional mechanisms to facilitate program development, to
control the assembly process, and to aid debugging. Assembly language statements
are entered one statement per line. Each statement follows the following format –

[label] mnemonic [operands] [;comment]

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.

Below are some examples of instructions supported by x86 processors.

 MOV – move data from one location to another


 ADD – add two values
 SUB – subtract a value from another value
 PUSH – push data onto a stack
 POP – pop data from a stack
 JMP – jump to another location
 INT – interrupt a process
The following assembly language can be used to add the numbers 3 and 4:

ove ax, 3 – loads 3 into the register “eax”


mov ebx, 4 – loads 4 into the register “ebx”
add eax, ebx, ecx – adds “eax” and “ebx” and stores the result (7) in “ecx”

Writing assembly language is a tedious process since each operation must be


performed at a very basic level.
2.
 Allocating Storage Space for Initialized Data
The syntax for storage allocation statement for initialized data is −
[variable-name] define-directive initial-value [,initial-value]...
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 –
Directive Purpose Storage Space
DB Define Byte allocates 1 byte
DW Define Word allocates 2 bytes
DD Define Doubleword allocates 4 bytes
DQ Define Quadword allocates 8 bytes
DT Define Ten Bytes allocates 10 bytes

Please note that −


 Each byte of character is stored as its ASCII value in hexadecimal.
 Each decimal value is automatically converted to its 16-bit binary
equivalent and stored as a hexadecimal number.
 Processor uses the little-endian byte ordering.
 Negative numbers are converted to its 2's complement
representation.
 Short and long floating-point numbers are represented using 32 or
64 bits, respectively.

3.
 Three Categories of Instructions:
The instruction set is a collection of instructions each representing a CPU
operation. In assembly language, the instructions are represented by the
mnimonics and the operands (or their addresses) involved in the operation.

1. Data manipulation:-
 Arithmetic manipulation:
add, sub, mult, div, etc.
 Logic and bit manipulation:
and, or, nor, xor, etc.
 Shift and rotation (to right or left):
sll, srl, sra, rol, ror, etc.
Note: Instruction srl (shift right logical) shifts a 0 into the vacated bit (the sign bit),
while instruction sra (shift right arithmetic) repeats the sign bit (sign extension for
signed 2's complement).
2. Data transfer:-
 transfer data between MM and CPU:
lw (load word), la (load address), lb (load byte),sw (store word), sb
(store byte), etc.
 transfer data between registers in RF:
move, mfhi, mflo, mthi, mtlo,
3. Iteration Control Instruction:-
 branch to instruction other than the one following the current one
conditionally or unconditionally (based on comparison between
two operands or between one operand and zero):
b, beq, bne, bgt, blt, bge, ble, beqz, bnez, bgez, bgtz,
 jump to different segments of the program (functions, subroutines,
etc.) j, jal, jalr, jr, etc.
4. Interrupt Instructions
5. Logical Instructions
6. Stack Instructions
7. I/O Instructions

4.
I. Data Transfer Instructions:-
These instructions are used to transfer the data from the source operand
to the destination operand. Following are the list of instructions under
this group –

 Instruction to transfer a word :-

 MOV − Used to copy the byte or word from the provided source to
the provided destination.
 PPUSH − Used to put a word at the top of the stack.
 POP − Used to get a word from the top of the stack to the provided
location.
 PUSHA − Used to put all the registers into the stack.
 POPA − Used to get words from the stack to all registers.
 XCHG − Used to exchange the data from two locations.
 XLAT − Used to translate a byte in AL using a table in the memory.

 Instructions for input and output port transfer:-

 IN − Used to read a byte or word from the provided port to the accumulator.
 OUT − Used to send out a byte or word from the accumulator to the
provided port.

 Instructions to transfer the address :-

 LEA − Used to load the address of operand into the provided register.
 LDS − Used to load DS register and other provided register from the
memory
 LES − Used to load ES register and other provided register from the
memory.

5.
 Instructions to transfer flag registers :-
 LAHF − Used to load AH with the low byte of the flag register.
 SAHF − Used to store AH register to low byte of the flag register.
 PUSHF − Used to copy the flag register at the top of the stack.
 POPF − Used to copy a word at the top of the stack to the flag register.

II. Data manipulation Instruction:-


 Arithmetic Instructions:-
These instructions are used to perform arithmetic operations like addition,
subtraction, multiplication, division, etc. Following is the list of instructions under
this group –

i. Instructions to perform addition


 ADD − Used to add the provided byte to byte/word to word.
 ADC − Used to add with carry.

 The INC Instruction


The INC instruction is used for incrementing an operand by one. It works on a
single operand that can be either in a register or in memory. INC − Used to
increment the provided byte/word by 1. The INC instruction has the following
syntax −
INC destination

The operand destination could be an 8-bit, 16-bit or 32-bit operand.


Example
 INC EBX ; Increments 32-bit register
 INC DL ; Increments 8-bit register
 INC [count] ; Increments the count variable 7 | P a g e

 The DEC Instruction


The DEC instruction is used for decrementing an operand by one. It works on a
single operand that can be either in a register or in memory. The DEC instruction
has the following syntax −
DEC destination
The operand destination could be an 8-bit, 16-bit or 32-bit operand.
6.
Example
 segment .data
 count dw 0
 value db 15
 segment .text
 inc [count]
 dec [value]
 mov ebx, count
 inc word [ebx]
 mov esi, value
 dec byte [esi]
 AAA – Used to adjust ASCII after addition.
 DAA – Used to adjust the decimal after the addition/subtraction
operation.

 Instructions to perform subtraction:-

 SUB – Used to subtract the byte from byte/word from word.


 SBB – Used to perform subtraction with borrow.
 DEC – Used to decrement the provided byte/word by 1.
 NPG – Used to negate each bit of the provided byte/word and add ½’s
complement.
 CMP – Used to compare 2 provided byte/word.
 AAS – Used to adjust ASCII codes after subtraction.
 DAS – Used to adjust decimal after subtraction.

 Instruction to perform multiplication :-

 MUL – Used to multiply unsigned byte by byte/word by word.


 IMUL – Used to multiply signed byte by byte/word by word.
 AAM – Used to adjust ASCII codes after multiplication.

7.
 Instructions to perform division:-

 DIV − Used to divide the unsigned word by byte or unsigned double word
by word.
 IDIV − Used to divide the signed word by byte or signed double word by
word.
 AAD − Used to adjust ASCII codes after division.
 CBW − Used to fill the upper byte of the word with the copies of sign bit of
the lower byte.
 CWD − Used to fill the upper word of the double word with the sign bit of
the lower word.

 Bit Manipulation Instructions

These instructions are used to perform operations where data bits are involved, i.e.
operations like logical, shift, etc. Following is the list of instructions under this
group −
 Instructions to perform logical operation:-
 NOT − Used to invert each bit of a byte or word.
 AND − Used for adding each bit in a byte/word with the corresponding bit
in another byte/word.
 OR − Used to multiply each bit in a byte/word with the corresponding bit in
another byte/word.
 XOR − Used to perform Exclusive-OR operation over each bit in a
byte/word with the corresponding bit in another byte/word.
 TEST − Used to add operands to update flags, without affecting operands.

 Instructions to perform shift operations :-

 SHL/SAL − Used to shift bits of a byte/word towards left and put zero(S) in
LSBs.
 SHR − Used to shift bits of a byte/word towards the right and put zero(S) in
MSBs.
 SAR − Used to shift bits of a byte/word towards the right and copy the old
MSB into the new MSB.
8.
 Instructions to perform rotate operations :-
 ROL − Used to rotate bits of byte/word towards the left, i.e. MSB to LSB
and to Carry Flag [CF].
 ROR − Used to rotate bits of byte/word towards the right, i.e. LSB to MSB
and to Carry Flag [CF].
 RCR − Used to rotate bits of byte/word towards the right, i.e. LSB to CF
and CF to MSB.
 RCL − Used to rotate bits of byte/word towards the left, i.e. MSB to CF and
CF to LSB.

III. Iteration Control Instructions


These instructions are used to execute the given instructions for number of times.
Following is the list of instructions under this group −
 LOOP − Used to loop a group of instructions until the condition satisfies,
i.e., CX = 0
 LOOPE/LOOPZ − Used to loop a group of instructions till it satisfies ZF =
1 & CX = 0
 LOOPNE/LOOPNZ − Used to loop a group of instructions till it satisfies
ZF = 0 & CX = 0
 JCXZ − Used to jump to the provided address if CX = 0

IV. Interrupt Instructions


These instructions are used to call the interrupt during program execution.
 INT − Used to interrupt the program during execution and calling service
specified.
 INTO − Used to interrupt the program during execution if OF = 1
 IRET − Used to return from interrupt service to the main program

9.
V. Logical Instruction
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

Sr.No. Instruction Format


1 AND AND operand1,
operand2
2 OR OR operand1,
operand2
3 XOR XOR operand1,
operand2
4 TEST TEST operand1,
operand2
5 NOT NOT operand1

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:-

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 −
Operand1: 0101
Operand2: 0011
----------------------------
After AND -> Operand1: 0001
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.
AND BL, 0FH ; This sets BL to 0000 1010
10.
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.
Assuming the number is in AL register, we can write –
AND AL, 01H ; ANDing with 0000 0001
JZ EVEN_NUMBER

 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,
Operand1: 0101
Operand2: 0011
----------------------------
After OR -> Operand1: 0111
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.
OR BL, 0FH ; This sets BL to 0011 1111 14 | P a g e

 The XOR Instruction:-

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,
Operand1: 0101
Operand2: 0011
----------------------------
After XOR -> Operand1: 0110
XORing an operand with itself changes the operand to 0. This is used to clear a
register.
XOR EAX, EAX

11.
 The TEST Instruction:-

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.
TEST AL, 01H
JZ EVEN_NUMBER

 The NOT Instruction:-


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,
Operand1: 0101 0011
After NOT -> Operand1: 1010 1100

VI. Stack Instructions:-


 PUSH:- PUSH two bytes of data onto the stack
 POP:- POP two bytes of data off the stack.

 Program to compute 16 bit addition of the data.

DATA SEGMENT      ;INITIALIZE DATA SEGMENT


NUM1 DW 4537H     ;INITIALIZE NUM1 ANY DATA
NUM2 DW 2222H     ;INITIALIZE NUM2 ANY DATA
RESULT DW ?            ;INITIALIZE RESULT
DATA ENDS                ;END OF DATA SEGMENT

CODE SEGMENT       ;INITIALIZE CODE SEGMENT


START:                           ;START THE CODE
ASSUME CS:CODE , DS:DATA            ;ASSUMPTION OF CODE AND
DATA
MOV AX,DATA           ;MOV DATA INTO ACCUMULATOR AX
MOV DS,AX                  ;MOV DATA FROM ACCUMULATOR INTO

12.
DATA SEGMENT REGISTER
MOV AX,NUM1          ;MOV NUM1 INTO ACCUMULATOR AX
REGISTER
MOV BX,NUM2          ;MOV NUM2 INTO BASE BX REGISTER
ADD AX,BX                  ;ADDING THE CONTENTS OF AX AND BX.
RESULT IS STILL IN ACCUMULATOR
MOV RESULT,AX     ;MOVE THE RESULT FROM AX INTO RESULT
VARIABLE
MOV AH,4CH              ;END OF THE CODE
INT 21H                         ;END OF THE CODE
CODE ENDS                 ;END OF CODE SEGMENT
END START                ;END OF START
; INPUT :
; NUM1 – 4537H
; NUM2 – 2222H

; OUTPUT :
; RESULT – 6759H

---------------------************----------------------

13.

You might also like