Sei sulla pagina 1di 201

t

gi
is

Microprocessor Laboratory

ps

Introduction

gi

Microprocessor Laboratory Course


Content

is

10CSL48
12 Software programs
12 Hardware programs using interface kits
MASM (Microsoft Assembler)

ps

t
gi

Assembler

is

Is a computer program which translates from


assembly language to an object file or
machine language format

Program
.asm

Source
Program

Object file
.obj

Assemble

Assembler

link

Executable file
.exe

Obj &
Exe files

ps

object file - contains machine instructions, initial data,


and information used when loading the program

ps

is

gi

Assembler and Compiler

gi

What Is Assembly Language


Machine-Specific Programming Language

ALP

MOV AL, BL

is

one-one correspondence between statements and


native machine language
matches machine instruction set and architecture
Machine Language

1000100011000011
MOV

IBM-PC Assembly Language

Register
mode

ps

refers to 8086, 8088, 80186, 80286, 80386, 80486,


and Pentium Processors

ps

is

t
gi

Registers

gi

Why Learn Assembly Language?


Allows creation of small
and efficient programs
Allows programmers to
bypass high-level
language restrictions
Might be necessary to
accomplish certain
operations

ps

is

Learn how a processor


works
Understand basic
computer architecture
Explore the internal
representation of data
and instructions
Gain insight into
hardware concepts

gi

Overview of Assembly Language


Advantages:

is

Disadvantages:

Faster as compared to programs written using high-level


languages
Efficient memory usage
Control down to bit level
Need to know detail hardware implementation
Not portable
Slow to development and difficult to debug

Basic components in assembly Language:

ps

Instruction, Directive, Label, and Comment

gi

Numbering Systems

ps

is

Binary - Base 2
0, 1
Octal - Base 8
0, 1, 2, 7
Decimal - Base 10
0, 1, 2, , 9
Hexadecimal (Hex) - Base 16
0, 1, , 9, A, B, , F

gi

Assembler Directives

Tells the assembler what segments to use.

SEGMENT

Defines the segment name and specifies that the


code that follows is in that segment.

ENDS

End of segment

ORG

Originate or Origin: sets the location counter.

END

End of source code.

DW

Define word

DB

Define byte.

EQU

Equate or equivalence

is

End of the string

ps

ASSUME

t
gi
e
is
u
ps

Memory

8086 Instruction Set

t
gi
ps

is

Here we go!

gi

Sample Program
Problem Statement

is

Write a program (ALP) to add two 8 bit


numbers: 0000 0101 (=05)
and 0000 1000 (=04)

ps

Binary & Hexadecimal Numbers

gi

Developing Addition Algorithm

AL

MOV AL,05

BL

MOV BL,04

AL

ADD AL,BL

05
04

is

ps

09

8086 Microprocessor Block Diagram

gi

ALP Instruction Terminology

Operands
Destination Operand Source Operand
AL

05

MOV

BL

04

ADD

AL

BL

MUL

BL

DIV

AX

ps

MOV

is

Mnemonic

BL

ps

mov ah,4ch
int 21h
code ends
end start

Assembler directives

ALP just now written

is

assume cs:code
code segment
start:
mov al,05
mov bl,04
add al,bl

gi

MASM ALP (Microsoft Assembler Assembly Language Program)

Exit code (interrupt)

ps

mov ah,4ch
int 21h
code ends
end start

e
is

assume cs:code
code segment
start:
mov al,05
mov bl,04
add al,bl

gi

Add 2 numbers: COPY THIS

This code / program you need to


type in EDITOR, later.

gi

Basic Lab. Steps

ps

is

Command prompt
CD\
CD MASM32
EDIT
Type the ALP & save the file with
filename.asm

ps

is

1.EDIT filename.asm
2.MASM filename;
3.LINK filename;
4.CV filename

t
gi

MASM Steps

EDIT
MASM
LINK
CV

t
gi
is

Boot Your Computer to load


Windows XP

ps

Enter your
Login name and Password

gi

Step 1: EDITOR

is

Type your program in EDITOR

ps

On completion save as filename.asm


For example: add.asm
(filename should be 6
characters)

t
gi

Step 2: MASM

ps

is

MASM filename;

For example:
masm add;

t
gi

Step 3: LINK

ps

is

LINK filename;

For example:
link add;

t
gi

Step 4: CV

ps

is

CV filename

For example:
cv add

t
gi

Executing

ps

is

Execution Steps:
RUN ANIMATE
Or
RUN SINGLE STEP

program loaded in CV (CODEVIEW), which is


loader.

t
gi
is

Now

ps

Your Turn

is

mov al,05
mov bl,04
add al,bl

mov al,05

ps

add al,04

t
gi

Optimize Code

ps

mov ah,4ch
int 21h
end segment
end start

e
is

assume cs:code
code segment
start:
mov al,05
mov bl,04
add al,04

gi

Add 2 numbers: Optimized

This code / program you need to


type in EDITOR, try it.

mov al,number1
add al,number2
mov sum,al

ps

mov ah,4ch
int 21h
end segment
end start

gi
e
is

assume cs:code,ds:data
data segment
number1 db 04
number2 db 05
sum db ?
data ends
code segment
start:
mov ax,data
mov ds,ax

Add 2 numbers: Using Data segment

t
gi
is

VTU Lab. Program - 7a.asm

ps

displays your name at specified


location

ASSUME CS:CODE,DS:DATA

DATA SEGMENT
MSG1 DB "ENTER YOUR NAME:$"
DATA ENDS

START:

MOV AX,DATA
MOV DS,AX
LEA DX,MSG
MOV AH,09H
INT 21H

ps

MOV AH,4CH
INT 21H
CODE ENDS
END START

is

CODE SEGMENT

gi

Display message Enter Your Name:

gi

Display two messages


ASSUME CS:CODE,DS:DATA

is

DATA SEGMENT
MSG1 DB "ENTER YOUR NAME:$
MSG2 DB "WHAT IS YOUR NAME?$"
DATA ENDS
CODE SEGMENT
START:

ps

MOV AX,DATA
MOV DS,AX

gi

t
ps

MOV AH,4CH
INT 21H
CODE ENDS
END START

is

LEA DX,MSG2
MOV AH,09H
INT 21H

LEA DX,MSG1
MOV AH,09H
INT 21H

t
gi

7a Algorithm

ps

is

1. Display message Enter Your Name:


2. Receive name from keyboard and store in
memory (ARRAY)
3. Clear screen
4. Take cursor to the centre of the screen.
5. Display message What is your name?
6. Display your name already entered, from
memory (ARRAY)

gi

7a.asm program explained


;7a.asm
;displays your name at specified location

is

ASSUME CS:CODE,DS:DATA

ps

DATA SEGMENT
MSG1 DB 0AH,0DH,"ENTER YOUR NAME:$"
MSG2 DB "WHAT IS YOUR NAME?$"
ARRAY DB 30 DUP(0)
DATA ENDS

gi

CODE SEGMENT

is

START:
MOV AX,DATA
MOV DS,AX

ASSUME
CS:CODE,DS:DATA

ps

LEA DX,MSG1
MOV AH,09H
INT 21H

1. Display message
Enter Your Name:

MOV AH,01H
INT 21H
MOV [SI],AL
INC SI
CMP AL,0DH
JZ DOWN
JMP NEXT

gi

NEXT:

2. Receive name from


keyboard and store
in memory (ARRAY).

is

LEA SI,ARRAY

ps

DOWN:
DEC SI
MOV BYTE PTR [SI],'$'

t
gi

MOV DH,12
MOV DL,20
MOV AH,02
INT 10H

4. Take cursor to the


centre of the screen

ps

is

3. Clear screen

MOV AX,0003H
INT 10H

5. Display message
What is your
name?

LEA DX,ARRAY
MOV AH,09H
INT 21H

6. Display your name


already entered,
from memory
(ARRAY)

is

gi

LEA DX,MSG2
MOV AH,09H
INT 21H

MOV AH,4CH
INT 21H

ps

CODE ENDS
END START

t
gi
is

Writing Journal

ps

7a. asm
Displaying Name at the center of the
screen

gi

7.a. Displaying Name at the center of


screen
Aim:

is

Read your name from the keyboard and display


it at a specified location on the screen after the
message What is your name? You must clear
the entire screen before display.

Software used:

ps

Windows XP DOS mode, MASM32 package

t
gi

Theory:

ps

is

Data segment is loaded with two messages and


string for name to be entered by user.
OA new line introduced before MSG1 (line
feeds ASCII code)(same as new line \n in C
program)
OD cursor taken 1st row & 1st column
(=ENTERs ASCII code)

t
gi
e

Auto initialization of data segment by loading DS


register with address of data segment.

is

Appropriate interrupts are used to implement


the requirements of the algorithm.

ps

Algorithm is designed with knowledge of 80X86


architecture
and
assembly
language
programming.

t
gi

Algorithm:

ps

is

1. Display message Enter Your Name:


2. Receive name from keyboard and store in
memory (ARRAY)
3. Clear screen
4. Take cursor to the centre of the screen
5. Display message What is your name?
6. Display your name already entered, from
memory (ARRAY)

t
gi

Procedure:

ps

is

1. Boot up Windows XP, goto command prompt


- change the directory to MASM32
2. Invoke MS DOS editor, type the ALP, save the
program with filename.asm
3. Link the program to create filename.obj
4. Execute the program
5. Observe the results

t
gi

Results:

ps

is

Entered name is displayed on the DOS screen.

ps

is

gi

7.a. output snapshots

t
gi
is

2.a.

ps

Macros to read and display

gi

2.a. Macros to read and display


Aim:

is

Write two ALP modules stored in two different


files; one module is to read a character from the
keyboard and the other one is to display a
character. Use the above two modules to read a
string of characters from the keyboard terminated
by the carriage return and print the string on the
display in the next line.

Software used:

ps

Windows XP DOS mode, MASM32 package

t
gi

Theory:

ps

is

Macro:
The term macro refers to a word that stands for
an entire group of instructions.
permit a group of instructions to be defined as a
single entity
with a unique given label or name called up when
needed.
We need to write two macros: one to read a single
alpha-numeral from keyboard (in2a.asm)and
another to display (out2a.asm), separately as .asm
files.

t
gi
is

The macros have to be included in main (calling)


ALP 2a.asm, and stored in same masm32
directory.

Auto initialization of data segment by loading DS


register with address of data segment.

ps

Appropriate interrupts are used to implement


the requirements of the algorithm.

t
gi

Algorithm:

ps

is

1. Reserve memory to store string.


2. Call READ macro present in IN2A.ASM, which is
keyboard single character interrupt, AH=01H,
INT 21H.
3. Loop the macro till user presses ENTER (0DH),
updating CX.
4. Call DISPLAY macro from OUT2A.ASM, which is
single character display interrupt, AH=02, INT
21H with DL loaded with characters ASCII value.
5. Loop the process till CX becomes zero.

t
gi

Procedure:

ps

is

1. Boot up Windows XP, goto command prompt


- change the directory to MASM32
2. Invoke MS DOS editor, type the ALP, save the
program with filename.asm
3. Link the program to create filename.obj
4. Execute the program
5. Observe the results

t
gi

Results:

ps

is

Entered string followed by <enter> is displayed


on newline.

ps

t
is

gi

Screen shots:

gi

2a.asm program explained

DATA SEGMENT

is

INCLUDE IN2A.ASM
INCLUDE OUT2A.ASM

ASSUME DS:DATA,CS:CODE

MSG DB 0AH,0DH,"THE ENTERED STRING IS : $"


STRING DB 30H DUP(?)

ps

DATA ENDS

gi

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

UP:
READ
CMP AL,0DH
JZ STOP
MOV [SI],AL

ps

INC SI
INC CL
JMP UP

MOV CX,0000H

;initialize array to store to be entered string


(30 characters)
;initialize counter CX with 0, as LOOP acts up to CX=0

is

LEA SI,STRING

;AL loaded with read character, using MACRO in READ


;loop till enter pressed 0D-ASCII code for ENTER
;if pressed ENTER stop, and go to display process
;if next character, place in SI content addressed
memory location
;point to next address
;CX updated so that LOOP loops CX times
;loop until user presses ENTER key

STOP:

;display out message

LEA SI,STRING

;SI indexed to start of string

ps

MOV AH,4CH
INT 21H
CODE ENDS
END START

gi

DISPLAY
INC SI
LOOP UP1

;index transferred to AL, as DISPLAY


macro in OUT2A.ASM is using AL
;call MACRO that is in OUT2A.ASM
;arrange to display next character
;display all characters till $ is found,
loop till CX = 0

is

UP1:
MOV DL,[SI]

LEA DX,MSG
MOV AH,09H
INT 21H

;terminate interrupt

t
gi

in2a.asm

is

;IN2A.ASM is a include file containing a macro


having an keyboard input of single character
AH=01H,
;INT 21H waits for user to press a key & when
pressed stores its ASCII in AL.

ps

READ MACRO
MOV AH,01H
INT 21H
ENDM

t
gi

out2a.asm

is

;OUT2A.ASM is a include file containing a macro


;having an monitor output of single character,
;AH=02H,
;INT 21H sends a single character whose ASCII in DL.

ps

DISPLAY MACRO
MOV AH,02H
INT 21H
ENDM

t
gi
is

1a.asm

ps

Binary Search

gi

1a.asm Binary Search


Aim:

is

Search a key element in a list of n 16-bit


numbers using the Binary search algorithm.

Software used:

ps

Windows XP DOS mode, MASM32 package

t
gi

Theory:

ps

is

Binary search or half-interval search algorithm finds the


position of a specified input value (the search "key")
within an array sorted by key value.
In each step, the algorithm compares the search key
value with the key value of the middle element of the
array. If the keys match, then a matching element has
been found and its index, or position, is returned.
Otherwise, if the search key is less than the middle
element's key, then the algorithm repeats its action on
the sub-array to the left of the middle element or, if the
search key is greater, on the sub-array to the right.
If the remaining array to be searched is empty, then the
key cannot be found in the array and a special "not
found" indication is returned.

t
gi

Algorithm:

ps

is

lo := 0;
hi := 7; {array size: var BX : LIST [0..7] of integer}
repeat
mid := lo + (hi - lo) div 2;
if KEY > LIST[mid] then
lo := mid + 1;
else
hi := mid - 1;
until (LIST[mid] = KEY) or (lo > hi);

DATA ENDS

;auto-initialization
;of data segment

is

CODE SEGMENT
START:
MOV AX,DATA
MOV DS,AX
MOV AL,0
MOV AH,7
MOV BX,0000

;AL = 0, lo
;AH = 7, hi
; clear BX, mid

CMP AH,AL
JL NOMATCH

;compare hi & lo
;if AH < AL, go to NOMATCH

NEXT:

ps

MOV BL,AL
ADD BL,AH
SHR BL,01

;else, calculate mid

;BX mid

gi

Binary Search ALP

ASSUME CS:CODE,DS:DATA
DATA SEGMENT
LIST db 10H,20H,30H,40H,50H,60H,70H,80H
KEY equ 60H

;given list
;KEY to be searched,
;change it

;not necessary but, BH is cleared

CMP LIST[BX],KEY
JE MATCH
JB UPPER
MOV AH,BL
DEC AH
JMP NEXT

;compare mid with key


;if mid = key, key found, go to MATCH,
;if mid < key, key is in second upper half of LIST, go to UPPER
;else (i. e., mid > key) key is in lower half of the LIST, ? hi = mid
; AH AH - 1 ( hi hi - 1)
;go to NEXT

MOV AL,BL
INC AL
JMP NEXT

;lo = mid (AL BL)


;AL AL + 1 (lo lo + 1)
;go to NEXT

MOV DX,0FFFFH
JMP LAST

;DX FFFF on KEY found


;go to terminate

gi

MOV BH,0

is

UPPER:

MATCH:

ps

NOMATCH:
MOV DX,0AAAAH ;DX AAAA on KEY not found
LAST:
MOV AH,4CH
;terminate
INT 21H

t
gi

Procedure:

ps

is

1. Boot up Windows XP, goto command prompt


- change the directory to MASM32
2. Invoke MS DOS editor, type the ALP, save the
program with filename.asm
3. Link the program to create filename.obj
4. Execute the program
5. Observe the results

t
gi

Result:

is

Case 1:
If key found DX = FFFFF

ps

Case 2:
If key not found DX = AAAA

t
gi
is

3a.asm

ps

Bubble Sort

t
gi
list

CX

cmp al,[si]

;compare first two elements

jc down

;if first < next, go to down OR

xchg al,[si]

;if next > first, exchange them

mov [si-1],al

;take next element in SI

;decrement counter
;if not zero, go to next

loop next1

;if zero, go to next1

ps

jnz next

int 21h

SI

SI+1

SI+2

SI+3

SI+4

;load AL with element pointed by SI


;point to next element

mov ah,4ch

;point to list with SI

inc si

down: dec bx

;terminate

next: mov al,[si]

SI-1

;load count in BX

is

mov si,offset list

AL

mov cx,count

next1: mov bx,cx

BX

5>1

t
gi
is

3a.asm

ps

Journal Writing

t
gi
is

Aim:
Sort a given set of n numbers in ascending
order using the Bubble Sort algorithm.

Software used:

ps

Windows XP DOS mode, MASM32 package

t
gi

Theory:

ps

is

Bubble sort, sometimes incorrectly referred to as


sinking sort, is a simple sorting algorithm that works
by repeatedly stepping through the list to be sorted,
comparing each pair of adjacent items and
swapping them if they are in the wrong order.
The pass through the list is repeated until no swaps
are needed, which indicates that the list is sorted.
The algorithm gets its name from the way smaller
elements "bubble" to the top of the list. Because it
only uses comparisons to operate on elements, it is
a comparison sort.

t
is

CX count
BX count for inner loop
SI address of list to be sorted
AL SI pointed data
SI SI + 1
AL [SI] , C=1 if AL < [SI] else C = 0
If C = 1 jump to step 9
Else exchange the two elements

ps

1.
2.
3.
4.
5.
6.
7.
8.

gi

Algorithm:

t
gi
ps

is

10.BX BX 1
11.If BX 0 then jump to step 4
12.If CX = 0 then jump to step 2

gi

Example:

Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to
greatest number using bubble sort algorithm. In each step, elements written in bold are
being compared.

is

First Pass:
( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them.
( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm
does not swap them.

ps

Second Pass:
(14258) (14258)
( 1 4 2 5 8 ) ( 1 2 4 5 8 ), Swap since 4 > 2
(12458) (12458)
(12458) (12458)
And so on,

t
gi

3a.asm ALP
;3a.asm bubble sort in ascending order

assume cs:code,ds:data
data segment
list db 5h,1h,4h,2h,8h
count equ 04
data ends

is

;goto CV
;give command d ds:0000
;observe DS dump to see unsorted and sorted, on partially and fully execution of program,
;by F8 key twice and ALT+R+A commands, respectively.

mov ax,data
mov ds,ax

ps

start:

code segment

;auto initialization of Data Segment by loading address of DS


;in to DS register (immediate addressing mode is not possible)

next1:

gi

mov cx,count

mov bx,cx
mov si,offset list

;load count in BX
;point to list with SI

mov al,[si]
inc si
cmp al,[si]
jc down
xchg al,[si]
mov [si-1],al

;load AL with element pointed by SI


;point to next element
;compare first two elements
;if first < next, go to down
;if next > first, exchange them
;take next element in SI

dec bx
jnz next
loop next1

;decrement counter
;if not zero, go to next
;if zero, go to next1

is

next:

down:

ps

mov ah,4ch
int 21h
code ends
end start

;terminate

t
gi

Procedure:

ps

is

1. Boot up Windows XP, goto command prompt


- change the directory to MASM32
2. Invoke MS DOS editor, type the ALP, save the
program with filename.asm
3. Link the program to create filename.obj
4. Execute the program
5. Observe the results

t
gi

Result:

is

51248

Unsorted list in CV window:

Sorted list in CV window:

ps

12458

is

gi

Output Screen Shots

ps

1. Before sorting DS dump:


5 1 4 2 8

t
gi
e
is
u

ps

2. After execution DS contents:


1 2 4 5 8

t
gi
is

11a.asm

ps

Moving cursor

t
gi

Aim:

is

Read a pair of input co-ordinates in BCD and


move the cursor to the specified location on the
screen.

Software Used:

ps

Windows XP DOS mode, MASM32 package

t
gi

Theory:

ps

is

To print formatted output on the screen we have


to take away the cursor without writing anything
on the screen.

t
gi

Algorithm:

ps

is

1. Read row & column values (screen size is 25 x


80)
2. Use interrupt service to move cursor
3. Wait for user key pressed in a loop
4. Exit

t
gi

11a.asm ALP

DATA SEGMENT

is

ASSUME CS:CODE, DS:DATA

; 14a.asm-move cursor to x,y


; To execute, type filename & press enter
; You will observe cursor moved to (x,y) = (row,column)
; Location, declared in data segment
; The screen is having 25 x 80 character resolution

x db 20
y db 40

;row - 0 to 24
;column - 0 to 79

DATA ENDS

ps

CODE SEGMENT
START:

MOV AX,DATA
MOV DS,AX

;data segment
;auto-initialization

;DH = row
;DL = column
;BH = 0, new page / clear screen
;function # 02 in AH
;int 10h, places cursor

gi

MOV DH,x
MOV DL,y
MOV BH,00
MOV AH,02
INT 10H

is

LOOP2:

;DIRECT CONSOLE I/O CHARACTER INPUT


;DL 0FFH, AH 06H

MOV DL,0FFH
MOV AH,06H

;INT 21H KEPT IN A LOOP


;WAITS UNTILL USER PRESSES
;A KEY, THEN TERMINATES THE LOOP

MOV AH,4CH
INT 21H

; terminate
; interrupt

ps

CODE ENDS
END START

INT 21H
CMP AL,00H
JE LOOP2

t
gi

Procedure:

ps

is

1. Boot up Windows XP, goto command prompt


- change the directory to MASM32
2. Invoke MS DOS editor, type the ALP, save the
program with filename.asm
3. Link the program to create filename.obj
4. Execute the program
5. Observe the results

t
gi

Result:

ps

is

The cursor is moved to the location


specified by row & column values.

gi

11a Screen shots


Cursor moved to:

ps

is

x=row=10 & y=column=40

t
gi
is

5a.asm
Palindrome - Reverse a string

ps

Journal writing

t
gi

Aim:

is

Reverse a given string and check whether it is a


palindrome or not.

Software Used:

ps

Windows XP DOS mode, MASM32 package

gi

Theory:

ps

is

A palindrome is a word, phrase, number, or other


sequence of symbols or elements, whose meaning may
be interpreted the same way in either forward or reverse
direction.
Famous examples include
Ada
Anna
Bob
Aviva
Gadag
"Amore, Roma",
"A man, a plan, a canal: Panama"
"No 'x' in 'Nixon'

ps
e

is

gi

ps

t
is

gi

Algorithm:

NOTE: use n & r up to 6, AVOID (n-r) = 1

gi

5a.asm ALP

is

assume ds:data,cs:code

;5a.asm - palindrome
;DX=FFFF means string is palindrome
;DX=AAAA means string is not palindrome
;check result in CV in register DX
;use command D DS:0000 to see Data Segment dump in CV

ps

data segment
givenstring db "liril"
n dw $-givenstring ;no. of characters is
;calculated by"$-givenstring"
reversedstring db 10 dup(0)
data ends

gi

code segment
start:
mov ax,data
mov ds,ax

;auto initialization of
;data segment

;reversing begins
mov si,0
mov di,0

is

mov cx,n

;point to first character SI


;point to first location for reversing

add si,cx
dec si
mov al,givenstring[si]
mov reversedstring[di],al

ps

loop1:

;CX = n, counter, LOOP executes CX


;times, CX is decremented ;automatically
;SI point to last character of string, reverse
;pointer.
;SI loaded with count n
;reversing
;AL ? givenstring[SI] &
;reversedstring[DI] ? AL being SI & DI
;are opposite.

loop loop1
;reversing is complete here

;update SI
;update DI

gi

dec si
inc di

;loop till SI=0

;make SI=0000h, as it is FFFFh after dec of 0000h

is

mov si,0
mov cx,n

;checking for palindrome begins

loop2:
mov al,givenstring[si]

cmp al,reversedstring[si]

ps

je loop3

;take givenstring characters one by


;one in AL to compare
;compare AL to reversedstring one
;by one
;jump if matched

;if no matched, givenstring is not a


;palindrome, if DX=AAAA

gi

mov dx,0aaaah

mov ah,4ch
int 21h
loop3:

mov ah,4ch
int 21h

ps

code ends
end start

;update SI
;next character
;given string is a palindrome, if
;DX=FFFF

is

inc si
loop loop2
mov dx,0ffffh

t
gi

Procedure:

ps

is

1. Boot up Windows XP, go to command prompt


- change the directory to MASM32
2. Invoke MS DOS editor, type the ALP, save the
program with filename.asm
3. Link the program to create filename.obj
4. Execute the program
5. Observe the results

t
gi

Result:

ps

is

1. DX=FFFF means string is palindrome


2. DX=AAAA means string is not palindrome

is

gi

5a Screen shots

ps

Givenstring = liril
Reversedstring = liril

palindrome, DX = FFFFh

gi

Givenstring = lirila
Reversedstring = aliril

ps

is

not palindrome, DX = AAAAh

t
gi
is

8a.asm - nCr

ps

Journal Writing

t
gi

Aim:

is

Compute nCr using recursive procedure. Assume


that n and r are non-negative integers.

Software Used:

ps

Windows XP DOS mode, MASM32 package

t
gi

Theory:

ps

is

A Combination is a selection of some or all


of a number of different objects. It is an unordered collection of unique sizes. In a
permutation the order of occurrence of the
objects or the arrangement is important but in
combination the order of occurrence of the
objects is not important.

gi
is

Inputs n & r are put in data segment.


Calculate n! ---numerator
Calculate (n-r)
Calculate (n-r)! ---denominator1
Calculate r!
---denominator2
Calculate nCr = n! / ((n-r)! x (r!))
AX is having result.

ps

1.
2.
3.
4.
5.
6.
7.

Algorithm:

gi

8a.asm ALP

ps

is

assume cs:code,ds:data
data segment
n dw 0006h
r dw 0003h
numerator dw ?
denimonator1 dw ?
denominator2 dw ?
denominator dw ?
data ends
code segment

; 8a.asm - nCr
; NOTE: use n & r up to 6, AVOID (n-r) = 1
; Execution Steps:
; MASM, LINK & CV filename
; result is in AX in Hex form, refer table at the end for sample nCr values

start:

gi

mov ax,data
mov ds,ax
;--n!

mov bx,ax
mov cx,ax
dec cx

;number n is in immediate addressing


;mode, ax <-- n
;bx <-- n to facilitate n x (n-1)
;cx <-- n to facilitate LOOP up to (n-1)
;now LOOP executes (n-1) times,
;automatically decrementing cx each time

is

mov ax,n

ps

up1: dec bx
; always n x (n-1)
mul bx
loop up1
mov numerator,ax
;AX = n!

;(n-r) is calculated in ax

is

up2:

;mov ax,03h
mov bx,ax
mov cx,ax
dec cx
dec bx
mul bx
loop up2
mov denominator1,ax

;--r!

ps

mov ax,r
mov bx,ax
mov cx,ax
dec cx

gi

mov ax,n
mov bx,r
sub ax,bx

;--(n-r)!

gi
is

;--denom=(n-r)1 x r!
mov ax,denominator1
mov bx,denominator2
mul bx
mov denominator,ax

dec bx
mul bx
loop up3
mov denominator2,ax

up3:

;--complete nCr-------------------AX contains result


mov bx,denominator
mov ax,numerator
div bx

ps

int 03h
code ends
end start

;terminate without affecting AH

t
gi

Procedure:

ps

is

1. Boot up Windows XP, goto command prompt


- change the directory to MASM32
2. Invoke MS DOS editor, type the ALP, save the
program with filename.asm
3. Link the program to create filename.obj
4. Execute the program
5. Observe the results

t
gi

Results:

nCr Hexadecimal

4C2

06

5C2

10

0A

5C3

10

6C2

15

0F

6C3

20

14

6C4

15

0F

ps

is

nCr Decimal

nCr

0A

ps

is

t
gi

ASCII Code Chart:

is

gi

Screen shots:

ps

6C2 = 15D = 0FH in AX register.

t
gi
is

4a.asm ASCII Conversion &


Display

ps

Journal Writing

t
gi

Aim:

is

Read an alphanumeric character and display its


equivalent ASCII code at the center of the
screen.

Software Used:

ps

Windows XP DOS mode, MASM32 package

gi

4a.asm ALP

code segment

is

assume cs:code

;4a.asm - ASCII Display


;MASM, LINK & Direct filename, enter a alpha-numeral
;input: enter a alpha-numeral
;output: its ASCII is displayed on screen

start:

;read character in AL

ps

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

;save AL in BL to be used below

;again take character from BL to DL for processing


;mask lower nibble
;count=4 for shift instruction
;swap higher nibble and lower nibble
;compare with 10(0 to 9)
;if less than 10(0 to 9) jump to get added by 30, to make ASCII
;if greater than 9, add 7

mov dl,bl
and dl,0f0h
mov cl,04
shr dl,cl
cmp dl,10
jl down1
add dl,07

gi

; obtain ASCII value of higher nibble

down1:

mov ah,02h
int 21h

is

add dl,30h

;display service: 02h function, int 21h


;displays single character, when DL ? ASCII of the character

; convert lower nibble in to ASCII

ps

mov dl,bl
and dl,0fh
cmp dl,10
jl down2
add dl,07

;once again load DL with BL, the character pressed


;mask the higher nibble
;compare with 10
;jump if less than 10 (0 to 9)
;if greater than 9 (10 and above) add 7

t
;if less than 10, add 30 to make it ASCII
;display service: 02h function, int 21h
;displays single character, when DL ? ASCII
;of the character

is

mov ah,02h
int 21h

gi

down2:
add dl,30h

ps

code ends
end start

mov ah,4ch
int 21h

;terminate

Theory:

gi

Text in a computer is stored as numbers called ASCII numbers with each


letter having its own number. ASCII is short for American Standard Code
for Information Interchange. With applications in computers and other
devices that use text, ASCII codes represent text. Based on the English
alphabet, ASCII is a character-encoding scheme. ASCII was originally
developed from telegraphic codes.

is

Computers can only understand numbers, and ASCII codes are


numerical representations of characters that a computer can
understand. Examples of characters are a, 1, or >. For example, 097 is
the ASCII numerical representation of the character a. ASCII covers over
100 characters with some of these characters being control characters
that control how text appears.

ps

Many of todays character-encoding schemes are based on ASCII, plus


they include additional characters. At one time ASCII was used on the
World Wide Web as the most commonly used character encoding.

t
gi

Algorithm:

ps

is

1. Accepts a character from keyboard, AL is


having ASCII of the character.
2. Mask (make 0) lower nibble, to transfer
upper to lower nibble, swap upper and lower
nibble
3. If ASCII 9 add 30 to get ASCII of the nibble.
4. If ASCII 10 add 30 & 7 (37) to get ASCII of
the nibble.
5. Repeat for lower nibble.
6. Display the both nibble one after another by
loading in AL and calling int 21h, 02 function

t
gi

Procedure:

ps

is

1. Boot up Windows XP, goto command prompt


- change the directory to MASM32
2. Invoke MS DOS editor, type the ALP, save the
program with filename.asm
3. Link the program to create filename.obj
4. Execute the program
5. Observe the results

t
gi

Result:
ASCII

a
A
1
9

61H
41H
31H
39H
0DH

is

Alphanumeral

ps

(ENTER key)

ps

is

gi

ASCII Code Chart: (do not write in Journal)

ps

t
is

gi

Screen Shots:

t
gi
is

12a1.asm
12a2.asm

ps

file creation
and
file deletion

t
gi

Aim:

is

Write a program to create a file (input file) and


to delete an existing file.

Software Used:

ps

Windows XP DOS mode, MASM32 package

t
gi

Theory: file creation

Int 21H creates a new file if not already exists, when supplied
following parameters:

is

AX=716Ch, Function-Creates or opens a file


BX=0002h, Mode-Opens the file for reading and writing
CX=0000h, Attributes-The file can be read from or written
to
DX=0010h, Action-Creates a new file if it does not already
exist
DS=Segment address of filename

ps

SI=Offset of address of filename

t
gi

Algorithm: file creation

ps

is

1. Provide path and filename for the file to be


created.
2. Load function number, 716ch in AX register.
3. Load mode, attributes and action in BX, CX
and DX registers respectively.
4. Copy file path address in DS:SI register pair.
5. Call int 21h, ISR.
6. Browse the path to find file being created,
c:\newfile.txt.

gi

12a1.asm ALP file creation


; 12a1.asm - file creation
; No CV, direct filename, after MASM & LINK

ASSUME CS:CODE,DS:DATA

is

DATA SEGMENT
filename DB "c:\newfile.txt"
DATA ENDS

ps

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

is

ps

MOV AH,4CH
INT 21H
CODE ENDS
END START

t
gi

mov ax, 716ch


mov bx, 0002h
mov cx, 0000h
mov dx, 0010h
mov si, seg filename
mov ds, si
mov si, offset filename
int 21h

t
gi

Theory: file deletion

Int 21H deletes a file if not already exists, else


ignores, when supplied following parameters:

is

AH=41h, Function delete the existing file


DS=Segment address of filename

ps

SI=Offset of address of filename

t
gi

Algorithm: file deletion

ps

is

1. Provide path and filename for the file to be


deleted.
2. Load function number, 41h in AX register.
3. Copy file path address in DS:SI register pair.
4. Call int 21h, ISR.
5. Browse the path to find file ha has been
deleted, c:\newfile.txt

gi

12a2.asm ALP file deletion


; 12a2.asm file deletion
; No CV, direct filename, after MASM & LINK

is

ASSUME CS:CODE,DS:DATA

DATA SEGMENT
filename DB "c:\newfile.txt"
DATA ENDS

ps

CODE SEGMENT
START:

gi

MOV AX,DATA
MOV DS,AX

is

mov ah, 41h


mov si, seg filename
mov ds, si
mov si, offset filename
mov dx, si
int 21h

ps

MOV AH,4CH
INT 21H
CODE ENDS
END START

t
gi

Procedure:

ps

is

1. Boot up Windows XP, goto command prompt


- change the directory to MASM32
2. Invoke MS DOS editor, type the ALP, save the
program with filename.asm
3. Link the program to create filename.obj
4. Execute the program
5. Observe the results

ps

is

gi

Output Snapshot: file created

ps

is

gi

Output Snapshot: file deleted

t
gi
is

9a.asm

ps

Time display

t
gi

Aim:

is

Read the current time from the system and


display it in the standard format on the screen
(hh:mm:ss.deci-seconds).

Software Used:

ps

Windows XP DOS mode, MASM32 package

t
gi

Theory:

Getting time & displaying is achieved by interrupts.

ps

is

Get DOS time interrupt:


AH=02H function
INT 21H
CH hours, CL minutes
DH seconds
DL 1/100th seconds

t
gi
e

DAA - Decimal Adjust AL after Addition, to


convert hexa to decimal.

ps

is

display procedure converts decimal to ASCII and


displays.

t
is

Display The Current Time is:


Get DOS time by appropriate interrupt
Display HOURS, after decimal to ASCII conversion.
Display :
Display MINUTES, after decimal to ASCII conversion.
Display :
Display SECONDS, after decimal to ASCII conversion.
Display .
Display HUNDREDTH of SECONDS, after decimal to
ASCII conversion.

ps

1.
2.
3.
4.
5.
6.
7.
8.
9.

gi

Algorithm:

t
gi

9a.asm ALP:
assume cs:code,ds:data

lea dx,msg1
mov ah,09h
int 21h

ps

mov ah,2ch
int 21h

is

code segment
start:
mov ax,data
mov ds,ax

data segment
msg1 db 0ah,0dh,"The current time is: $"
data ends

t
gi

Procedure:

ps

is

1. Boot up Windows XP, goto command prompt


- change the directory to MASM32
2. Invoke MS DOS editor, type the ALP, save the
program with filename.asm
3. Link the program to create filename.obj
4. Execute the program
5. Observe the results

is

mov dl,':'
mov ah,02h
int 21h

mov al,cl
add al,00
daa
call display

ps

mov dl,':'
mov ah,02h
int 21h

t
gi

mov al,ch
add al,00
daa
call display

gi

mov al,dh
add al,00
daa
call display

is

mov dl,'.'
mov ah,02h
int 21h
mov al,dl
add al,00
daa
call display

ps

mov ah,4ch
int 21h

gi

display proc near


push cx
push dx

is

mov bl,al
and al,0f0h
mov cl,04h
shr al,cl
add al,30h

ps

mov dl,al
mov ah,02h
int 21h
mov al,bl
and al,0fh
add al,30h

t
gi
is

pop dx
pop cx

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

ret
display endp

ps

code ends
end start

t
gi

Result:

ps

is

HH:MM:SS.XX

Time displayed, in format:

ps

is

gi

Output Screen:

t
gi
is

10a.asm

ps

Decimal Up-counter

t
gi

Aim:

is

Write a program to simulate a Decimal Upcounter to display 0099.

Software Used:

ps

Windows XP DOS mode, MASM32 package

t
gi

Theory:

Two loops help in counting up for unit place and


tens place.

ps

is

Display screen is cleared as well as cursor is


taken to appropriate place, so that, counting
looks like an odometer.

1.
2.
3.

gi

AL 30H, ASCII of 0 (zero), as initially 0 has to be displayed, first digit


0 out of 00. Display first zero 0
Save AX on to stack
BL 30H, ASCII of 0 (zero), as initially 0 has to be displayed, second
digit 0 out of 00.
Display second zero 0
BL BL + 1 Call Delay Procedure, to improve visibility, otherwise fast up
count seen.
Get cursor position
Set cursor position, otherwise display row & column will be random, for
second digit 0 display.
Compare UNITs place, in BL with 39, that is digit 9
If BL < 39, then go to step 5, above.
Set cursor position
Restore AX
AL AL + 1
If AL > 39, then go to step 1
Stop

6.
7.

ps

8.
9.
10.
11.
12.
13.
14.

is

4.
5.

Algorithm:

ps

is

t
gi

Algorithm: Illustrated

ps
e

is

gi

t
gi

10a.asm ALP:
assume cs:code

mov al,30h
loop2: mov dl,al
mov ah,02h
int 21h

ps

push ax
mov bl,30h

is

start:

code segment

t
gi

loop1: mov ah,02h


mov dl,bl
int 21h

mov ah,03h
int 10h

ps

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

is

call delay

inc bl

t
gi

cmp bl,39h

is

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

jle loop1

pop ax
inc al
cmp al,39h
jle loop2

ps

mov ah,4ch
int 21h

t
gi
e

is

delay proc
push cx
push bx
mov cx,0ffh
n3:
mov bx,0ffffh
n4:
dec bx
jnz n4
loop n3
pop bx
pop cx
ret
delay endp

ps

code ends
end start

t
gi

Procedure:

ps

is

1. Boot up Windows XP, goto command prompt


- change the directory to MASM32
2. Invoke MS DOS editor, type the ALP, save the
program with filename.asm
3. Link the program to create filename.obj
4. Execute the program
5. Observe the results

t
gi

Result:

ps

is

Decimal 00 to 99 is up-counted in a odometer


style (format).

ps

is

gi

Output Screen:

t
gi
is

6a.asm

ps

Strings Compare

t
gi

Aim:

is

Read two strings, store them in locations STR1


and STR2.
Check whether they are equal or not and display
appropriate messages.

Software Used:

ps

Windows XP DOS mode, MASM32 package

t
gi

Theory:

Both the strings are compared character by


character once the length are found equal.

ps

is

String matching finds application in text analysis.

t
gi

Algorithm:
For String comparison:

4.

is

3.

Two strings are read and stored in memory str1 & str2.
In successive loops, by comparing each character
with$ both strings lengths are counted (BL & BH).
If lengths of both are equal, then only they are
compared character by character.
Display strings equal or not along with their length.

ps

1.
2.

t
gi

6a.asm ALP:

is

displaymsg macro msg


lea dx,msg
mov ah,09h
int 21h
endm

;-----------displaymsg macro------

;-----------displaylen macro------

ps

displaylen macro len


mov dl,len
add dl,30h
mov ah,02h
int 21h
endm

gi

assume cs:code,ds:data
data segment
str1 db 20 dup(0)
str2 db 20 dup(0)

is

msg1 db 0ah,0dh,"Enter a string - $"


msg2 db 0ah,0dh,"String1 - $"
msg3 db 0ah,0dh,"String2 - $"
msg4 db 0ah,0dh,"Strings are equal - $"
msg5 db 0ah,0dh,"Strings are not equal $"
msg6 db 0ah,0dh,"Length of string1 - $"
msg7 db 0ah,0dh,"Length of string2 - $"

ps

data ends

code segment

gi

start:
mov ax,data
mov ds,ax
displaymsg msg1

is

lea si,str1
call read
displaymsg msg1
lea si,str2
call read

ps

displaymsg msg2
displaymsg str1
displaymsg msg3
displaymsg str2

gi

call check
mov ah,4ch
int 21h

int 21h
cmp al,0dh
jz last
mov [si],al
inc si

is

read proc near


up:
mov ah,01h

;-----------read procedure------begin

jmp up
last:

ps

mov byte ptr[si],'$'


ret
read endp
;-----------read procedure------ends

ps

gi
e

is

check proc near


lea si,str1
lea di,str2
mov bl,00h
mov bh,00h
up1:
cmp byte ptr[si],'$'
jz next1
inc bl
inc si
jmp up1
next1:
cmp byte ptr[di],'$'
jz next2
inc bh
inc di
jmp next1

;-----------check procedure------begin

ps

is

gi

next2:
displaymsg msg6
displaylen bl
;bl=lentgh of string 1
displaymsg msg7
;bh=lentgh of string 2
displaylen bh
cmp bl,bh
jnz fail
lea si,str1
lea di,str2
up2:
cmp byte ptr[si],'$'
jz final
mov al,[si]
cmp al,[di]
;if lengths are equal, compare each
characters in loop
jnz fail
;length times
inc si
inc di
jmp up2

displaymsg msg4
ret
fail:

is

check endp

displaymsg msg5
ret

;-----------check procedure------ends

ps

code ends
end start

gi

final:

t
gi

Procedure:

ps

is

1. Boot up Windows XP, goto command prompt


- change the directory to MASM32
2. Invoke MS DOS editor, type the ALP, save the
program with filename.asm
3. Link the program to create filename.obj
4. Execute the program
5. Observe the results

t
gi

Result:

ps

is

User entered strings matched or not is displayed


on the screen along with length of the both
strings.

ps

is

gi

Output Screen:

t
gi
e
is
ps

EXTRA INFO.

t
gi
ps

is

masm/tasm/fasm/nasm/goasm/hla
MOV AL, 1h ; Load AL with immediate value 1
MOV AL, 61h ; Load AL with 97 decimal (61 hex)
10110000 01100001
B0 61

ps

is

gi

8086 Microprocessor Block Diagram

BACK

ps

BACK

is

gi

ps
e

is

gi

is

gi

Binary &
Hexadecimal
Numbers

ps

BACK

gi

Source Program ALP

mov al,number1
add al,number2
mov sum,al

ps

mov ah,4ch
int 21h
code ends
end start

is

assume cs:code,ds:data
data segment
number1 db 04
number2 db 05
sum db ?
data ends
code segment
start:
mov ax,data
mov ds,ax

Source File ALP

t
gi

EXE File

ps

is

EXE file

BACK

(Data Transfer)

;Convert Byte to Word AL AX


;Convert Word to Double in AX DX,AX
;Input
;Load AH from Flags
;Load pointer to DS
;Load EA to register
;Load pointer to ES
;Load memory at SI into AX
;Move
;Move memory at SI to DI
;Output
;Pop
;Pop Flags
;Push
;Push Flags
;Store AH into Flags
;Store AX into memory at DI
;Exchange
;Translate byte to AL

is

ps

CBW
CWD
IN
LAHF
LDS
LEA
LES
LODS
MOV
MOVS
OUT
POP
POPF
PUSH
PUSHF
SAHF
STOS
XCHG
XLAT

gi

x86 Instruction Set Summary

(Arithmetic/Logical)

;ASCII Adjust for Add in AX


;ASCII Adjust for Divide in AX
;ASCII Adjust for Multiply in AX
;ASCII Adjust for Subtract in AX
;Add with Carry
;Add
;Logical AND
;Complement Carry
;Compare
;Compare memory at SI and DI
;Decimal Adjust for Add in AX
;Decimal Adjust for Subtract in AX
;Decrement
;Divide (unsigned) in AX(,DX)
;Divide (signed) in AX(,DX)
;Multiply (unsigned) in AX(,DX)
;Multiply (signed) in AX(,DX)
;Increment

is

ps

AAA
AAD
AAM
AAS
ADC
ADD
AND
CMC
CMP
CMPS
DAA
DAS
DEC
DIV
IDIV
MUL
IMUL
INC

gi

x86 Instruction Set Summary

Cont.)

ps

;Negate
;Logical NOT
;Logical inclusive OR
;Rotate through Carry Left
;Rotate through Carry Right
;Rotate Left
;Rotate Right
;Shift Arithmetic Right
;Subtract with Borrow
;Scan memory at DI compared to AX
;Shift logical/Arithmetic Left
;Shift logical Right
;Subtract
;AND function to flags
;Translate byte to AL
;Logical Exclusive OR

is

NEG
NOT
OR
RCL
RCR
ROL
ROR
SAR
SBB
SCAS
SHL/SAL
SHR
SUB
TEST
XLAT
XOR

gi

x86 Instruction Set Summary (Arithmetic/Logical

(Control/Branch Cont.)

ps

;Call
;Clear Carry
;Clear Direction
;Clear Interrupt
;Escape (to external device)
;Halt
;Interrupt
;Interrupt on Overflow
;Interrupt Return
;Jump on Below/Not Above or Equal
;Jump on Below or Equal/Not Above
;Jump on CX Zero
;Jump on Equal/Zero
;Jump on Less/Not Greater or Equal
;Jump on Less or Equal/Not Greater
;Unconditional Jump
;Jump on Not Below/Above or Equal
;Jump on Not Below or Equal/Above
;Jump on Not Equal/Not Zero
;Jump on Not Less/Greater or Equal

is

CALL
CLC
CLD
CLI
ESC
HLT
INT
INTO
IRET
JB/JNAE
JBE/JNA
JCXZ
JE/JZ
JL/JNGE
JLE/JNG
JMP
JNB/JAE
JNBE/JA
JNE/JNZ
JNL/JGE

gi

x86 Instruction Set Summary

(Control/Branch)

ps

;Jump on Not Less or Equal/Greater


;Jump on Not Overflow
;Jump on Not Parity/Parity Odd
;Jump on Not Sign
;Jump on Overflow
;Jump on Parity/Parity Even
;Jump on Sign
;Bus Lock prefix
;Loop CX times
;Loop while Not Zero/Not Equal
;Loop while Zero/Equal
;No Operation (= XCHG AX,AX)
;Repeat/Repeat Not Equal/Not Zero
;Repeat Equal/Zero
;Return from call
;Segment register
;Set Carry
;Set Direction
;Set Interrupt
;AND function to flags
;Wait

is

JNLE/JG
JNO
JNP/JPO
JNS
JO
JP/JPE
JS
LOCK
LOOP
LOOPNZ/LOOPNE
LOOPZ/LOOPE
NOP
REP/REPNE/REPNZ
REPE/REPZ
RET
SEG
STC
STD
STI
TEST
WAIT

gi

x86 Instruction Set Summary

gi

Assembler Directives

end of program, label is entry point

proc far|near

begin a procedure; far, near keywords


specify if procedure in different code
segment (far), or same code segment (near)

endp

end of procedure

page

set a page format for the listing file

title

title of the listing file

.code

mark start of code segment

.data

mark start of data segment

.stack

set size of stack segment

ps

is

end label

t
gi

Memory
Memory

Memory Content

Number1

4000H

04

Number2

4001H

05

msg1

4002H

msg2

4003H

4005H

ps

is

Memory Address (Hex)

Memory Location
Address Label

BACK

t
gi

Procedure:

ps

is

Boot up Windows XP, goto command prompt change the directory to MASM32
Invoke MS DOS editor, type the ALP, save the
program with filename.asm
Link the program to create filename.obj
Load filename.obj in CodeView
debugger(loader)
Execute the program
Observe the results

t
gi
list
CX

BX

AL

SI-1

CARRY

SI

SI+1

SI+2

SI+3

SI+4

mov cx,count

next: mov al,[si]

;point to list with SI

;load AL with element pointed by SI

inc si

;point to next element

cmp al,[si]

;compare first two elements

jc down

;if first < next, go to down OR

xchg al,[si]

;if next > first, exchange them

mov [si-1],al

;take next element in SI

;decrement counter

jnz next

;if not zero, go to next

loop next1

;if zero, go to next1

mov ah,4ch

down: dec bx

ps

int 21h

;terminate

mov si,offset list

;load count in BX

is

next1: mov bx,cx

5>1

t
gi
list
CX BX AL SI-1
4

mov cx,count

next1: mov bx,cx


;load count in BX
mov si,offset list ;point to list with SI
;load AL with element pointed by SI
;point to next element
;compare first two elements
;if first < next, go to down OR
;if next > first, exchange them
;take next element in SI

down: dec bx
jnz next
loop next1

;decrement counter
;if not zero, go to next
;if zero, go to next1

is

next: mov al,[si]


inc si
cmp al,[si]
jc down
xchg al,[si]
mov [si-1],al

;terminate

ps

mov ah,4ch
int 21h

5 1 4 2 8
SI SI+1 SI+2 SI+3 SI+4

5>1 0
1

5
1

5
5

4
4

2
2

8
8

t
gi
list
CX

SI-1

SI

SI+1

SI+2

SI+3

SI+4

;load count in BX

;point to list with SI

next: mov al,[si]

;load AL with element pointed by SI

inc si

;point to next element

cmp al,[si]

;compare first two elements

jc down

;if first < next, go to down OR

xchg al,[si]

;if next > first, exchange them

mov [si-1],al

;take next element in SI

jnz next

;if not zero, go to next


;if zero, go to next1

ps

loop next1

mov ah,4ch
int 21h

;decrement counter

down: dec bx

;terminate

is

mov si,offset list

AL

mov cx,count
next1: mov bx,cx

BX

5>1

ps

7a
11a
9a
2a
5a
4a
8a

gi

Plan sequence

is

1. 7a
2. 1a
3. 3a
4. 2a
5. 11a
6. 5a
7. 8a
8. 4a
9. 12a
10.9a
11.10a
12.6a

t
gi
4A

0A

04

10

34

65

ps

40

is

65

30
10

ADD 37 48+10+7

Potrebbero piacerti anche