how to show the index of element found in tasm program

空扰寡人 提交于 2020-12-13 20:58:28

问题


how can i display index number of the element which i have found? i have found out the maximum element from the array and now I want to print the index of the element which I have found how to proceed?

i want to find the element of the largest number which i found in the array according to the below logic ?

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 maximum is: $"
 len db ?
 max 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 max,al

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

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

 mov bl,max
 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:


You can find a very, very similar answer here How bizarre that one deals with the maximum and the other deals with the minimum...



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 maximum 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 bigger value:

 ...
 mov di, offset buf
 mov al, [di] 
 mov max, al
 mov si, di    ; Address of the first chosen maximum
chk:
 mov al, [di]
 cmp al, max
 jbe a
 mov max, al   ; Bigger than anything before
 mov si, di    ; Remember the address of the newest maximum
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 maximum was found:

sub si, offset buf  ; The index of the maximum

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

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

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



来源:https://stackoverflow.com/questions/65121872/how-to-show-the-index-of-element-found-in-tasm-program

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