0% found this document useful (0 votes)
13 views

Co All Programs

Uploaded by

22311a05gz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Co All Programs

Uploaded by

22311a05gz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Introduction to MASM

(Microsoft assembler)
To Create Source File:
An editor is a program which allows you to create a file containing the assembly language
statements for your program. This file is called a source file.

Command to create a source file

D:\5K6\> EDIT filename. ASM Enter

The next step is to process the source file with an assembler. When you run the assembler, it
reads the source file of your program. On the first pass through the source program, the
assembler determines the displacement of named data items, the offset labels, etc. and puts this
information in a symbol table. On the second pass through the source program the assembler
produces the binary code for each instruction and inserts the offsets, etc. that it calculated during
first pass.

D:\5K6\> MASM filename.ASM Enter

With this command assembler generates three files.


1. The first file (X) called the object file, is given the extension .OBJ. The object file contains
the binary codes for the instructions and information about the addresses of the instructions.
2. The second file (Y) generated by the assembler is called the assembler list file and is given the
extension .LST. The list file contains your assembly language statements, the binary codes for
each instruction and the offset for each instruction.
3. The third file (Z) generated by this assembler is called the cross-reference file and is given the
extension .CRF. The cross-reference file lists all labels and pertinent information required for
cross – referencing

NOTE :
The Assembler only finds syntax errors : It will not tell you whether program does what it is
supposed to do. To determine whether your program works, you have to run the program and test
it.
Next step is to process the object file with linker.

1
D:\5K6\> LINK filename .OBJ Enter

To debug
D:\5K6\> DEBUG filename.EXE Enter

ASSEMBLY LANGUAGE PROGRAM DEVELOPMENT TOOLS:

EDITOR: An editor is a program, which allows you to create a file containing the assembly
language statements for your program.
ASSEMBLER: An assembler program is used to translate the assembly language Mnemonic
instructions to the corresponding binary codes. The second file generated by assembler is called
the assembler List file.
LINKER: A Linker is a program used to join several object files in to one large object file. The
linkers produce link files with the .EXE extension.
DEBUGGER: If your program requires no external hardware, then you can use a debugger to
run and debug your program. A debugger is a program, which allows you to load your object
code program into system memory, execute the program, and troubleshoot or “debug” it.

ASSEMBLER DIRECTIVES:

An assembler is a program used to convert an assembly language program into the equivalent
machine code modules. The assembler decides the address of each label and substitutes the
values for each of the constants and variables. It then forms the machine code for mnemonics
and data in assembly language program.
Assembler directives help the assembler to correctly understand assembly language programs to
prepare the codes. Commonly used assembler directives are DB, DD, DW, DUP, ASSUME,
BYTE, SEGMENT, MACRO, PROC, OFFSET, NEAR, FAR, EQU, STRUC, PTR, END,
ENDM, ENDP etc. Some directives generate and store information in the memory, while others
do not.
DB :- Define byte directive stores bytes of data in memory.
BYTE PTR :- This directive indicates the size of data referenced by pointer.
SEGMENT :- This directive is to indicate the start of the segment.

2
DUP (Duplicate) :- The DUP directive reserves memory locations given by the number
preceding it, but stores no specific values in any of these locations.
ASSUME : - The ASSUME statement is only used with full segment definitions. This statement
tells the assembler what names have been chosen for the code, data, extra and stack segments.
EQU : - The equate directive equates a numeric ASCII or label to another label.
ORG : - The ORG (origin) statement changes the starting offset address in a segment.
PROC and ENDP : - The PROC and ENDP directives indicate start and end of a procedure
(Sub routine). Both the PROC and ENDP directives require a label to indicate the name of the
procedure. The PROC directive, must also be followed with the NEAR or FAR. A NEAR
procedure is one that resides in the same code segment as the program. A FAR procedure may
reside at any location in the memory system.

MACROS
A macro is a group of instructions that performs one task, just as a procedure. The difference is
that a procedure is accessed via a CALL instruction, while a macro is inserted in the program at
the point of usage as a new sequence of instructions.

MACRO : - The first statement of a macro is the MACRO directive preceded with name of the
macro.
ENDM : - The last statement of a macro is the ENDM instruction. Never place a label in front of
the ENDM statement.
PUBLIC &EXTRN : - The public and extern directives are very important to modular
programming. We use PUBLIC to declare that labels of code, data or entire segments are
available to other program modules. We use EXTRN to declare that labels are external to a
module. Without this statement, we could not link modules together to create a program using
modular programming techniques.
OFFSET : - Offset of a label. When the assembler comes across the OFFSEToperator along with
a label, it first computes the 16 – bit displacement of the particular label, and replaces the string
‘OFFSET LABEL’ by the computed displacement.
LENGTH : - Byte length of the label. This directive is used to refer to the length of data array or
a string.

3
PROGRAMS

1. Write an ALP and execute progam to add two 8-BIT Numbers

ASSUME CS:CODE,
DS:DATA

DATA SEGMENT
NUM1 DB 04H
NUM2 DB 02H
RES DB 00H
DATA ENDS
Result
CODE SEGMENT Registers
START:
MOV AX, DATA
MOV DS, AX
MOV AX, 00H Data Segment

MOV AL, NUM1


MOV BL, NUM2
ADD AL, BL Flag Register
MOV RES, AL

INT 03H
CODE ENDS
END START

4
2. Write an ALP and execute progam to add two 16-BIT Numbers

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
NUM1 DW 0004H
NUM2 DW 1234H
RES DW 00H
DATA ENDS

CODE SEGMENT
Result
Registers
START:
MOV AX, DATA
MOV DS, AX
MOV AX, 00H
Data Segment

MOV AX, NUM1


MOV BX, NUM2
ADD AX, BX
MOV RES, AX Flag Register

INT 03H
CODE ENDS
END START

5
3. Write an ALP and execute progam to add two 32-BIT Numbers

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
X1 DW 1234H
X2 DW 2345H
Y1 DW 3456H
Y2 DW 4567H
RES1 DW 00H
RES2 DW 00H
DATA ENDS

CODE SEGMENT
START:
Result
MOV AX, DATA
Registers
MOV DS, AX
MOV AX,0H

MOV AX, X1 Data Segment


MOV CX, Y1
MOV BX, X2
MOV DX, Y2
ADD AX, CX Flag Register
ADC BX, DX
MOV RES1, AX
MOV RES2, BX

INT 03H
CODE ENDS
END START

6
4. Write an ALP and execute progam to subtract two 8-BIT Numbers

ASSUME CS:CODE,DS:DATA

DATA SEGMENT
NUM1 DB 0F4H
NUM2 DB 0C2H
RES DB 00H
DATA ENDS
Result
CODE SEGMENT
Registers
START:
MOV AX,DATA
MOV DS,AX
MOV AX,00
Data Segment

MOV AL,NUM1
MOV BL,NUM2
SUB AL,BL
MOV RES,AL Flag Register

INT 03H
CODE ENDS
END START

7
5. Write an ALP and execute progam to subtract two 16-BIT Numbers

ASSUME CS:CODE,DS:DATA

DATA SEGMENT
NUM1 DW 56ABH
NUM2 DW 10CFH
RES DW 00H
DATA ENDS

CODE SEGMENT
Result
START: Registers
MOV AX,DATA
MOV DS,AX
MOV AX,00

MOV AX,NUM1 Data Segment


MOV BX,NUM2
SUB AX,BX
MOV RES,AX
Flag Register
INT 03H
CODE ENDS
END START

8
6. Write an ALP and execute progam to subtract two 32-BIT Numbers

ASSUME CS:CODE,DS:DATA

DATA SEGMENT
X1 DW 12F4H
X2 DW 5B78H
Y1 DW 5043H
Y2 DW 0AB0DH
RES1 DW 00H
RES2 DW 00H
DATA ENDS

CODE SEGMENT
START:
Result
MOV AX,DATA
MOV DS,AX Registers
MOV AX,00H

MOV AX,X1
MOV BX,X2 Data Segment
MOV CX,Y1
MOV DX,Y2
SUB AX,CX
SBB BX,DX
Flag Register
INT 03H
CODE ENDS
END START

9
7. Write an ALP and execute progam to multiply two 8-BIT Numbers

ASSUME CS:CODE,DS:DATA

DATA SEGMENT
NUM1 DB 34H
NUM2 DB 0CDH
PROD DW 00H
DATA ENDS
Result
CODE SEGMENT
Registers
START:
MOV AX,DATA
MOV DS,AX
MOV AX,00H Data Segment

MOV AL,NUM1
MOV BL,NUM2
MUL BL Flag Register
MOV PROD,AL

INT 03H
CODE ENDS
END START

10
8. Write an ALP and execute progam to multiply two 16-BIT Numbers

ASSUME CS:CODE,DS:DATA

DATA SEGMENT
NUM1 DW 1234H
NUM2 DW 5678H
PROD1 DW 00H
PROD2 DW 00H
DATA ENDS
Result
Registers
CODE SEGMENT
START:
MOV AX,DATA
MOV DS,AX
Data Segment
MOV AX,00H

MOV AX,NUM1
MOV BX,NUM2
MUL BX Flag Register
MOV RESULT,AX
MOV RESULT1,DX

INT 3H
CODE ENDS
END START

11
9. Write an ALP and execute progam to divide two 8-BIT Numbers

ASSUME CS:CODE,DS:DATA

DATA SEGMENT
NUM1 DB 47H
NUM2 DB 22H
QUO DB 00H
REM DB 00H
DATA ENDS
Result
CODE SEGMENT
Registers
START:
MOV AX,DATA
MOV DS,AX
MOV AX,00H
Data Segment

MOV AL,NUM1
MOV BL,NUM2
DIV BL
MOV QUO,AL Flag Register
MOV REM,AH

INT 03H
CODE ENDS
END START

12
10. Write an ALP and execute progam to divide two 16-BIT Numbers

ASSUME CS:CODE,DS:DATA

DATA SEGMENT
NUM1 DW 0ABC4H
NUM2 DW 1232H
QUO DW 0H
REM DW 0H
DATA ENDS
Result
CODE SEGMENT Registers
START:
MOV AX,DATA
MOV DS,AX
MOV AX, 0H Data Segment

MOV DX,00
MOV AX,NUM1
MOV BX,NUM2
Flag Register
DIV BX
MOV QUO,AX
MOV REM,DX

INT 03H
CODE ENDS
END START

13
11. Write an ALP and execute progam to find the square of a number

ASSUME CS:CODE,DS:DATA

DATA SEGMENT
NUM1 DB 04H
RES DW 00H
DATA ENDS

CODE SEGMENT
START: Result
MOV AX,DATA Registers
MOV DS,AX
MOV AX,00H

MOV AL, NUM1 Data Segment


MOV BL, NUM1
MUL BL
MOV RES,AX
Flag Register
INT 03H
CODE ENDS
END START

14
12. Write an ALP and execute progam to find the Cube of a number

ASSUME
CS:CODE,DS:DATA

DATA SEGMENT
NUM1 DB 04H
RES DW 00H
DATA ENDS

CODE SEGMENT Result


START: Registers
MOV AX,DATA
MOV DS,AX
MOV AX,00H
Data Segment
MOV AL, NUM1
MOV BL, NUM1
MUL BL
MUL BL Flag Register
MOV RES,AX

INT 03H
CODE ENDS
END START

15
13. Write an ALP and execute progam to exchange two numbers

ASSUME CS:CODE,DS:DATA

DATA SEGMENT
NUM1 DB 04H
NUM2 DB A2H
DATA ENDS

CODE SEGMENT
START: Result
MOV AX,DATA Registers
MOV DS,AX
MOV AX,00H

MOV AL, NUM1 Data Segment


MOV BL, NUM1
XHCG AX,BX

INT 03H Flag Register


CODE ENDS
END START

16
14. Write an ALP and execute progam to find the factorial of a number

ASSUME CS:CODE,DS:DATA

DATA SEGMENT
NUM1 DB 04H
RES DW 00H
DATA ENDS

CODE SEGMENT
START:
MOV AX,DATA
Result
MOV DS,AX
Registers
MOV AX,0001H

MOV BL,NUM1

GO: Data Segment


MUL BL
DEC BL
JNZ GO
MOV RES,AX
Flag Register
INT 03H
CODE ENDS
END START

17
PROGRAMS

1. Write an ALP to add given series of Numbers

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
LIST DB 01H,02H,03H,04H,05H
COUNT DB 05H
RES DW 0H
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV AX,0H
Result
MOV CL,COUNT
LEA SI,LIST
Registers
GO:
ADD AL,[SI]
INC SI
Data Segment
DEC CL
JNZ GO
MOV RES,AX

INT 03H Flag Register


CODE ENDS
END START

1
2. Write an ALP to find average of given series of Numbers

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
LIST DB 01H,02H,03H,04H,05H
COUNT DB 05H
RES DB 0H
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV AX,0H

MOV CL,COUNT Result


LEA SI,LIST
GO: Registers
ADD AL,[SI]
INC SI
DEC CL Data Segment
JNZ GO
MOV CL,COUNT
DIV CL Flag Register
MOV RES,AL

INT 03H
CODE ENDS
END START

2
3. Write an ALP and execute progam to find sum of squares of given series of
Numbers

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
LIST DB 01H,02H,03H,04H,05H
COUNT DB 05H
RES DW 0H
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV AX,0H
MOV BX,0H
MOV DX,0H

MOV CL,COUNT
LEA SI,LIST
LEA DI,LIST
Result
GO:
MOV AL,[SI] Registers
MOV BL,[SI]
MUL BL
INC SI Data Segment
ADD DX,AX
DEC CL
JNZ GO Flag Register
MOV RES,DX

INT 03H
CODE ENDS
END START

3
4. Write an ALP and execute progam to find sum of cubes of given series of
Numbers

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
LIST DB 01H,02H,03H,04H,05H
COUNT DB 05H
RES DW 0H
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV AX,0H
MOV BX,0H
MOV DX,0H

MOV CL,COUNT
LEA SI,LIST
LEA DI,LIST

GO: Result
MOV AL,[SI]
MOV BL,[SI] Registers
MUL BL
MUL BL
Data Segment
INC SI
ADD DX,AX
DEC CL
JNZ GO Flag Register
MOV RES,DX

INT 03H
CODE ENDS
END START

4
PROGRAMS

1. Write an ALP to find the largest from given series of Numbers

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
LIST DB 04H,0AH,01H,21H,09H
LARG DB 0H
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV AX,0H

MOV CL,04
LEA SI,LIST
MOV AL,[SI] Result
L2:
CMP AL,[SI+1] Registers
JNC L1
MOV AL,[SI+1]
L1: Data Segment
INC SI
DEC CL
JNZ L2
MOV LARG,AL Flag Register

INT 03H
CODE ENDS
END START

1
2. Write an ALP to find the smallest from given series of Numbers

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
LIST DB 04H,0AH,01H,21H,09H
SMALL DB 0H
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV AX,0H

MOV CL,04
LEA SI,LIST
MOV AL,[SI] Result
L2:
CMP AL,[SI+1] Registers
JC L1
MOV AL,[SI+1]
L1: Data Segment
INC SI
DEC CL
JNZ L2
MOV SMALL,AL Flag Register

INT 03H
CODE ENDS
END START

2
3. Write an ALP to sort the given series of Numbers in ascending order

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
LIST DB 02H,22H,45H,01H,32H
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV AX,0H

MOV CL,04

GO3: LEA SI,LIST


MOV DL,04H
GO1: MOV AL,[SI]
CMP AL,[SI+1]
JC GO2
XCHG AL,[SI+1] Result
MOV [SI],AL
GO2: INC SI
DEC DL Data Segment
JNZ GO1
DEC CL
JNZ GO3

INT 03H
CODE ENDS
END START

3
4. Write an ALP to sort the given series of Numbers in descending order

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
LIST DB 02H,22H,45H,01H,32H
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV AX,0H

MOV CL,04

GO3: LEA SI,LIST


MOV DL,04H
GO1: MOV AL,[SI]
CMP AL,[SI+1]
JNC GO2
XCHG AL,[SI+1]
MOV [SI],AL
GO2: INC SI Result
DEC DL
JNZ GO1
DEC CL
Data Segment
JNZ GO3

INT 03H
CODE ENDS
END START

4
PROGRAMS

1. Write an ALP to Display Fibonacci series

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
COUNT DB 0AH
FIB DB 10 DUP(?)
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV CL,COUNT

LEA SI,LIST
Result
MOV AL,0H
MOV [SI],AL
MOV BL,01H
Data Segment
GO: INC SI
ADD BL,AL
MOV [SI],BL
MOV AL,[SI-1]
DEC CL
JNZ GO

INT 03H
CODE ENDS
END START

1
2. Write an ALP to move a string of data bytes form one location to another

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
STR1 DB ‘CSE’
DATA ENDS

EXTRA SEGMENT
STR2 DB ‘00H’
EXTRA ENDS

CODE SEGMENT
START: Result
MOV AX, DATA
MOV DS, AX Data Segment

MOV AX, EXTRA


MOV ES, AX
Extra Segment
LEA SI, STR1
LEA DI, STR2
MOV CL,03H

CLD
REP MOVSB

INT 03H
CODE ENDS
END START

2
3. Write an ALP to concatenate two strings

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
STR1 DB ‘CSE’
STR2 DB ‘SNIST’
STR3 DB 00H
DATA ENDS

CODE SEGMENT
START:
MOV AX,DATA
MOV DS,AX
MOV AX,0H

MOV CL,08H Result


LEA SI,STR1
LEA DI,STR3 Data Segment

GO:
MOV AL,[SI]
MOV [DI],AL
INC SI
INC DI
DEC CL
JNZ GO

INT 03H
CODE ENDS
END START

3
4. Write an ALP to reverse a given string

ASSUME CS:CODE, DS:DATA,

DATA SEGMENT
STR1 DB ‘CSE’
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV AX,0H

MOV CL,03H

LEA SI,STR1 Result


LEA DI,STR+6
Data Segment
GO:
MOV AL,[SI]
MOV [DI],AL
INC SI
DEC DI
DEC CL
JNZ GO

INT 03H
CODE ENDS
END START

4
PROGRAMS

1. Write an ALP to find the compare two strings

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
STR1 DB ‘CSE’
L1 DB 03H
STR2 DB ‘ECE’
L2 DB 03H
RES DB 0H
DATA ENDS

CODE SEGMENT
START:
MOV AX,DATA
MOV DS,AX
MOV ES,AX
LEA DI,STR1
LEA SI,STR2
MOV CL,L2
MOV DL, 0H Result
CMP CL,L1
JNZ EXIT Registers
REP CMPSB
JNZ EXIT
MOV DL,01H
EXIT: Data Segment
MOV RES,DL

INT 03H Extra Segment


CODE ENDS
END START

1
2. Write an ALP to find the Length of a given string

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
STR1 DB ‘CSEBATCH’,0
LEN DW 0H
DATA ENDS

CODE SEGMENT
START:
MOV AX,DATA
MOV DS,AX Result
MOV ES,AX,
MOV AX,0H
LEA DI,STR1
REPNE SCASB Data Segment
DEC DI
MOV LEN,DI
Extra Segment
INT 03H
CODE ENDS
END START

2
3. Write an ALP to find whether the given byte is in the string or not

ASSUME CS:CODE, DS:DATA

DATA SEGMENT
STR1 DB ‘CSE’
L1 DB 03H
STR2 DB ’E’
RES DB 0H
DATA ENDS

CODE SEGMENT
START:

MOV AX, DATA


MOV DS, AX
MOV ES,AX Result
MOV DL,0H Registers

LEA DI,STR1
MOV AL,STR2 Data Segment
MOV CL,L1
CLD
REPNE SCASB
JNZ EXIT
MOV DL,01H Extra Segment
EXIT:
MOV RES,DL

INT 03H
CODE ENDS
END START

You might also like