Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Assembly Language Programming Lab Edited For Ext

The document discusses assembly language programming concepts such as variables, assignment, input/output, and control flow. It specifically focuses on programming in 8086 assembly language. Key points include: - Variables are represented using registers that do not need declaration. Assignment uses MOV instructions. - Input/output uses software interrupts via the INT instruction to call MS-DOS functions, specifying the operation in registers like AH. - Common I/O operations like character output and input are demonstrated through examples storing characters in DL and AL respectively. - The document provides several short programs as examples, such as outputting a string of characters or reading and displaying a character. Termination returns to MS-DOS using INT 21

Uploaded by

3d nat nati
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Assembly Language Programming Lab Edited For Ext

The document discusses assembly language programming concepts such as variables, assignment, input/output, and control flow. It specifically focuses on programming in 8086 assembly language. Key points include: - Variables are represented using registers that do not need declaration. Assignment uses MOV instructions. - Input/output uses software interrupts via the INT instruction to call MS-DOS functions, specifying the operation in registers like AH. - Common I/O operations like character output and input are demonstrated through examples storing characters in DL and AL respectively. - The document provides several short programs as examples, such as outputting a string of characters or reading and displaying a character. Termination returns to MS-DOS using INT 21

Uploaded by

3d nat nati
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Assembly Language Programming

1 Compiled by: Abdisa L.


 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 Compiled by: Abdisa L.


Variables

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


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

Compiled by: Abdisa L.


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 Compiled by: Abdisa L.


 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 Compiled by: Abdisa L.


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 Compiled by: Abdisa L.


 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 Compiled by: Abdisa L.


 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 Compiled by: Abdisa L.


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 Compiled by: Abdisa L.
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

Compiled by: Abdisa L.


10
Program 2
 Write a code fragment to display the character „a‟ , „b‟ ,‟d‟, ‟i‟, ‟s‟, ‟a‟ on the screen:
org 100h
mov dl, 'a' ; dl = ‘a’
mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character
mov dl, 'b' ; dl = ‘b’
mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character
Output:
mov dl, 'd'
mov ah, 2h
; dl = ‘d’
; character output subprogram
abdisa
int 21h ; call ms-dos, output character
mov dl, 'i' ; dl = ‘i’
mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character

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


mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character
mov dl, 'a' ; dl = ‘a’
11 Compiled by: Abdisa L.
mov ah, 2h ; character output subprogram
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 Compiled by: Abdisa L.
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 Compiled by: Abdisa L.


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

Compiled by: Abdisa L.


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
15 Compiled by: Abdisa L.
int 21h
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

Compiled by: Abdisa L.


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

Compiled by: Abdisa L.


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 Compiled by: Abdisa L.
19 Compiled by: Abdisa L.
Program 8:
 Write a program to load character ‘ ? ’ into register ax and
display the same on the screen.

Compiled by: Abdisa L.


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 Compiled by: Abdisa L.
String output

The register DX should be used for this purpose

22 Compiled by: Abdisa L.


Procedure

23 Compiled by: Abdisa L.


Compiled by: Abdisa L.
24
25 Compiled by: Abdisa L.
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 Compiled by: Abdisa L.
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
Compiled by: Abdisa L.
end start
27
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 Compiled by: Abdisa L.
28
 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


29 Compiled by: Abdisa L. 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]
Compiled by: Abdisa L.
30
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
mov cl,10h
mov ah,0
aad
div cl
or ax,3030h
mov bl,ah
mov dl,al
mov ah,2
int 21h
mov dl,bl
mov ah,2
int 21h
31 Compiled by: Abdisa L. 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

or al,30h
mov dl,al
mov ah,2
int 21h

mov dl,bl
mov ah,2
int 21h

mov ax, 4c00h


int 21h
32 end start Compiled by: Abdisa L.
Pgm16: Multiplication (use of AAM)
.model small
.stack
.data
.code
start:
mov al,03h
mov cl,09h
mul cl
aam

or ax,3030h
mov cx,ax
mov al,ch
mov ah,2h
int 21h

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

mov ax, 4c00h


int 21h Compiled by: Abdisa L.
33
end start
Pgm 10: Division (use of AAD)

34 Compiled by: Abdisa L.


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
35 Compiled by: Abdisa L.
end start
Program 12: Program to add consecutive 10
numbers

36 Compiled by: Abdisa L.


Program 13: Program to add any 10 stored numbers

37 Compiled by: Abdisa L.


Prgm 14: An interactive Pgm to swap any two numbers

38 Compiled by: Abdisa L.


Program 15: Program to add any 3 numbers, entered through the keyboard (User interactive )

39 Compiled by: Abdisa L.


Pgm16: greeting

40 Compiled by: Abdisa L.


41 Compiled by: Abdisa L.
42 Compiled by: Abdisa L.

You might also like