ASM Programs
ASM 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
MOV AX,4C00H
INT 21H
END
___________________________________________________________________
Program # 2
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
Lab Assignment:
.MODEL SMALL
.DATA X EQU ’B’
Y DB 43H
.STACK 200
.CODE
MOV AX,@DATA
MOV DS,AX
END
______________________________________________________________
.MODEL SMALL
.DATA
MESSAGE DB ‘This is the message to be displayed’,’$’
.STACK 200
.CODE
MOV AX,@DATA
MOV DS, AX
.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
Memory
Model Size of Code and Data
Stack Directive:
Memory allocation:
Code Segment :
- Directive is .code for code segment
- The "program" resides here
End of Program:
- Directive is End
Note:
The sequence of instructions at the beginning of a program used to assign the data
segment:
MOV AX,@DATA
MOV DS,AX
.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.
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
.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,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
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).
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
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 -
1 / To put two decimal digits into the same byte use the following:
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
END
Fill in table 5.3 while running the above program using CodeView.