Pages

Friday, 25 January 2013

8086 microprocessor Programming (Part 4)


WORKING WITH DATA TRANSFER AND ARITHMETIC INSTRUCTIONS


Aims of the Experiment:

  • Give an introduction to the use of Microsoft Macro-Assembler MASM. 
  • To develop more programs using data transfer and arithmetic instruction. 


Important Notes:


  • The Assembly Process:

The first step in assembly language programming is the writing of the source program with the help of a text editor. The source program is a collection of text characters making up a program that follows the rules of assembly language. We’ll be using the ‘EDIT’ editor available with the newer versions of DOS for writing the source program. You can, of course, use a text editor of your choice. The source program should be saved as a filename with extension ‘asm’ (*.asm).
The assembly language programs must be converted to machine language before they can be executed. Following steps are involved in this assembly process:
Assembly Step: An assembler is a program designed to read an assembly language source program as input and generate an object program (*.obj) as output. The assembler may also create a listing file (*.lst), which lists both the source statements and the machine code, and a cross-reference file (* .crf). An object program (.obj) is a machine language interpretation of the original source program, lacking certain information needed by DOS before it may be executed. To assemble a program (*.asm file) we’ll use the following command typed in either lowercase or uppercase at the DOS prompt:
MASM/L/N Filename; 
Note that the ‘asm’ extension is not written with the file-name. The /L option specifies an assembly listing file, and the /N option suppresses tables in the listing file. Refer to chapter 4 of [1] for more details. 
Link Step: The linker supplied with the Macro Assembler reads an object program (*.obj) and generates an executable (*.exe) program. The executable program can be executed just by typing its name. To link an object program, use the following command at the DOS prompt: 
LINK Filename; 

TASM (Turbo Assembler)

Step1: Edit a file using any editor with .asm extension
Step 2: Assemble the program and create an .obj file by writing tasm filename.asm at the DOS prompt
Step 3: Link the file using tlink.exe.  The command is  tlink filename.obj 
Step 4: Load the exe file in the debugger. The command is debug filename.exe. Load the file, Unassemble it and execute it using either of g, t or p debug commands

The Program Template:

Use the following program template for creating new programs:
Page 132
Title name/purpose of the program 
Dosseg 
.Model small 
.Stack 100h 
.Data ; variable here
.Code ;code here
Name of the main procedure   proc 
mov ax, @data ;initialize the DS register  
mov ds, Ax
mov  ax,4c00h :end program 
int 21h 
name of the main procedure  endp 
 
end name of the main procedure 
The actual assembly language program is written in the space where ellipses (…) are shown. All text typed to the right of semicolon (;) is ignored, you can use it for your comments. MASM is case insensitive, meaning that it doesn’t recognize the difference between uppercase and lowercase letters.  

Assembler Directives:

Assembler directives are different form assembly language instructions in that the former are directions to the assembler whereas the latter are directions to the 8086 microprocessor. The following directives are used in the above template:
.CODE Mark the beginning of the code segment 
.DATA Mark the beginning of the data segment 
DOSSEG Use standard segment ordering 
END End of program assembly 
ENDP End of procedure 
.MODEL Determine the program model memory size 
PAGE Set a page format for the listing  file 
PROC Begin procedure 
.STACK Set the size of the stack segment 
TITLE Title of listing file 
Another important directive that we’ll be using is ‘offset’ 
OFFSET Returns the offset address of the following memory location. 
For more details on these and other assembler directives, refer to chapter 4 of [1].

  • Write the following program in a text editor. 

Page, 132
Title program for adding 8 data words 
Dosseg 
.model small 
.stack 100h 
.data ; variable here 
value dw 0024h, 2357h, 001Fh, 0123h, 01FBh, 0F45h, 089Fh, 1FDBh
.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, 08 h ; initialize the counter reg. 
Next-word: ; this a label 
add ax, [si+offset value] ;add each number 
inc si ; inc si twice to point it to
Inc si ;the next word
Dec cx ; decrement counter 
Jnz next-word ; go back if count not zero 
mov    [si+offset value],ax ; data 
mov ax, 4c00h ; end program 
Int 21h 
Add-word endp 
end add-word 

save it with the name ‘prog1.asm.’ assemble and link by typing ‘tasm prog1.asm’ and ‘tlink prog1.obj’ at the DOS prompt. Load the program in DEBUG by typing ‘debug prog1.exe’ and then trace it. The following points are worth paying attention: 
  • In the ‘jnz’ instruction, we have specified a return label rather than a return address as we have been doing while writing programs with the help of DEBUG. This results in ease of programming especially when the source program is relatively bigger. 
  • The value parameter in the add instruction represents the address of the first memory location in the data segment where the eight words are stored. 
  • The following program converts the binary code to reflected Gray code. Write the following assembly language program using a text editor: 

Page, 132 
Title program for converting Binary code to Gray Code 
Comment #This program takes the binary number in CL as input and replaces it with its Gray code equivalent number. #
Dosseg 
.model small 
.stack 100h
.data  ;  variables here 
values db oh, 1h, 3h,2h, 6h, 7h, 5h, 4h, 0ch, 0dh, 0fh, 0eh, 0ah, , 6h, 8h. 

.code
code_conv proc
mov ax, @data ; initialize the DS register 
mov ds, ax 
mov bx,offset values ; use offset directive to load 
  ; the offset address. Of values in BX 
mov al, cl 
xlat ; replace AL by contents of  DS:BX+Al 
mov cl, al 
mov ax, 4C00h   ; end program 
int 21h 
code-conv endp 
end code-conv

Save the program, assemble and link it, and finally load it in DEBUG. Load CL with some value between 0h and Fh, trace this program, and verify if it gives correct results. Repeat for some other values in CL. 
Note: Use ‘r cx’ command in DEBUG for loading various values in CL. 

  • You have used a new assembler directive in this program: 

COMMENT# tex# - you can write as many lines of comment between the two delimiters # as you may think to be necessary. 
  • The XLAT instruction is used to simplify implementation of the look-up table operation. In the above program, we have saved the Gray code at the starting address of DS value. The first byte corresponds to Gray code of 0h, the second to that of 1h, the third to that of 2h, and so on. The usefulness of this program may not become clear to you now, however, if you think this program as a subroutine of some big program then you may think the main program passing an input argument in the form of a binary number to this subroutine. In reply, this subroutine will return the corresponding Gray code, which may later be used in the main program. 


  • Write, assemble, and debug the following program, and find out its purpose. There are two new commands used in this program:

MUL DL – it multiplies the contents of AL with DL and puts the result in AX. 
DIV CL – it divides AX by CL, puts quotient in AL and remainder in AH. 
Page, 132 
Title quiz program 
Comment # Find the purpose of the following program. Add comments were you feel necessary. # 

Dossag 
.model small
.stack 100h
.data 
value db 01,02, 03, 04, 05,06, 07, 08

.code
main proc 
mov ax, @data 
mov ds, ax
mov cl, 08h 
mov si, 0h 
mov bx, 0h 
next-byte:
mov al, [offset value+si]
mov dl, al
mul dl
add bx, ax
inc si 
dec cl 
jnz next-byte
mov cl, 08h
mov ax, bx 
div cl 
mov [offset value+si] , ax
mov ax, 4C00h
int 21h 
main endp 

end main 

  • Write, assemble, and debug an assembly language program that calculates the average of following positive words:

0123h, 00FBh, 00BDh, 0223h, 02CDh, 1100h, 0223h, 019Bh. 

  • REFERENCES:

  1. Kip R. Irvine, Assembly Language for the IBM-PC. Macmillan Publishing Commpany,1990. 
  2. Avtar Singh and Walter A. Triebel, The 8086and 80286 Microprocessors, Hardware, Software, and Interfacing. Prentice-Hall Inc. 1990.


5 comments: