finding index of the array in tasm assembly language and printing it

倖福魔咒の 提交于 2021-01-29 09:23:55

问题


I have made a tasm assembly language program, which finds the minimum in the user-inputted array. I want to find the index of the element of the minimum value which the program is finding.

I want to find the index of the element which the program finds.

For example: input array is [1,2,3,4,5,6]. It should return 1 as minimum value and 0 as index.

Here is the code.

Data Segment
 msg db 0dh,0ah,"Please enter the length of the array: $"
 msg1 db 0dh,0ah,"Enter a number: $"
 newl db 0dh,0ah," $"
 res db 0dh,0ah,"The minimum is: $"
 len db ?
 min db ?
Data ends
Code Segment
assume CS:Code,DS:Data
Start:
 mov ax,Data
 mov DS,ax

 mov dx,offset msg
 mov ah,09h
 int 21h
 
 call Accept

 mov len,bl
 mov cl,bl
 mov ch,00h

 mov di,1000h
 
back: mov dx,offset msg1
 mov ah,09h
 int 21h
 call Accept
 mov [di],bl
 inc di
 loop back

 mov di,1000h
 mov cl,len
 mov ch,00h

 mov dx,offset newl
 mov ah,09h
 int 21h

 mov al,[di] 
 mov min,al

chk: mov bl,min
 mov al,[di]
 cmp bl,al
 jc a
 mov min,al
 jmp b
a: mov min,bl
b: inc di
 loop chk

 mov dx,offset res
 mov ah,09h
 int 21h

 mov bl,min
 call DispNum

 mov ah,4ch
 int 21h
Accept proc
 mov ah,01h
 int 21h
 call AsciiToHex
 rol al,4
 mov bl,al
 mov ah,01h
 int 21h
 call AsciiToHex
 add bl,al
 ret
endp
DispNum proc
 mov dl,bl
 and dl,0f0h
 ror dl,4
 call HexToAscii
 mov ah,02h
 int 21h
 mov dl,bl
 and dl,0fh
 call HexToAscii
 mov ah,02h
 int 21h
endp
AsciiToHex proc
 cmp al,41h
 jc sk
 sub al,07h
sk: sub al,30h
 ret
endp
HexToAscii proc
 cmp dl,0ah
 jc sk2
 add dl,07h
sk2: add dl,30h
 ret
endp
Code ends
end Start


回答1:


mov di,1000h

Because in your 2 hexdigits input routine the biggest value that the user can type is "FF" (255), you could reserve a buffer of that size in your program's data section instead of just trusting that the offset address 1000h will not overlap with anything important in memory.

Data Segment
 buf db 256 dup (0)
 msg db 0dh,0ah,"Please enter the length of the array: $"
 ...

To tackle the task of getting the index of the array element where the minimum is located, you can save the current value of the address in DI in an extra register like SI and update that each time the code stumbles upon a smaller value:

 ...
 mov di, offset buf
 mov al, [di] 
 mov min, al
 mov si, di    ; Address of the first chosen minimum
chk:
 mov al, [di]
 cmp al, min
 jae a
 mov min, al   ; Smaller than anything before
 mov si, di    ; Remember the address of the newest minimum
a:
 inc di
 loop chk

After this code the index that you're looking for is obtained from subtracting the start of the array from the address of where the minimum was found:

sub si, offset buf  ; The index of the minimum

With introducing that SI address comes the opportunity of no longer needing the separate min variable:

 ...
 mov di, offset buf
 mov si, di    ; Address of the first chosen minimum
chk:
 mov al, [di]
 cmp al, [si]  ; At [si] is the current minimum
 jae a
 mov si, di    ; Remember the address of the newest minimum
a:
 inc di
 dec cx
 jnz chk
 ;;; mov al, [si]    ; The minimum should you need it
 sub si, offset buf  ; The index of the minimum

Please note that I've removed the slower loop chk instruction and replaced it by the pair dec cx jnz chk.



Here is a very, very similar answer that deals with the maximum instead of the minimum...



来源:https://stackoverflow.com/questions/65124016/finding-index-of-the-array-in-tasm-assembly-language-and-printing-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!