Warm tip: This article is reproduced from serverfault.com, please click

assembly-如何显示在tasm程序中找到的元素的索引

(assembly - how to show the index of element found in tasm program)

发布于 2020-12-03 07:57:21

如何显示找到的元素的索引号?我已经从数组中找到了最大的元素,现在我想打印该元素的索引,该索引已经找到了如何进行?

我想根据以下逻辑找到我在数组中找到的最大数量的元素?

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
Questioner
Desmond9989
Viewed
11
Sep Roland 2020-12-04 05:02:57

你可以找到一个非常,非常类似的答案在这里如何离奇的是一个涉及最大和最小的其他交易...



mov di,1000h

因为在你的2个十六进制输入例程中,用户可以键入的最大值是“ FF”(255),所以你可以在程序的数据部分中保留该大小的缓冲区,而不仅仅是相信偏移量地址1000h不会与任何内容重叠在记忆中很重要。

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

为了解决获取最大值所在的数组元素的索引的任务,你可以将地址的当前值保存DI在一个类似的额外寄存器中,SI并在每次代码偶然发现一个更大的值时进行更新:

 ...
 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

在此代码之后,你要查找的索引是从找到最大值的地址中减去数组的开头而获得的:

sub si, offset buf  ; The index of the maximum

引入该SI地址后,就不再需要单独的max变量了:

 ...
 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

请注意,我已经删除了较慢的loop chk说明,并用一对替换dec cx jnz chk