Introduction to Assembly Language Programming


This article contains a brief on assembly language programming and the assembly language programs for basic arithmetic operations addition, subtraction, multiplication, division. I explained the programs in a way that helps beginners of assembly programming.

Assembly Language Programming

It is a low level language very near to the machine language i.e. the language understandable by the computer which consists of the bits 0 and 1 only. An assembly language program consists of assembler directives, instructions and variables (memory locations or labels).

Assembler Directives

Assembler directives are predefined alphabetical strings that are hints given to the assembler which converts assembly language to machine language. Some commonly used assembler directives are:
DB (Define Byte) : It is used to reserve byte(s) of memory locations in the memory available.
DW (Define Word) : It is used to reserve word(s) of memory locations in the memory available.
DQ (Define Quad Word) : It is used to reserve 64 bits of memory locations in the memory available.
DT (Define Ten Bytes) : It is used to reserve 10 bytes of memory locations in the memory available.
ASSUME : It informs assembler, name of logical segments to be assumed for different segments in the program.
END : It represents the end of a program.
ENDP : It represents the end of a procedure (like a function in C).
ENDS : It represents the end of a segment.
EQU : It assigns a value to a label.
PROC : It represents the start of a procedure.
Also, + and - operators can be directly used to represent arithmetic addition and subtraction.

Instructions

There are different types of instructions like arithmetic and logical instructions, data copy or transfer instructions, flag manipulation instructions, machine control instructions etc,. To know about different instructions used in the assembly language programs, refer the instruction set of 8086 micro processor.

Program Execution

Follow these steps in order to write and execute an assembly language program.
1) Download and Install TASM or MASM.
2) Write the program. For your information, the editor is called Norton Editor.
3) Save the program. Let us consider it as program.ASM.
4) Assemble the program. Type MASM program.ASM or TASM program.ASM accordingly.
5) Link the program. Type LINK program.OBJ for linking the program.
6) You may or may not use DEBUG command which is used for debugging.
Now, an executable file (.exe) is produced so that you can execute your program whenever you want to.

Assembly Language Programs for Arithmetic

Below mentioned are the assembly language programs for arithmetic operations addition, subtraction, multiplication, division and their explanations. These programs perform addition and subtraction on two 16 bit numbers but multiplication and division on two 8 bit numbers.

Addition

The assembly language program for addition of two 16-bit numbers and its explanation are as follows.


ASSUME CS : CODE, DS : DATA

DATA SEGMENT
OPR1 DW 1111H
OPR2 DW 1111H
RES DW ?
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
MOV BX, OPR2
CLC
ADD AX, BX
MOV RES, AX
INT 21H
CODE ENDS
END START

Line 1 : Data Segment and Code Segment are assumed to be DS and CS respectively.
Lines 2 to 6 : OPR1 and OPR2 are the two operands in the addition operation. They are declared in the data segment. The result of the addition operation is stored in RES which is also declared in the data segment. The ? is used because RES is unknown before performing the addition operation.
Lines 7 to 18 : In the code segment, both operands are moved to registers A and B. Then, addition is performed using ADD instruction and result in AX is moved to RES.

Subtraction

The assembly language program for subtraction of two 16-bit numbers and its explanation are as follows.


ASSUME CS : CODE, DS : DATA

DATA SEGMENT
OPR1 DW 1111H
OPR2 DW 1111H
RES DW ?
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
MOV BX, OPR2
CLC
SUB AX, BX
MOV RES, AX
INT 21H
CODE ENDS
END START

Line 1 : Data Segment and Code Segment are assumed to be DS and CS respectively.
Lines 2 to 6 : OPR1 and OPR2 are the two operands in the subtraction operation. They are declared in the data segment. The result of the subtraction operation is stored in RES which is also declared in the data segment. The ? is used because RES is unknown before performing the subtraction operation.
Lines 7 to 18 : In the code segment, both operands are moved to registers A and B. Then, subtraction is performed using SUB instruction and result in AX is moved to RES.

Multiplication


The assembly language program for multiplication of 2 8-bit numbers and its explanation are as follows.


ASSUME CS : CODE, DS : DATA

DATA SEGMENT
OPR1 DB 11H
OPR2 DB 11H
RES DW ?
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV AL, OPR1
MOV BL, OPR2
CLC
MUL BL
MOV RES, AX
INT 21H
CODE ENDS
END START

Line 1 : Data Segment and Code Segment are assumed to be DS and CS respectively.
Lines 2 to 6 : OPR1 and OPR2 are the two operands in the multiplication operation. They are declared in the data segment. The result of the multiplication operation is stored in RES which is also declared in the data segment. The ? is used because RES is unknown before performing the multiplication operation.
Lines 7 to 18 : In the code segment, both operands are moved to lower parts of registers A and B. Then, multiplication is performed using SUB instruction and result in AX is moved to RES.

Division

The assembly language program for division of 2 8-bit numbers and its explanation are as follows.


ASSUME CS : CODE, DS : DATA

DATA SEGMENT
OPR1 DB 11H
OPR2 DB 11H
QUO DW ?
REM DW ?
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV AL, OPR1
MOV BL, OPR2
CLC
DIV BL
MOV QUO, AL
MOV REM, AH
INT 21H
CODE ENDS
END START

Line 1 : Data Segment and Code Segment are assumed to be DS and CS respectively.
Lines 2 to 7 : OPR1 and OPR2 are the two operands in the division operation. They are declared in the data segment. The results of the division operation i.e. quotient and remainder are stored in QUO and REM respectively which are also declared in the data segment. The ? is used because QUO and REM are unknown before performing the division operation.
Lines 8 to 20 : In the code segment, both operands are moved to lower parts of registers A and B. Then, division is performed using DIV instruction and results i.e. quotient and remainder in AL and AH respectively are moved to QUO and REM respectively.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: