Assembly Language Programming lab
Assembly Language Programming lab
1
Learning any imperative programming language involves mastering a
number of common concepts:
Variables: Declaration/definition
Assignment: Assigning values to variables
Input/output: Displaying messages/displaying variable values
Control flow: Loops, JUMPs
2
Variables
3
Assignment
In programming languages such as C/C++/Java, assignment
takes the form:
x = 42 ;
y = 24;
z = x + y;
In assembly language we carry out the same operation but we use
an instruction to denote the assignment operator (“=”). The
above assignments would be carried out in 8086 assembly
langauge as follows:
Mov ax, 42
Add ax, 24
Mov bx,ax
4
The mov instruction carries out assignment.
It allows us to place a number in a register or in a memory
location.
Example:
mov bx, ‘A’ ➔To store the ASCII code for the letter A in
register bx.
mov bx, 2 ➔ Loads the value 2 in to bx
5
Input/output
In 8086 assembly language, we do not call operating system subprograms
by name, instead, we use a software interrupt mechanism
The 8086 INT instruction generates a software interrupt.
It uses a single operand which is a number indicating which MS-DOS
subprogram is to be invoked.
6
For I/O, the number used is 21h. Thus, the instruction INT 21h
transfers control to the operating system, to a subprogram that
handles I/O operations.
This subprogram handles a variety of I/O operations by calling
appropriate subprograms.
This means that you must also specify which I/O operation
(e.g. read a character, display a character,…) you wish to
carry out.
This is done by placing a specific number in a specific register.
7
The ah register is used to pass this information.
For example, the subprogram to display a character is
subprogram number 2h.
This number must be stored in the ah register.
When the I/O operation is finished, the interrupt service
program terminates and our program will be resumed.
8
Character Output
There are three elements involved in carrying out this operation using
the INT instruction:
We specify the character to be displayed. This is done by storing the
character’s ASCII code in a specific 8086 register.
In this case we use the dl register, i.e. we use dl to pass a parameter to
the output subprogram.
We specify which of MS-DOS’s I/O subprograms we wish to use. The
subprogram to display a character is subprogram number 2h. This
number is stored in the ah register.
We request MS-DOS to carry out the I/O operation using the INT
instruction.
This means that we interrupt our program and transfer control to
the MS-DOS subprogram that we have specified using the ah register.
9
Program 1
Example : Write a code fragment to display the character ‘a’ on the
screen:
10
Program 2
Write a code fragment to display the character ‘h’ , ‘e’ ,’l’, ’l’, ’o’, ’w’ on the screen:
org 100h
mov dl, ‘h' ; dl = ‘h’
mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character
mov dl, ‘e' ; dl = ‘e’
mov ah, 2h ; character output subprogram
int 21h
mov dl, ‘l'
; call ms-dos, output character
; dl = ‘l’
Output:
mov ah, 2h
int 21h
; character output subprogram
; call ms-dos, output character
hellow
mov dl, ‘l' ; dl = ‘l’
mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character
mov dl, ‘o' ; dl = ‘o’
mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character
mov dl, ‘w' ; dl = ‘w’
mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character
11
ret
Program 3
Write a code fragment to display the number ‘1’ , ‘2’ ,’3’, ’4’, ’5’, ’6’ on the screen:
org 100h
mov dl, ‘1' ; dl = ‘1’
mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character
mov dl, ‘2' ; dl = ‘2’
mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character
13
Program 4
Example: Write a code fragment to read a character from
the keyboard
14
Program 5
Write a code fragment to read a 7 character s or 7 numbers or any 7
alphanumeric from the keyboard
org 100h
mov ah, 1h
int 21h
mov ah, 1h
int 21h
15 ret
Program 6
Example: Reading and displaying a character:
16
Like carrying out an I/O operation, termination of a program is
accomplished by using the INT instruction.
This time MS-DOS subprogram number 4c00h is used and is stored in
register AX.
It is the subprogram to terminate a program and return to MS-DOS.
Hence, the instructions:
mov ax, 4c00h ; Code for return to MS-DOS
int 21H ; Terminates program and return to MS-DOS.
17
Program 7:
➢ A complete program to display the letter ‘a’ on the screen:
.model small
.stack 100h Output:
.code a
start:
mov dl, 'a' ;stor ASCII code of 'a' in dl
mov ah, 2h ;ms-dos character output function
int 21h ;displays character in dl register
mov ax, 4c00h ;return to ms-dos
int 21h
end start
18
19
Program 8:
Write a program to load character ‘ ? ’ into register ax and
display the same on the screen.
20
.model small
.stack Output:
.code ?
start:
mov ax, ‘?' ;stor ASCII code of ‘?' in ax
mov dl, al
mov ah, 2h ;ms-dos character output function
int 21h ;displays character in dl register
mov ax, 4c00h ;return to ms-dos
int 21h
end start
21
String output
22
Procedure
23
24
25
Program 9 : A program to print “AUWC department CS microprocessor lab” on screen
.model small
.stack
.data
Message db "AUWC department of CS microprocessor lab.$"
.code
main proc
mov ax, seg Message ;moves the segment "Message" into AX
mov ds, ax ;moves ax into ds(ds=ax)
mov ah,9 ;func 9 of dos interrupt 21h prints the string that terminate with "&".
int 21h
mov ah,9 ;func 9 of dos interrupt 21h prints the string that terminate with "&".
int 21h
mov ah,9
int 21h
mov ah,1h
✓ 13 is the decimal
int 21h value of CR ASCII
code (carriage
mov [bx],al
mov dx,offset Disp return)
mov ah,9 ✓ 10 is the decimal
int 21h
value of LF ASCII
mov dl,[bx] code (line feed)
mov ah,2h
int 21h
31
❖ ASCII representation
o Numbers are stored as a string of ASCII characters
» Example: 1234 is stored as 31 32 33 34H
ASCII for 1 is 31H, for 2 is 32H, etc
❖ BCD representation
Unpacked BCD
» Example: 1234 is stored as 01 02 03 04H
– Additional byte is used for sign
❖ Sign byte: 00H for + and 80H for –
❖ Packed BCD
» Saves space by packing two digits into a byte
– Example: 1234 is stored as 12 34H
32
Program 13Program to add any two hexa decimal numbers (8-bit)
.model small
.stack
.data
.code
main proc
mov dl,15h
mov bl,36h
add bl,dl
mov al,bl
daa ; Decimal Adjust after Addition
mov cl,10h
mov ah,0
aad ; ASCII Adjust before Division
div cl
or ax,3030h
mov bl,ah
mov dl,al
mov ah,2
int 21h
mov dl,bl
mov ah,2
int 21h
33 mov ax, 4c00h
int 21h main endp end main
Pgm14: ASCII value addition (use of AAA)
.model small
.stack
.data
.code
start:
mov cl,32h
add cl,35h
mov al,cl
aaa ; ASCII adjust for Addition
or al,30h
mov dl,al
mov ah,2
int 21h
mov dl,bl
mov ah,2
int 21h
mov dl,cl
mov ah,2h
int 21h
36
Program 18: A program to list numbers from 0,......., 7 (Illustration of Loop )
.model small
.stack
.data
.code
start:
mov dx,30h
mov cx,07h
lop: mov ah,2h
int 21h
inc dx
loop lop
38
Program 13: Program to add any 10 stored numbers
39
Prgm 14: An interactive Pgm to swap any two numbers
40
Program 15: Program to add any 3 numbers, entered through the keyboard (User interactive )
41
Pgm16: greeting
42
43
44