8086汇编求整形数组的最大值与最小值(函数实现,输入范围:-32768~32767)

Posted u25th_engineer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了8086汇编求整形数组的最大值与最小值(函数实现,输入范围:-32768~32767)相关的知识,希望对你有一定的参考价值。


图 1 程序运行示意


图 2 程序运行示意

stack   segment stack
        db 512 dup(?)
stack   ends
    
data    segment
		;arr		dw 25, 3, -32768, 88, 9, 32767, 889, 175, 800, -1
		arr		dw 10085, 32061, 14723, 19165, 4242, 11128, 12435, 1, 888, 343
		msg1	db "The original intege array is as follows:", 0ah, 0dh, '$'
		msg2	db "Max of array:", 0ah, 0dh, '$'
		msg3	db "Min of array:", 0ah, 0dh, '$'
        string1 db "Press any key to continue!", 0ah, 0dh, '$'
data    ends
		
code 	segment
        assume ds: data, cs: code, ss: stack
main:
		mov ax, stack
        mov ss, ax
		mov ax, data
        mov ds, ax
		
		lea dx, msg1
		mov ah, 09h
		int 21h
		lea ax, arr
		push	ax
		mov ax, 10
		push	ax
		call	far ptr print_arr
		call	far ptr lineFeed
		
		lea ax, arr
		push	ax
		mov ax, 10
		push	ax
		call	far ptr array_Min_Max
		
		push	ax
		lea dx, msg2
		mov ah, 09h
		int 21h
		pop		ax
		call	far ptr dispsiw
		call	far ptr lineFeed
		
		lea dx, msg3
		mov ah, 09h
		int 21h
		mov ax, bx
		push	ax
		call	far ptr dispsiw
		call	far ptr lineFeed
	
		jmp done

;
; input: 
;			ax -- address of array
;			ax -- length of array
; output:
;			ax -- max of array
;			bx -- min of array
; function:
;			return the max and min of the integer array
;
array_Min_Max	proc	far
		push	bp
		push	si
		push	di
		mov bp, sp
		
		mov si, [bp]
		xor di, di
		mov cx, [bp+10]
		dec cx
		
		mov ax, word ptr [di]		; ax -- maxn
		mov bx, word ptr [di]		; bx -- minn
		
find_max_min:
		inc di
		inc di
		cmp ax, word ptr [di]
		jl get_max
		cmp bx,  word ptr [di]
		jg get_min
		jmp nextMinMax
get_max:
		mov ax, word ptr [di]
		jmp nextMinMax
get_min:
		mov bx, word ptr [di]
nextMinMax:		
		loop find_max_min		
		
		pop di
		pop si
		pop	bp
		ret
array_Min_Max	endp

read_arr	proc	far
		push	bp
		push	si
		push	di
		mov bp, sp
		
		mov si, [bp]
		xor di, di
		mov cx, [bp+10]

readArr:
		call	far ptr readsiw
		mov [di], ax
		add di, 2
		loop readArr
		
		pop di
		pop si
		pop	bp
		ret
read_arr	endp				

print_arr	proc	far
		push	bp
		push 	si
		push 	di
		mov	bp,sp
		
		mov si, [bp]
		xor di, di
		mov cx, [bp+10]			; why must to be [bp+10] here?
		
printArr:
		mov ax, [di]
		call	far ptr dispsiw
		;call	far ptr lineFeed
		call	far ptr space
		add di, 2
		loop printArr

		pop di
		pop si
		pop	bp
		ret
print_arr	endp

space proc	far
		pushf
		push ax
		push dx
		
		mov ah,02h
		mov dl,' '
		int 21h
			
		pop dx
		pop ax
		popf
		ret
space endp

readsiw proc	far
		push bx
		push cx
		push dx
		xor bx, bx   
		xor cx, cx   
		mov ah, 1
		int 21h
		cmp al, '+'
		jz rsiw1
		cmp al, '-'
		jnz rsiw2
		mov cx, -1
rsiw1: 
		mov ah,  1
		int 21h
rsiw2:    
		cmp al, '0'
		jb rsiw3
		cmp al, '9'
		ja rsiw3
		sub al, 30h
		xor ah, ah
		shl bx, 1
		mov dx, bx
		shl bx, 1
		shl bx, 1
		add bx, dx
		add bx, ax
		jmp rsiw1
rsiw3:
		cmp cx, 0
		jz rsiw4
		neg bx
rsiw4:
		mov ax, bx
		pop dx
		pop cx
		pop bx
		ret
readsiw endp

dispsiw proc	far
		push ax
		push bx
		push dx
		test ax, ax 
		jnz dsiw1
		mov dl, '0' 
		mov ah, 2
		int 21h
		jmp dsiw5
dsiw1:
		jns dsiw2  
		mov bx, ax
		mov dl, '-'
		mov ah, 2
		int 21h
		mov ax, bx
		neg ax 
dsiw2:  
		mov bx, 10
		push bx 
dsiw3:
		cmp ax, 0
		jz dsiw4
		xor dx, dx 
		div bx  
		add dl, 30h  
		push dx     
		jmp dsiw3
dsiw4:
		pop dx
		cmp dl, 10
		je dsiw5
		mov ah, 2
		int 21h
		jmp dsiw4
dsiw5:
		pop dx
		pop bx
		pop ax
		ret
dispsiw endp

lineFeed proc	far
		push ax
		push dx
		mov dl, 0dh
		mov ah, 2
		int 21h
		mov dl, 0ah
		mov ah, 2
		int 21h
		pop dx
		pop ax
		ret
lineFeed endp

done:		
		lea dx, string1
		mov ah, 09h
		int 21h
		
		mov ah, 07h
		int 21h
		
		
		mov ah, 4ch
		int 21h
code	ends
		end main

以上是关于8086汇编求整形数组的最大值与最小值(函数实现,输入范围:-32768~32767)的主要内容,如果未能解决你的问题,请参考以下文章

8086汇编求整形数组的最大值与最小值(函数实现,输入范围:-32768~32767)

8086汇编求整形数组的最大值与最小值(函数实现,输入范围:-32768~32767)

8086汇编求整形数组的最大值与最小值(函数实现,输入范围:-32768~32767)

8086汇编显示整形数组,输入并显示整形数组(函数实现,输入范围:-32768~32767)

8086汇编显示整形数组,输入并显示整形数组(函数实现,输入范围:-32768~32767)

8086汇编显示整形数组,输入并显示整形数组(函数实现,输入范围:-32768~32767)