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

ASM Programs

The document provides example assembly language programs for students in the MPES course, including detailed explanations of each program's functionality and relevant assembly instructions. It also includes lab assignments that require students to write programs for specific tasks, such as password input and string manipulation. Additionally, the document covers addressing modes, memory models, and data manipulation techniques in assembly language.

Uploaded by

Saleem Momin
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)
7 views

ASM Programs

The document provides example assembly language programs for students in the MPES course, including detailed explanations of each program's functionality and relevant assembly instructions. It also includes lab assignments that require students to write programs for specific tasks, such as password input and string manipulation. Additionally, the document covers addressing modes, memory models, and data manipulation techniques in assembly language.

Uploaded by

Saleem Momin
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/ 19

Example Programs

Subject : MPES
Semester: Spring 2003
Instructor: Muhammad Sabih

Following are the Example programs for the students taking MPES in Spring2003. These
programs are well explained using tables and comments.
Refer to the Handout (the two chapter handout distributed in 7th April Class).
In case of difficulty you can email me at following address:
[email protected]
Program # 1

; The following lines are just comments, they may be omitted,


; However, they are very useful.

TITLE “A simple program”


.MODEL SMALL
.STACK 32
.CODE
MOV AX,2000
MOV BX,2000H
MOV CX,10100110B
MOV DX, -4567
MOV AL,'A'
MOV AH,'a'

MOV AX,4C00H
INT 21H
END
___________________________________________________________________

Program # 2

TITLE "Our second program".MODEL SMALL.STACK 32.DATA


MULT1 EQU 25
MULT2 DW 5.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,00
MOV BX,MULT1
MOV CX,MULT2
MULT: ADD AX,BX
DEC CX
JNZ MULT
MOV DX,AX
MOV AX,4C00H
INT 21H
END
Input and Output

Function Input in Output in Effect


01H AH AL Read a character with echo on the
screen.
02H No output Display a character on the screen
AH,

Character in DL
08H AH AL Read character without echo.
09H AH No output Display a string terminated by a
‘$’ sign
0AH AH Offset in Read a string of characters from
DX the keyboard

Function 0AH Read from Keyboard


Entry AH = 0AH ; DX = address of keyboard input buffer
First byte of buffer contains the size of the buffer (up to 255)
Exit Second byte of buffer contains the number of characters read.
Reading operation continues until buffer full, or a carriage return
(CR = 0DH) is typed.

Lab Assignment:

Write an assembly language program that prompts you to enter a password of 3


characters in length. The password should not be echoed to the screen. The program then
displays your name and ID number on the screen.

Submit your work at the end of the lab.


Title “Exp. 2 Prog. 1”
; This program displays the characters A B C, using INT 21H function 02.

.MODEL SMALL
.DATA X EQU ’B’
Y DB 43H

.STACK 200

.CODE

MOV AX,@DATA
MOV DS,AX

MOV AH,02 ; LOAD FUNCTION 02


MOV DL,’A’ ; LOAD CHARACTER TO BE DISPLAYED
INT 21H ; CALL INTERRUPT 21H

MOV DL,X ; LOAD CHARACTER TO BE DISPLAYED


INT 21H ; CALL INTERRUPT 21H

MOV DL,Y ; LOAD CHARACTER TO BE DISPLAYED


INT 21H ; CALL INTERRUPT 21H

MOV AX,4C00H; Exit to DOS


INT 21H

END
______________________________________________________________

Title “Exp. 2 Prog. 2”


; This program displays a string terminated by a $ sign using INT 21H function 09H.

.MODEL SMALL

.DATA
MESSAGE DB ‘This is the message to be displayed’,’$’

.STACK 200

.CODE
MOV AX,@DATA
MOV DS, AX

MOV DX, OFFSET MESSAGE


MOV AH, 09H
INT 21H

MOV AX, 4C00H ; Exit to DOS


INT 21H
END
; Character input with echo INT 21H, function call 01H
; Character input without echo INT 21H, function call 08H

Title “Exp. 2 Prog. 3”

.MODEL SMALL
.DATA
MESSAGE DB ‘Enter a character: ’,0DH, 0AH,’$’
MESSAGE2 DB ‘The character you typed is: ’,0DH, 0AH,’$’

.STACK 200

.CODE

MOV AX,@DATA
MOV DS,AX

LEA DX, MESSAGE


MOV AH,09H
INT 21H ; Display message

MOV AH, 01H ; Function read character


INT 21H ; Character read is returned in AL register and echoed to the screen

MOV AH,02 ; Function 02H, display character


MOV DL,AL ; Load character to be displayed INT 21H ;

LEA DX, MESSAGE


MOV AH,09H
INT 21H

MOV AH, 08H ; Function read character without echo.


INT 21H ; Character read is returned in AL register. No echo on the display.

MOV BL,AL ;Save character read in BL register

LEA DX, MESSAGE2


MOV AH,09H ;Display MESSAGE2
INT 21H

MOV AH,02 ; Function 02H, display character


MOV DL,BL ; Load character to be displayed from BL INT 21H

MOV AH,4CH ; Exit to DOS


INT 21H
END
Segmentation and Addressing Modes

Addressing Source operand


Mode Assuming: DS = 1000H, BX = 0200H, SI = 0300H
Address
Example Type Address Generation
Register MOV AX,BX Register - -
Immediate MOV AX, 0F7H Immed. - -
Direct MOV AX,[1234H] Memory DS x 10H +1234H 11234H
Register- MOV AX,[BX] Memory DS x 10H +0200H 10200H
Indirect
Based MOVAX,[BX+06] Memory DS x 10H +0200H + 0006H 10206H
Indexed MOVAX,[SI+06] Memory DS x 10H +0300H + 0006H 10306H
Based-Indexed MOV AX,[BX+SI+06] Memory DS x 10H +0200H +0300H + 0006H 10506H

Memory
Model Size of Code and Data

TINY Code and data no more than 64KB combined


SMALL Code and data segments must be no more than 64KB each
MEDIUM Code can be more than 64KB, data still limited to no more than
64KB
COMPACT Code limited to no more than 64KB, data can be more than
64KB
LARGE Code and data can each be more than 64K, no array can be
larger than 64KB
HUGE Code and data can each be more than 64KB, arrays can be
larger than 64KB

Stack Directive:

- Directive is .stack for stack segment


- Should be declared even if program itself doesn't use stack
needed for subroutine calling (return address) and possibly
passing parameters
- May be needed to temporarily save registers or variable content
- Will be needed for interrupt handling while program is running

Memory allocation:

- Directive is .data for data segment


- All variables must be declared, and memory space for each
allocated.
- Data definition directive can be followed by a single value, or a
list of
values separated by commas
- Different data definition directives for different size types of
memory
1. DB - define byte (8 bits)
2. DW - define word (16 bits)
3. DD - define double word (32 bits)
4. DQ - define quad word (64 bits)

Code Segment :
- Directive is .code for code segment
- The "program" resides here

End of Program:

- Directive is End

- Tells assembler that this is the end of the program

Note:

The sequence of instructions at the beginning of a program used to assign the data
segment:

MOV AX,@DATA
MOV DS,AX

May be replaced by the following directive:

.STARTUP

which assigns both DATA and CODE segments, and hence no warning will be issued by the
assembler. However, it should be noted that the program will start at address CS:0017h. The
bytes CS:0000 to CS:0017 are occupied by the Startup directive.

Identically, the sequence used to terminate and exit to DOS can be replaced by the
directive .EXIT which has exactly the same effect.

Lab Assignment:

Write a program that prompts the user to enter a string, in capital letters, of a maximum
length of 20 characters. Read the string in capital letters and convert it to small letters. Then
display the new string.
Note:
To convert a capital letter to a small one, use the following instruction:

;Read character
MOV AL, character_read
ADD AL, 20H
; Display character in AL register

Use the following to loop through the string you just entered.

MOV CX, Number of bytes read


Again: Start loop here
;Convert to small letters.

LOOP Again

; This program displays a string terminated by a $ sign using INT 21H function 09H.
TITLE “Program 3-1”
.MODEL SMALL

.STACK 200
.DATA
MESSAGE DB ‘This is the message to be displayed: ’, ‘$’
MESSAGE2 DB ‘The message you just entered : ;’ , ‘$’
BUF DB 10 ; Number of characters to be read
DB 10 DUP(?); Reserve 10 bytes for string
.CODE
MOV AX,@DATA
MOV DS,AX

LEA DX,MESSAGE
MOV AH,09H
INT 21H
MOV AH, 0AH
MOV DX, OFFSET BUF
INT 21H

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

LEA DX, BUF


MOV AH,09H
INT 21H
MOV AX,4C00H
INT 21H
END
TITLE “PROGRAM 2 EXPERIMENT 3”
; This program displays a message and reads a new message from the keyboard

.MODEL SMALL
.STACK 200
.DATA
CRLF DB 0DH,0AH,'$'
PROMPT DB 'Enter a name of max. length 30 char.: ',0DH,0AH,'$'
STRING1 DB 'Mr. ','$'
STRING2 DB ' studies 8086 programming. ','$'

; Allocate 32 bytes for BUFFER, and put the value 31 in the second byte.
BUFFER DB 31H,32 DUP(?)

.CODE
.STARTUP ;This directive initializes the DS and CS segments.
LEA DX,PROMPT ;display prompt
MOV AH,09H
INT 21H

MOV AH,0AH ;read into buffer


LEA DX, BUFFER
INT 21H

LEA DX, CRLF ;move cursor to next line


MOV AH,09H
INT 21H

LEA DX,STRING1 ;display string1


MOV AH,09H
INT 21H

;now display the buffer i.e. what has been read.

MOV AH,09H
MOV BH,00H
MOV BL,BUFFER[1] ;move in bl buffer length
MOV BUFFER[BX+2],'$' ;put a $ sign at the end of buf
LEA DX,BUFFER[2] ;load actual length of buffer
INT 21H

LEA DX,STRING2 ;display string2


MOV AH,09H
INT 21H

LEA DX, CRLF ;move cursor to next line


MOV AH,09H
INT 21H

MOV AH, 02H ; display nubmer of characters read if less than 10


MOV DL,BUFFER[1] ; read second byte of buffer
ADD DL, 30H ; convert to number
INT 21H
MOV AX,4C00H
INT 21H
END
Experiment 3, Program #1:

Source Destination Addressing


Mode
Instruction Address/ Content Address/ Contents
Register Register Before After
MOV AX,@DATA
MOV DS,AX
LEA DX,MESSAGE
MOV AH,09H
INT 21H Before INT 21H, IP = After INT 21H, IP =
MOV AH, 0AH
MOV DX, OFFSET
BUF
INT 21H Before INT 21H IP = After INT 21H IP =
LEA DX,MESSAGE2
MOV AH,09H
INT 21H Before INT 21H IP = After INT 21H IP =
LEA DX, BUF
MOV AH,09H
INT 21H Before INT 21H IP = After INT 21H IP =
MOV AX,4C00H
INT 21H S k i p
Experiment 3, Program #2:

Addressing Modes
Instructions
Source Destination
LEA DX,PROMPT
MOV AH,09H
INT 21H
MOV AH,0AH
LEA DX, BUFFER
INT 21H
LEA DX, CRLF
MOV AH,09H
INT 21H
LEA DX,STRING1
MOV AH,09H
INT 21H
MOV AH,09H
MOV BH,00H
MOV BL,BUFFER[1]
MOV BUFFER[BX+2],'$'
LEA DX,BUFFER[2]
INT 21H
LEA DX,STRING2
MOV AH,09H
INT 21H
LEA DX, CRLF
MOV AH,09H
INT 21H
MOV AH, 02H
MOV DL,BUFFER[1]
ADD DL, 30H
INT 21H
MOV AX,4C00H
INT 21H
Indexing, Data Manipulation Introduction:
Arithmetic Instructions:

Use Turbo Debugger (TD) to check the flag bits affected by the instructions
Addition
ADD
ADC
INC
DAA
Subtraction
SUB
SBB
DEC
DAS
NEG
Multiplication
MUL
IMUL
Division
DIV
IDIV
Sign Extension
CBW
CWD
Notes:
The DAA (Decimal Adjust after Addition) instruction allows addition of numbers
represented in 8-bit packed BCD code. It is used immediately after normal addition
instruction operating on BCD codes. This instruction assumes the AL register as the
source and the destination, and hence it requires no operand. The effect of DAS (Decimal
Adjust after Subtraction) instruction is similar to that of DAA, except the fact that it is
used after performing a subtraction.CBW and CWD are two instructions used to facilitate
division of 8 and 16 bit signed numbers. Since division requires a double-width dividend,
CBW converts an 8-bit signed number (in AL), to a word, where the MSB of AL register
is copied to AH register. Similarly, CWD converts a 16-bit signed number to a 32-bit
signed number (DX,AX).

Note on the use of arrays:


The DB and DW directives are respectively used to declare a variable of size byte or
word. The following declaration defines a variable X of size byte and assigns it the value
10H.
X DB 10H
Identically the following will define a variable of size word, and assigns it the value
13EFH:
Y DW 13EFH
The DUP directive may be used to reserve more than one consecutive data item and
initialize reserved items to the same value. For example, the instruction:
ByteArray DB 100 DUP(0)
Instructs the assembler to reserve an array of 100 bytes, and initializes each byte to the
value zero. If the “0” in the above declaration is replaced with “?”, the assembler will not
initialize the bytes of the array to any value.
To access the different elements of an array, we use one of the following addressing
modes.
Based addressing mode.
Indexed addressing mode.
Based-Indexed addressing mode.
The Based-Indexed addressing mode may be used to access a two-dimensional array.
Here are examples of each case.
Array1 DB 0,1,2,3,4,5,6,7,8,9
Array2 DB 10 DUP(0)
Array3 DB 11,12,13,21,22,23,31,32,33
RowSize EQU 3
Based addressing mode:
MOV BX, OFFSET Array1 ; Address Array1
MOV AL,[BX+4] ; Access 5th element of Array1
Indexed addressing mode:
MOV DI, OFFSET Array2 ; Address Array2
MOV [DI+6],AL ; Copy to 7th element of Array2
MOV SI,3
MOV Array2[SI],AL ;Copy to 4th element of Array2
Based-Indexed addressing mode:
MOV BX, OFFSET Array3 ; Address Array3
MOV SI,1*RowSize ; Beginning of 2nd row
MOV DI,2*RowSize ; Beginning of 3rd row
MOV AL, [BX+SI+1] ; Access 2nd element of 2nd row
MOV [BX+DI+2],AL ; Access 3rd element of 3rd row
Remark:Notice that row R, has index (R-1), and element n has index (n-1).

Lab Assignment: Write a program that prompts the user to enter two numbers of 4 digits
each. Then the program calculates the quotient and remainder of the division of the two
numbers. The two numbers are entered as two one-dimensional arrays of size four (4).
TITLE "PROGRAM 1 EXPERIMENT 4"
; This program reads two numbers from the keyboard and
; gives their sum. This program uses internal registers
; to store the variables.

.MODEL SMALL
.STACK 200
.DATA CRLF DB 0DH,0AH,'$'
PROMPT1 DB 'Enter the first positive integer: ','$'
PROMPT2 DB 'Enter the second positive integer: ','$'
PROMPT3 DB 'The sum of the two numbers is: ','$'
.CODE
.STARTUP
LEA DX,PROMPT1 ;DISPLAY PROMPT1
MOV AH,09H
INT 21H
MOV AH,01H ;READ FIRST NUMBER
INT 21H
SUB AL,30H ;Convert character to number
MOV CL,AL ;SAVE THE NUMBER IN CL
LEA DX,CRLF ;MOVE CURSOR TO NEXT LINE
MOV AH,09H
INT 21H
LEA DX,PROMPT2 ;DISPLAY PROMPT2
MOV AH,09H
INT 21H
MOV AH,01H ;READ SECOND NUMBER
INT 21H
SUB AL,30H ;Convert character to number
ADD AL,CL ;PERFORM ADDITION AND SAVE RESULT IN CL
MOV CL,AL
ADD CL,30H ;CONVERT DIGIT TO CHARACTER
LEA DX,CRLF ;MOVE CURSOR TO NEXT LINE
MOV AH,09H
INT 21H
LEA DX,PROMPT3 ;DISPLAY PROMPT3
MOV AH,09H
INT 21H
MOV DL,CL ;DISPLAY SUM
MOV AH,02H
INT 21H
.EXIT
END
TITLE "PROGRAM 1 EXPERIMENT 4"
; This program reads two numbers from the keyboard and
; displays their sum. This program uses the memory to; store the variables.
.MODEL SMALL
.STACK 200.DATA
CRLF DB 0DH,0AH,'$'
PROMPT1 DB 'Enter the first positive integer: ','$'
PROMPT2 DB 'Enter the second positive integer: ','$'
PROMPT3 DB 'The sum of the two numbers is: ','$'
NUM1 DB ?
NUM2 DB ?
RES DB ?

.CODE
.STARTUP
LEA DX,PROMPT1 ;DISPLAY PROMPT1
MOV AH,09H
INT 21H
MOV AH,01H ;READ FIRST NUMBER
INT 21H
SUB AL,30H ;Convert character to number
MOV NUM1,AL ;SAVE NUM1
LEA DX,CRLF ;MOVE CURSOR TO NEXT LINE
MOV AH,09H
INT 21H
LEA DX,PROMPT2 ;DISPLAY PROMPT2
MOV AH,09H
INT 21H
MOV AH,01H ;READ SECOND NUMBER
INT 21H
SUB AL,30H ;Convert character to number
MOV NUM2,AL ;SAVE NUM2
ADD AL,NUM1 ;PERFORM ADDITION
MOV RES,AL ;SAVE RESULT IN RES
LEA DX,CRLF ;MOVE CURSOR TO NEXT LINE
MOV AH,09H
INT 21H
LEA DX,PROMPT3 ;DISPLAY PROMPT3
MOV AH,09H
INT 21H ;DISPLAY SUM
MOV DL,RES ;RETREIVE RES FROM MEMORY
ADD DL,30H ;CONVERT DIGIT TO CHARACTER
MOV AH,02H
INT 21H
.EXIT
END
Logic InstructionsIntroduction

Flags
Instruction Example Meaning
OF SF ZF AF PF CF

AND AND AX, FFDFH AX ← AX AND FFDFH 0 * * ? * 0

OR OR AL, 20H AL ← AL OR 20H 0 * * ? * 0

XOR XOR NUM1, FF00 [NUM1]←[NUM1]XOR FF00 0 * * ? * 0

NOT NOT NUM2 [NUM2]←[ NUM2] - - - - - -

Operation Effect
b AND 0 = 0 Reset the bit
b OR 1 = 1 Set the bit
b XOR 1 = b Complement the bit
b XOR 0 = b -

Byte manipulations for reading and displaying purposes:

1 / To put two decimal digits into the same byte use the following:

MOV AH, 01H


INT 21H
SUB AL, 30H
MOV CH, AL ; Read high digit e.g. 8

MOV AH, 01H


INT 21H
SUB AL, 30H
MOV CL, AL ; Read low digit e.g. 3

MOV AL, 10000B ; Use MUL by 10000B to shift left by 4 bits


MUL CH ; Shift AL 4 bits to the left
XOR AH, AH ; Clear AH
OR AL, CL ; Result in AL ç 83

If we want to perform addition:

; If AL contains the first number in BCD format


; and CL contains the second number in BCD format
ADD AL, CL
DAA ; Decimal adjust
; New result in AL in BCD format
MOV CL, AL
; Number in CL register. See next how to display it as decimal number.

2 / To display a number in BCD format use the following:

; The number is in the CL register:


MOV AL, CL ; Move CL to AL
XOR AH, AH ; Clear AH
MOV BL, 10000B
DIV BL ; Shift AX 4 bits to the right
AND AL, 0FH ; Clear 4 high nibbles of AL
ADD AL, 30H ; Convert to character
; Now Display AL as high digit first

MOV AL, CL ; Read number again


AND AL, 0FH ; Clear 4 high nibbles of AL
ADD AL, 30H ; Convert to character
; Now Display AL as low digit second
Displaying Data in any Number Base r:

The basic idea behind displaying data in any number base is division. If a binary number is divided
by 10, and the remainder of the division is saved as a significant digit in the result, the remainder
must be a number between zero and nine. On the other hand, if a number is divided by the radix r,
the remainder must be a number between zero and (r-1). Because of this, the resultant remainder
will be a different number base than the input which is base 2. To convert from binary to any other
base, use the following algorithm.

Algorithm:

1. Divide the number to be converted by the desired radix (number base r).
2. Save the remainder as a significant digit of the result.
3. Repeat steps 1 and 2 until the resulting quotient is zero.
4. Display the remainders as digits of the result.
Note that the first remainder is the least significant digit, while the last
remainder is the most significant one.

Lab Assignment:

Write a program that reads two numbers of 2 digits each, converts them to binary inside
the internal registers. Multiply the two numbers using a simple MUL operation, and
display the result in decimal format.

To ease bit manipulation and shifting, use division and multiplication by 2, to perform
right shift and left shift.
TITLE “Program 5.1: Logic Instructions”
; This program shows the effect of the logic instructions

.MODEL SMALL
.STACK 200
.DATA
NUM1 DW 0FA62H
NUM2 DB 94H
.CODE
.STARTUP

MOV AX, NUM1 ;load AX with number NUM1


AND AX, 0FFDFH ;Reset 6th bit of AX
OR AL, 20H ;Set 6th bit of AL
XOR NUM1, 0FF00H ;Complement the high order byte of
; NUM1
NOT NUM2 ;Complement NUM2
XOR AX, AX ;Clear AX
MOV AX, NUM1
AND AX, 0008H ; Isolate bit 4 of NUM1
XOR AX, 0080H ;Complement 4th bit of AX
.EXIT

END

Fill in table 5.3 while running the above program using CodeView.

Destination Content Status Flags


Statement
Before After O D I S Z A P C
F F F F F F F F
1. MOV AX, NUM1
2. AND AX, 0FFDFH
3. OR AL, 20H
4. XOR NUM1, 0FF00H
5. NOT NUM2
6. XOR AX, AX
7. MOV AX, NUM1
8. AND AX, 0008H
9. XOR AX, 0080H
TITLE “Lab Exp. # 5 Program # 5.2”
; This program converts a number NUM from Hexadecimal,
; to a new numbering base (RADIX).
.MODEL SMALL
.STACK 200
.DATA
RADIX DB 10 ;radix: 10 for decimal
NUM DW 0EFE4H ;the number to be converted
;put here any other number.
;Note that: 0EFE4H = 6141210
TEMP DB 10 DUP(?) ;Used to simulate a stack
.CODE
.STARTUP
MOV AX, NUM ;load AX with number NUM
;display AX in decimal
MOV CX, 0 ;clear digit counter
XOR BH, BH ;clear BH
MOV BL, RADIX ;set for decimal
XOR SI, SI ;Clear SI registerDISPX1:
MOV DX, 00 ;clear DX
DIV BX ;divide DX:AX by 10
MOV TEMP[SI], DL ;save remainder
INC SI
INC CX ;count remainder
OR AX, AX ;test for quotient of zero
JNZ DISPX1 ;if quotient is not zero
DEC SIDISPX2:
MOV DL, TEMP[SI] ;get remainder
MOV AH, 06H ;select function 06H
ADD DL, 30H ;convert to ASCII
INT 21H ;display digit
DEC SI
DEC CX ;repeat for all digits
JNZ DISPX2
.EXIT ;exit to dos
END

You might also like