PROGRAMMING (CONTINUED)
Aims of the Experiment:
- To develop further understanding of arithmetic instructions.
- Give a brief introduction to subroutine handling instructions.
- Give introduction to some simple DOS functions using ‘int 21h’.
Important Notes:
- DOS Function Requests:
The DOS operating system uses interrupt type 21h to make many functions available to the user. Most of these functions provide for input and/or output to devices. Many are for various operations with disk files. See Appendix C of [1] for details of these functions. In this experiment, we’ll discuss how to use DOS functions and describe some of the simpler ones.
All functions called with ‘int 21h’ invoke the same interrupt handler. The desired function is selected by placing its function number in the AH register half. Many functions either require additional data to be sent to the interrupt handler procedure and/or they return information. Such parameters are normally passed using registers. The only registers altered by this interrupt handler are those used to return information to the user. The following table summarizes some of the functions covered in this experiment.
Function Action Parameter (s)
for DOS Parameter(s) DOS Returned by
Number
1h Input character
(with echo) None Character in AL
2h Display character Character in
DL None
8h Input character
(no echo) None Character in Al
4Ch Terminate
process Return code
in AL None
Each complete program in previous experiment used DOS function 4Ch to terminate program execution. (Remember we put 4C00h in AX before ‘int 21 h’ instruction.) We’ll use other functions in this experiment for input and output of characters from/to the console.
- You have been using DEBUG program. While using this program, it should be kept in mind that it works in default with hex numbers. The MASM/TASM program, on the other hand, works in default with decimal numbers. So, in order to work with hex numbers while using MASM/TASM , always put an ‘h’ followed by the number. For example ‘1234’ will be treated as decimal 1234, whereas ‘1234h’ will be treated as hex 1234.
- Whenever the first digit in a hex number is an alphabet (a-h), do not forget to put a ‘0’ before this number, otherwise an error will occur. As an example, instead of writing ‘F000h’ always write ‘0F000h’.
- Write the following program in a text editor.
Page, 132
Title #. None #
Dosseg
.model samall
.stack 100h
.data ; variables here
.code
order _word proc
mov ax, @data ; initialize the DS register
mov ds, ax
mov si, 1000h
mov cx, 08h ; initialize the counter reg.
mov ah,01h ; DOS function for input character with echo
next_word: ; this a label
int 21h ; interrupt for DOS services
mov [si] , al ; save the input byte
inc si ; the next byte
dec cx ; decrement counter
jnz next-word ; go back if count if count not zero
mov cx, 08h ; DOS function for display char
mov ah, 02h
Previous_word:
dec si
mov d1, [si] ; get char to be displayed in DL
int 21h ; interrupt for DOS services
dec cx
jnz previous_word
mov ax, 4C00h ; end program
int 21 h
order_word endp
end order_word
Save assemble, and link this program and run it first at the DOS prompt. The program will wait for you to type eight characters on the keyboard. Answer the following questions:
- What is this program doing?
- Why have we used the instruction ‘dec si’ before ‘int 21h’? What will happen if we place it after the ‘int 21h’ instruction?
- Make a modification in this program so that it displays only the first, third, fifth, and seventh input characters in that order. Verify our program.
- The following program uses the Binary to Reflected Gray Code conversion subroutine of the last experiment to convert a string of binary numbers to a corresponding string of Gray Code numbers. Write the following assembly language program using a text editor:
Page, 132
Title program for converting Binary String to Gray Code String
Dosseg
.model small
.stack 100h
.data ; variables here
values db 0h, 1h, 3h,2h, 6h, 7h, 5h, 4h, 0ch, 0dh, 0fh, 0eh, 0ah, 0bh, 9h, 8h.
.code
order_word proc
mov ax, @data ; initialize the DS register
mov ds, ax
mov si, 01000h
mov dx, 08h initialize the counter reg.
mov ah, 01h ; DOS function for input character, with echo
next_word: this a label
int 21 h ;nterrupt for DOS services
mov cl, al ;opy AL in CL
and cl, 0fh ;make the upper nibble ‘0’
;ince AL contains ASCII code
and al, 0f0h ;save the upper nibble
call code_conv
or cl, al ;convert back to ASCII
mov [si], cl ; save the input byte
inc si ; the next byte
dec dx ; decrement counter
jnz next_word ; go back if cont not zero
mov si , 1000h ; re-initialize SI
mov cx, 08h
mov ah, 02h ; DOS function for display char
previous_word:
mov d1, [si] ; get char to be displayed in DL
int 21 h ; interrupt for DOS services
inc si
dec cx
jnz previous_word
mov ax, 4C00h ; end program
int 21h
order_word endp ; main procedure ends here
code_conv proc ; Code conversion subroutine
push ax ; save AX as it will be
; used in this subroutine
mov bx, offset values ; offset directive to load
; the offset ad. Of values in BX
mov al, cl
xlat ; replace AL by contents
; of DS: BX + AL
mov cl, al
pop ax ; Get back the orig. value of AX
ret
code_conv endp
end order_word
Save the program, assemble and link it, and finally run it form the DOS prompt. Load in DEBUG and trace the program to understand the meanings of each instruction.
Answer the following questions:
- What will happen if you do not use the ‘AND’ and ‘OR’ instructions in the above program?
- Using DEBUG, find out what are the contents of SS: SP before and after the execution of the ‘PUSH AX’ instruction.
- Convert the negative numbers in the following list in to 2’s complement form and replace ni (i=1 to 8) in the data segment of the following program by these numbers. Write this program, assemble, and link it. Find out the purpose of this program. Put comments where you feel necessary. To get more insight, trace with DEBUG. List of numbers: 0228h, -225Ah, 0524h, 25Afh, -023Bh, -1133h, 2FFABh, 1230h.
Page, 132
Title program for Adding 8 data words
Dosseg
.model small
.stack 100h
.data ; variables here
value dw nl, n2,n3,n4, n5, n6, n7, n8
.code
add_word proc
mov ax, @data ; initialize the DS register
mov ds, ax
mov si, 0h ; initialize source index
mov ax, 0h ; initialize the sum register
mov cx, 08h ; initialize the counter reg.
mov dl, 0h
next_word:
add ax, [si+ offset value]
jnc no_increment
inc dl
no_increment:
inc si
inc si
dec cx
jnz next_word
mov [si+ offset value] , ax
inc si
inc si
mov [si+ offset value] , dl
mov ax, 4C00h ; end program
int 21h
add_word endp
end add_word
- Write, assemble, and debug an assembly language program that asks the user to enter 8 decimal numbers (0-9), and then it finds the mean of these numbers and displays the result on screen.
- Try to modify the program such that it calculates the mean square value and then displays all the digits of the result on screen?
REFERENCES:
- Kip R. Irvine, Assembly Language for the IBM-PC. Macmillan Publishing Company, 1990.
- Avtar Sing and Walter A. Trieble, The 8086 and 80286 Microprocessors, Hardware, Software, and Interfacing. Prentice-Hall Inc. 1990
- Richard C Detmer, Fundamentals of Assembly Language Programming, Using IBM PC and Compatibles. DC Heath and Company. 1990.
No comments:
Post a Comment