如何显示在 tasm 程序中找到的元素的索引 [英] how to show the index of element found in tasm program
问题描述
如何显示我找到的元素的索引号?我已经找到了数组中的最大元素,现在我想打印我发现如何进行的元素的索引?
我想根据以下逻辑找到我在数组中找到的最大数字的元素?
数据段msg db 0dh,0ah,"请输入数组的长度:$";msg1 db 0dh,0ah,"请输入一个数字:$";newl db 0dh,0ah,"$"res db 0dh,0ah,"最大值为:$";连数据库?最大数据库?数据结束代码段假设 CS:Code,DS:Data开始:移动斧头,数据mov DS,axmov dx,offset msg移动啊,09h21 小时呼叫接受mov len,blmov cl,blmov ch,00hmov di,1000h返回:mov dx,偏移 msg1移动啊,09h21 小时呼叫接受mov [di],bl公司环回mov di,1000hmov cl,lenmov ch,00hmov dx,offset newl移动啊,09h21 小时mov al,[di]最大移动chk: mov bl,maxmov al,[di]cmp bl,aljcmov max,bljmp ba: mov max,alb: 公司目录循环chkmov dx,offset res移动啊,09h21 小时移动 bl,max调用 DispNum移动啊,4ch21 小时接受程序移动啊,01h21 小时调用 AsciiToHex滚动,4mov bl,al移动啊,01h21 小时调用 AsciiToHex添加 bl,al回复结束DispNum 过程mov dl,bl和 dl,0f0h错误 dl,4调用 HexToAscii移动啊,02h21 小时mov dl,bl和 dl,0fh调用 HexToAscii移动啊,02h21 小时结束AsciiToHex 过程cmp al,41hjc sksub al,07hsk: sub al,30h回复结束HexToAscii 过程cmp dl,0ahjc sk2添加 dl,07hsk2: 添加 dl,30h回复结束代码结束结束 开始
你可以找到一个非常非常相似的答案 这里 一个处理最大值而另一个处理最小值是多么奇怪......
<块引用>
mov di,1000h
因为在您的 2 个十六进制数字输入例程中,用户可以输入的最大值是FF".(255),您可以在程序的数据部分保留一个该大小的缓冲区,而不是仅仅相信偏移地址 1000h 不会与内存中的任何重要内容重叠.
数据段buf db 256 重复 (0)msg db 0dh,0ah,"请输入数组的长度:$";...
为了解决获取最大值所在数组元素的索引的任务,可以将DI
中地址的当前值保存在一个额外的寄存器中,如SI
并在每次代码偶然发现更大的值时更新:
<代码> ...mov di, 偏移缓冲区mov al, [di]最大移动mov si, di ;第一个选择的最大值的地址查克:mov al, [di]cmp al, 最大值jbe a移动最大, al ;比以前任何东西都大mov si, di ;记住最新最大值的地址A:公司循环chk
在此代码之后,您要查找的索引是通过从找到最大值的地址中减去数组的开头获得的:
sub si, offset buf ;最大指数
引入 SI
地址后,不再需要单独的 max 变量:
<代码> ...mov di, 偏移缓冲区mov si, di ;第一个选择的最大值的地址查克:mov al, [di]cmp al, [si] ;在 [si] 是当前最大值jbe amov si, di ;记住最新最大值的地址A:公司十二月jnz chk;;;mov al, [si] ;您需要的最大值sub si, 偏移量 buf ;最大指数
请注意,我删除了较慢的 loop chk
指令并将其替换为 dec cx
jnz chk
对.
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
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
.
这篇关于如何显示在 tasm 程序中找到的元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!