問題:
Using the ArrayScan program in Section 6.3.4.2 as a model,
implement the search using the LOOPZ instruction.
程序:
TITLE Scanning an Array (ArryScan.asm)
; Scan an array for the first nonzero value.
; Last update: 06/01/2006
INCLUDE Irvine32.inc
.data
intArray SWORD 0,0,0,0,1,20,35,-12,66,4,0
;intArray SWORD 2,0,0,0
;intArray SWORD 0,0,0,0
;intArray SWORD 0,0,0,3
noneMsg BYTE "A non-zero value was not found",0
.code
main PROC
mov ebx,OFFSET intArray ; point to the array
mov ecx,LENGTHOF intArray ; loop counter
sub ebx,2 ; move forward one element
L1:
add ebx,2
cmp WORD PTR [ebx],0 ; compare value to zero
loopz L1 ; continue the loop
cmp WORD PTR [ebx],0
jnz found
jmp notFound ; none found
found:
movsx eax,WORD PTR [ebx] ; otherwise, display it
call WriteInt
jmp quit
notFound:
mov edx,OFFSET noneMsg ; display "not found" message
call WriteString
quit:
call crlf
exit
main ENDP
END main
; Scan an array for the first nonzero value.
; Last update: 06/01/2006
INCLUDE Irvine32.inc
.data
intArray SWORD 0,0,0,0,1,20,35,-12,66,4,0
;intArray SWORD 2,0,0,0
;intArray SWORD 0,0,0,0
;intArray SWORD 0,0,0,3
noneMsg BYTE "A non-zero value was not found",0
.code
main PROC
mov ebx,OFFSET intArray ; point to the array
mov ecx,LENGTHOF intArray ; loop counter
sub ebx,2 ; move forward one element
L1:
add ebx,2
cmp WORD PTR [ebx],0 ; compare value to zero
loopz L1 ; continue the loop
cmp WORD PTR [ebx],0
jnz found
jmp notFound ; none found
found:
movsx eax,WORD PTR [ebx] ; otherwise, display it
call WriteInt
jmp quit
notFound:
mov edx,OFFSET noneMsg ; display "not found" message
call WriteString
quit:
call crlf
exit
main ENDP
END main