;_80x86 OPTIMIZATION_
;by Michael Abrash
;80x86a.asm

;[LISTING ONE]

; Copies one string to another string, converting all characters to
; uppercase in the process, using a loop containing LODSB and STOSB.
; Adapted from Zen of Assembly Language, by Michael Abrash; not a
; standalone program, but designed to be used with the Zen timer from
; that book via the Zen timer's PZTIME.BAT batch file: ZTimerOn starts
; the clock, ZTimerOff stops it, and the test-bed program linked in by
; PZTIME.BAT starts the program, reports the results, and ends.

	jmp	Skip		;skip over data in CS and subroutine

SourceString	label	word		;sample string to copy
	db	'This space intentionally left not blank',0
DestString	db	100 dup (?)	;destination for copy

; Copies one zero-terminated string to another string,
; converting all characters to uppercase.
; Input: DS:SI = start of source string; DS:DI = start of destination buffer
; Output: none
; Registers altered: AX, BX, SI, DI, ES
; Direction flag cleared

CopyStringUpper:
	mov	ax,ds
	mov	es,ax	;for STOS
	mov	bl,'a'	;set up for fast register-register
	mov	bh,'z'	; comparisons
	cld
StringUpperLoop:
	lodsb		;get next character and point to following character
	cmp	al,bl	;below 'a'?
	jb	IsUpper	;yes, not lowercase
	cmp	al,bh	;above 'z'?
	ja	IsUpper	;yes, not lowercase
	and	al,not 20h ;is lowercase-make uppercase
IsUpper:
	stosb		;put character into new string and point to 
                        ; following location
	and	al,al	;is this the zero that marks end of the string?
	jnz	StringUpperLoop ;no, do the next character
	ret

; Calls CopyStringUpper to copy & convert SourceString->DestString.
Skip:
	call	ZTimerOn		;start timing
	mov	si,offset SourceString	;point SI to the string to copy from
	mov	di,offset DestString	;point DI to the string to copy to
	call	CopyStringUpper		;copy & convert to uppercase
	call	ZTimerOff		;stop timing

