;DELETE_ONE--G5_7
;**********************************************************
dataseg	segment
  list	dw	6,125,67,756,45,1234,8
dataseg	ends
;**********************************************************
prog	segment
main    proc	far
	assume	cs:prog,ds:dataseg,es:dataseg
start:
	push 	ds	
	sub	ax,ax
	push	ax
;
	mov	ax,dataseg
	mov	ds,ax
	mov	es,ax
;
	lea	di,list
	mov	ax,45
	call	del_ul
	ret
;
main 	endp



;Delete the value in AX from an unordered list in the extra
;  segment,if that value is in the list.
;Inputs:(DI)=starting address of the list.
;        First location of the list=Length of list(words)

del_ul	proc	near		
	cld			;make DF=0,to scan forward
	push	di		;save starting address
	mov	cx,es:[di]	;fetch element count
	add	di,2		;make DI point to 1st data el.
	repne	scasw		;value in the list?
	je	delete		;if so,go delete it
	pop	di		;otherwise,exit
	jmp	short exit
;The following instruction delete an element from the 
;  list,as follows:
;    (1) If the element lies at the end of the list,delete
;        it by decreasing the element count by 1.
;    (2) Otherwise,delete the element by moving all 
;        subsequent elements up by one position.

delete:	jcxz	dec_cnt		;if (CX)=0,delete last el.
next_el:mov	bx,es:[di]	;move one el.up in list
	mov	es:[di-2],bx
	add	di,2		;point to next el.
	loop	next_el		;repeat until all el.moved
dec_cnt:pop	di		;decrease el. count by 1
	dec	word ptr es:[di]
exit:	ret			;exit
del_ul	endp			;end of main part of prog.
;***********************************************************
prog	ends
;***********************************************************
	end	start
