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

Assembly Language Programming lab

The document provides an overview of assembly language programming, focusing on key concepts such as variables, assignment, input/output operations, and control flow using the 8086 architecture. It includes examples of code fragments for displaying characters, reading input, and managing strings, along with explanations of the necessary instructions and registers. Additionally, it outlines various programming exercises to reinforce the concepts learned.

Uploaded by

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

Assembly Language Programming lab

The document provides an overview of assembly language programming, focusing on key concepts such as variables, assignment, input/output operations, and control flow using the 8086 architecture. It includes examples of code fragments for displaying characters, reading input, and managing strings, along with explanations of the necessary instructions and registers. Additionally, it outlines various programming exercises to reinforce the concepts learned.

Uploaded by

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

Assembly Language Programming

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

Programming in assembly language involves mastering the same


concepts and a few other issues.

2
Variables

 We will use the 8086 registers as the variables in our


programs.
 Registers have predefined names and do not need to be
declared.

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:

mov dl, ‘a’ ; dl = ‘a’


mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character

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

mov dl, ‘3' ; dl = ‘3’


Output:
mov ah, 2h
int 21h
; character output subprogram
; call ms-dos, output character
123456
mov dl, ‘4' ; dl = ‘4’
mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character

mov dl, ‘5' ; dl = ‘5’


mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character
mov dl, ‘6' ; dl = ‘6’
mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character
12 ret
Character Input
 There are also three elements involved in performing character
input:
 As for character output, we specify which of MS-DOS’s I/O
subprograms we wish to use, i.e. the character input from the
keyboard subprogram.
 This is MS-DOS subprogram number 1h.
 This number must be stored in the ah register.
 We call MS-DOS to carry out the I/O operation using the INT
instruction as for character output.
 The MS-DOS subprogram uses the al register to store the character
it reads from the keyboard.

13
Program 4
 Example: Write a code fragment to read a character from
the keyboard

mov ah, 1h ; keyboard input subprogram


int 21h ; character input
; character is stored in al

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 ; keyboard input subprogram


int 21h ; character input, character is stored in al
mov ah, 1h
int 21h
mov ah, 1h
int 21h
mov ah, 1h
int 21h
mov ah, 1h
int 21h

mov ah, 1h
int 21h
mov ah, 1h
int 21h
15 ret
 Program 6
 Example: Reading and displaying a character:

mov ah, 1h ; keyboard input subprogram


int 21h ; read character into al
mov dl, al ; copy character to dl
mov ah, 2h ; character output subprogram
int 21h ; display character in dl

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.

It is also possible to use as follows;


mov ah, 4ch
int 21H

 It is must to terminate a program code, otherwise the program may crash

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

The register DX should be used for this purpose

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 dx, offset Message ;move offset Message into dx

mov ah,9 ;func 9 of dos interrupt 21h prints the string that terminate with "&".
int 21h

mov ax, 4c00h ;return to ms-dos


int 21h
main endp
Output:
end main AUWC department of CS microprocessor lab
26
Program 10: LEA instruction that displays “AUWC School of Computing
department CS microprocessor lab ” ;
.model small
.stack
Output:
.data AUWC School of Computing department CS microprocessor lab
Message db "AUWC "
db "School of Computing "
db "department CS "
db "microprocessor lab.$"
.code
Start:
mov ax, seg Message ;moves the segment "Message" into AX
mov ds, ax ;moves ax into ds(ds=ax)

lea dx,Message ;stores the offset within the datasegment of the


;bit-string message into the Dx register

mov ah,9 ;func 9 of dos interrupt 21h prints the string that terminate with "&".
int 21h

mov ax, 4c00h ;return to ms-dos


int 21h
end start
27
Displaying many Strings
.model small
.stack
.data
msg1 db "AUWC department of CS microprocessor lab.$"
msg2 db "Students mark list.$"
.code
main proc
mov ax,@data ;datamoves the segment "Message" into AX
mov ds,ax ;moves ax into ds(ds=ax)

mov dx, offset msg1 ;move offset Message into dx


mov ah,9 ;func 9 of dos interrupt 21h prints the string that terminate with "&".
int 21h

mov dx,10 ;newline feeding


mov ah,2
int 21h Output:
mov dx,13 ;cartilage return AUWC department of CS microprocessor lab
mov ah,2 Students mark list
int 21h

mov dx, offset msg2 ;move offset Message into dx


mov ah,9 ;func 9 of dos interrupt 21h prints the string that terminate with "&".
int 21h

mov ax, 4c00h


int 21h
28 main endp
end main
Program 11:
 Program to list all alphanumeric characters (ASCII Character Set) use jump instruction)
.model small
.stack
.data
.code
main proc
mov dl,1h
repet: mov ah,02h
int 21h
inc dl
jnz repet

mov ax, 4c00h


int 21h
main endp
end main
29
 Program 12: Write an interactive ALP to read a byte from the keyboard and display it on the screen
.model small
.stack
.data
Message db "Enter a character:$"
Disp db 13,10,'the Entered character is $'
.code
Begin:
mov ax,@data
mov ds,ax

mov dx, offset Message

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

mov ah, 4ch


30 int 21h
end begin
Lab Exercises Questions
 Program 13: Program to add any two hexa decimal numbers (use of
DAA)
 Program 14: Addition of any two ASCII numbers. (use of AAA)
 Program 15: Program to subtract packed bcd numbers (use of DAS)
 Program 16: Program to multiply 2 numbers (use of AAM)
 Program 17: Program to divide a packed bcd number. (use of AAD)
 Program 18: A program to list numbers from 0,......., 9 (Illustration of Loop )
 Program 19: Program to add consecutive 10 numbers
 Program 20: Program to add any 10 stored numbers
 Program 21: Program for swapping any two numbers
 Program 22: Program to add any 3 numbers, entered through the keyboard
(User interactive)
 Program 23: A program that prompts the user with the question ‘Is it after 12 noon
(Y/N)?’ . If the response is ‘y’ for yes , greet the user with the message 'Good afternoon,
world!’, if the response is ‘n’ for No , greet the user with the message 'Good morning,
world!’, else greet with the default message 'Good day, world!’ in a newline. [This program
will make use of ‘cmp’, ‘jmp’ instructions]

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 ax, 4c00h


int 21h
34 end start
Pgm16: Multiplication (use of AAM)
.model small
.stack
.data
.code
start:
mov al,03h ; multiplier in ASCII
mov cl,09h ; multiplicand in ASCII
mul cl
aam ; ASCII adjust for Multiplication
;AX := 0207H
or ax,3030h ; AL := 37H
mov cx,ax
mov al,ch
mov ah,2h
int 21h

mov dl,cl
mov ah,2h
int 21h

mov ax, 4c00h


int 21h
35
end start
Pgm 10: Division (use of AAD)

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

mov ax, 4c00h


int 21h
37
end start
Program 12: Program to add consecutive 10
numbers

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

You might also like