0% found this document useful (0 votes)
4 views12 pages

11.Assembly Programming Language

Assembly language is a low-level programming language that uses symbolic code to represent machine language instructions, making it more understandable for software development. It offers advantages such as reduced memory usage and execution time, and is suitable for hardware-specific tasks and time-critical jobs. The document also covers the structure of assembly programs, memory segments, registers, and provides examples of basic assembly language operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

11.Assembly Programming Language

Assembly language is a low-level programming language that uses symbolic code to represent machine language instructions, making it more understandable for software development. It offers advantages such as reduced memory usage and execution time, and is suitable for hardware-specific tasks and time-critical jobs. The document also covers the structure of assembly programs, memory segments, registers, and provides examples of basic assembly language operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Assembly Programming Language

What is Assembly Language?


Each personal computer has a microprocessor that manages the computer's arithmetical, logical,
and control activities. Each family of processors has its own set of instructions for handling
various operations such as getting input from keyboard, displaying information on screen and
performing various other jobs. These set of instructions are called 'machine language
instructions'. A processor understands only machine language instructions, which are strings of
1's and 0's. However, machine language is too obscure and complex for using in software
development. So, the low-level assembly language is designed for a specific family of
processors that represents various instructions in symbolic code and a more understandable
form.

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.
Other advantages of using assembly language are −
 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.

Addressing Data in Memory


The process through which the processor controls the execution of instructions is referred as
the fetch-decode-execute cycle or the execution cycle. It consists of three continuous steps −

 Fetching the instruction from memory


 Decoding or identifying the instruction
 Executing the instruction
The processor may access one or more bytes of memory at a time. Let us consider a
hexadecimal number 0725H. This number will require two bytes of memory. The high-order
byte or most significant byte is 07 and the low-order byte is 25.
The processor stores data in reverse-byte sequence, i.e., a low-order byte is stored in a low
memory address and a high-order byte in high memory address. So, if the processor brings the
value 0725H from register to memory, it will transfer 25 first to the lower memory address and
07 to the next memory address.

x: memory address
When the processor gets the numeric data from memory to register, it again reverses the bytes.
There are two kinds of memory addresses −
 Absolute address - a direct reference of specific location.
 Segment address (or offset) - starting address of a memory segment with the offset value.

Assembly - Basic Syntax

Assembly Language Statements


Assembly language programs consist of three types of statements −

 Executable instructions or instructions,


 Assembler directives or pseudo-ops, and
 Macros.
The executable instructions or simply instructions tell the processor what to do. Each
instruction consists of an operation code (opcode). Each executable instruction generates one
machine language instruction.
The assembler directives or pseudo-ops tell the assembler about the various aspects of the
assembly process. These are non-executable and do not generate machine language instructions.
Macros are basically a text substitution mechanism.

Syntax of Assembly Language Statements


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.
include 'emu8086.INC'
.stack 100h
.model small
.data
.code
main proc

main endp
end main

Following are some examples of typical assembly language statements −


INC COUNT ; Increment the memory variable COUNT

MOV TOTAL, 48 ; Transfer the value 48 in the memory variable TOTAL

ADD AH, BH ; Add the content of the BH register into the AH register

AND MASK1, 128 ; Perform AND operation on the variable MASK1 and 128

ADD MARKS, 10 ; Add 10 to the variable MARKS


MOV AL, 10 ; Transfer the value 10 to the AL register
Assembly - Memory Segments
There are three sections of an assembly program. These sections represent various memory
segments as well.

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.
 Code segment − It is represented by .text section. This defines an area in memory that
stores the instruction codes. This is also a fixed area.
 Stack − This segment contains data values passed to functions and procedures within the
program.

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 −

 General registers,
 Control registers, and
 Segment registers.
The general registers are further divided into the following groups −

 Data registers,
 Pointer registers, and
 Index registers.

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 −
 As complete 32-bit data registers: EAX, EBX, ECX, EDX.
 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.

Some of these data registers have specific use in arithmetical operations.


AX is the primary accumulator; it is used in input/output and most arithmetic instructions.
For example, in multiplication operation, one operand is stored in EAX or AX or AL register
according to the size of the operand.
BX is known as the base register, as it could be used in indexed addressing.
CX is known as the count register, as the ECX, CX registers store the loop count in iterative
operations.
DX is known as the data register. It is also used in input/output operations. It is also used with
AX register along with DX for multiply and divide operations involving large values.

Pointer Registers
The pointer registers are 32-bit EIP, ESP, and EBP registers and corresponding 16-bit right
portions IP, SP, and BP. There are three categories of pointer registers −
 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 −
 Source Index (SI) − It is used as source index for string operations.
 Destination Index (DI) − It is used as destination index for string operations.

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)
 Direction Flag (DF)
 Interrupt Flag (IF)
 Trap Flag (TF)
 Sign Flag (SF)
 Zero Flag (ZF)
 Auxiliary Carry Flag (AF)
 Parity Flag (PF)
 Carry Flag (CF)

Segment Registers
Segments are specific areas defined in a program for containing data, code and stack. There are
three main segments −
 Code Segment − It contains all the instructions to be executed. A 16-bit Code Segment
register or CS register stores the starting address of the code segment.
 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.
 Stack Segment − It contains data and return addresses of procedures or subroutines. It is
implemented as a 'stack' data structure. The Stack Segment register or SS register stores
the starting address of the stack.
Apart from the DS, CS and SS registers, there are other extra segment registers - ES (extra
segment), FS and GS, which provide additional segments for storing data.

Examples of different Programs:


ADD Two Numbers
include 'emu8086.INC'
.stack 100h
.model small
.data
.code
main proc
print 'enter first number:'
mov ah,01h
int 21h
sub al,48
mov bl,al

mov dl,10
mov ah,02h
int 21h

mov dl,13
mov ah,02h
int 21h

print 'enter second number:'


mov ah,01h
int 21h
sub al,48

add bl,al ;bl=bl+al


add bl,48

mov dl,10
mov ah,02h
int 21h

mov dl,13
mov ah,02h
int 21h
print 'your sum is:'
mov dl,bl
mov ah,02h
int 21h

main endp
end main
Product of Two Numbers

include 'emu8086.INC'
.stack 100h
.model small
.data
.code

main proc
print 'enter first number:'

mov ah,01h
int 21h
sub al,48
mov bl,al

mov dl,10
mov ah,02h
int 21h

mov dl,13
mov ah,02h
int 21h

print 'enter second number:'


mov ah,01h
int 21h
sub al,48

mul bl ;al=al*bl
mov bl,al

add bl,48

mov dl,10
mov ah,02h
int 21h

mov dl,13
mov ah,02h
int 21h

print 'your product is:'


mov dl,bl
mov ah,02h
int 21h

main endp
end main
Subtraction of Two Numbers
include 'emu8086.INC'
.stack 100h
.model small
.data
.code
main proc
print 'enter first number:'
mov ah,01h
int 21h
sub al,48
mov bl,al

mov dl,10
mov ah,02h
int 21h

mov dl,13
mov ah,02h
int 21h

print 'enter second number:'

mov ah,01h
int 21h
sub al,48

sub bl,al ;bl=bl-al


add bl,48

mov dl,10
mov ah,02h
int 21h

mov dl,13
mov ah,02h
int 21h

print 'your subtraction is:'


mov dl,bl
mov ah,02h
int 21h

main endp
end main
Loop of 10 Natural Numbers
include emu8086.INC
.stack 100h
.model small

.data

.code

main proc

MOV CX,10
mov bl,1

START:
mov dl,bl
add dl,48
mov ah,02h
int 21h

MOV DL,10
MOV AH,02
INT 21H

MOV DL,13
MOV AH,02
INT 21H

inc bl
LOOP START

main endp
end main
Loop to print hello world
include emu8086.INC
.stack 100h
.model small

.data

.code

main proc

MOV CX,10

START:

PRINT 'HELLO WORLD'


MOV DL,10
MOV AH,02
INT 21H

MOV DL,13
MOV AH,02
INT 21H
LOOP START

main endp
end main
Divide Two Numbers

include emu8086.INC
.stack 100h
.model small

.data

.code

main proc

mov al,4
mov bl,2
div bl

mov bx,ax

print 'R:'
mov dl,bh
add dl,48
mov ah,02h
int 21h

mov dl,10
mov ah,02h
int 21h

mov dl,13
mov ah,02h
int 21h

print 'Q'
mov dl,bl
add dl,48
mov ah,02h
int 21h

main endp
end main

You might also like