Friday, 25 January 2013

8086 microprocessor Programming (Part 6)


8086 STATUS FLAGS


Aims of the Experiment: 

  • To study the effect of different instructions on the 8086 status flags. 
  • To introduce the students to the strategy for finding errors (debugging) in an assembly language program. 


Introduction: 

The 8086 microprocessor has nine flags. Out of these nine flags, six are called status flags. These flags indicate conditions that are produced as the result of executing an arithmetic or logic instruction. That is, specific flag bits are reset (logic 0) or set (logic 1) at the completion of execution of the instruction. These status flags are : the carry flag (CF), the parity flag (PF), the auxiliary carry flag (AC) , the zero flag (ZF), the sign flag (SF), and the overflow flag (OF). The remaining three (out of nine) flags are called the control flags, and they are : the trap flag (TF), the interrupt flag (IF), and the direction flag (DF).  

In the first part of this experiment, we’ll use DEBUG to study these flags (except TF). When you load a program in DEBUG, and then trace it, the contents of all the registers as well as the status of all but trace flag (TF) are displayed after the execution of each instruction. The flags are displayed in the order given below. The DEBUG syntax for set/reset condition of these flags is also included in the following table:

Flag Type
Syntax for Set (logic 1)
Syntax for Reset (logic 0)
Overflow
OV
NV
Direction
DN
UP
Interrupt
EI
DI
Sing
NG
PL
Zero
ZR
NZ
Auxiliary Carry
AC
NA
Parity
PE
PO
Carry
CY
NC

Following points are worth mentioning here:
  • None of the data transfer instructions (mov, xchg, xlat, lea, lds, and les) changes any of the status flags, that is, the status of these flags remains the same after the execution of these instructions as it was before the execution. 
  • The addition and subtraction instructions (add, adc, sub, sbb) affect all the six status flags. 


These flags are used in decision-making instructions like the ‘jnz’ and ‘jnc’ instructions that you are already familiar with. It is important to know before taking a decision based on these flags (for example using conditional jumps) whether the instruction you are using affects the flag used by the decision making instruction. It is a good  practice to consult a reference book to check if the instruction in use changes the status of a flag. 

Important Notes:

  • Write the following program in a text editor, assemble, and link it, and then load it in DEBUG. Trace the program, and answer on your work sheet the questions asked in comments. Answer only for the six status flags. 


Page, 132 
Title # Program for studying flags # 

Dosseg 
.model small 
.stack 100h
.code 
order-word proc 
mov ax, @data ;initialize the DS register 
mov ds, ax 
mov al, 53h 
sub al, 53h ;1. which flags are set and why?
mov   ah, 73h 
add ah,38h ; 2. Which flags are set and why?
mov cx, 9FF8h
add cx, 9CCCh ; 3. Which flags are set and why?
clc ; clear carry flag command. It 
; will be reset (logic 0).
mov dl, 0Feh ; 4. Check if any flags changed. 
Inc dl ; 5 . Which flags are set and why?
Inc dl ; 6. which flags are set and why?
stc ; set carry flag. Check if so.
mov ax, 2000h
adc ax, 0Fh ; add with carry command 
; 7. Which flags are set and why?
mov al , 08h 
mov cl, 1Fh
mul cl ; 8. Note CF and OF status 
; the following segment contains 8086 logic instructions 
mov al, 01010101b ; load binary number 
and al, 00011111b ; 9. What is result? Why? Note 
; OF, CF, PF , ZF, and SF 
or al, 11000000b ;10. What is result now?
Xor al, 00001111b ;11. What is result now?
not al ;12.What now? (1’s comp.) 

Comment# The following segment contains 8086 shift instructions 
;13 Note the status of carry flag after each of the following instructions are executed. Also note the contents of AL after each SHL instruction. You’ll notice that shifting left by one position is equivalent to multiplying by 2, and shift right by one position is same as dividing by 2. # 

mov al, 0aah
shl al,1 ;shift AL left one position
shl al,1
shl al,1
shl al,1
mov cl, 04ch ;shift AL left 4 times
shl al, cl

mov bx, 0888h
mov cl, 05
shr bx,cl ;note the contents of BX register

mov ax, 0888h
mov cl, 20h
div cl ; note the contents of AL
; they should be the same as of BX

Comment # The following program segment uses the 8086 rotate instructions. They are different from shift instructions in that the contents of carry flag are also involved in this movement.
1b. Note the contents of carry flag and the AX register after each of the following instructions is executed #

mov ax, 5555h
ror ax, 1 ; rotate right one position
ror ax, 1
ror ax, 1
ror ax, 1

mov cl, 06h ; rotate right six position
ror ax, cl

rol ax, cl ; rotate left by six 
rol ax, l ; rotate left by one position
rol ax, l
rol ax, l

mov ax, 4C00h
int 21h

order_word endp
.data
end order_word


  • In the last experiment there was a program for converting a string of binary numbers into a corresponding string of gray code numbers. There were some errors in the algorithm, because of which the program only converted the numbers between 0-7 (hex) correctly into gray code numbers. It returned garbage for the numbers between 8-F (hex). In the following program, we have improved the algorithm so that it should return the correct gray code string for a string of hex numbers 0-F (hex). There are, however two bugs deliberately placed in this program find out the bugs. Use DEBUG whenever you feel necessary. It will help you great deal if you make flow chart for this program.


Title program for converting binary string to gray code string.

Dosseg
.model small
.stack 100h
.code

order_word proc
mov ax, @data
mov ds, ax ;initialize data segment register
mov si, 1000h
mov dx, 08h ;initialize the counter
mov ah, 01h ;character with echo
next_word:
int21h
cmp al, 60h ;compare instructions
jc ab_cd ;from AL but Al remains unchanged
add al, 09h ;only flag is affected
ab_cd:
and al, 0fh
mov cl, al
call code_conv
cmp cl, 0ah
jc numeral
sub cl, 09h
or cl,60h
jmp alphabet
numeral:
or cl, 30h
alphabet:
mov [si], cl
inc si
dec dx
jnz next_word
mov si, 1000h
mov cx, 08h
mov ah, 02h
previous_word:
inc si
mov dl, [si]
int 21h
dec cx
jnz previous_word
mov ax, 4c00h
int 21h
order_word endp
code_conv proc
push ax
mov bx, offset values
mov al, cl
xlat
mov cl, al
pop ax
ret
code_conv endp

.data
values db 0h,1h,3h,2h,6h,7h,5h,0ch,0dh,0fh,0eh,0ah,9h,8h
end order_word


  • Try the program for finding mean square of eight decimal numbers (0-9), that you were asked to do in the bonus part of the last experiment.



No comments:

Post a Comment