在 tasm 汇编语言中查找数组的索引并打印它 [英] finding index of the array in tasm assembly language and printing it
问题描述
我做了一个tasm汇编语言程序,它在用户输入的数组中找到最小值.我想找到程序正在查找的最小值的元素的索引.
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.
例如:输入数组是[1,2,3,4,5,6].它应该返回 1 作为最小值,0 作为索引.
For example: input array is [1,2,3,4,5,6]. It should return 1 as minimum value and 0 as index.
这是代码.
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
推荐答案
mov di,1000h
因为在您的 2 个十六进制数字输入例程中,用户可以输入的最大值是FF".(255),您可以在程序的数据部分保留一个该大小的缓冲区,而不是仅仅相信偏移地址 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: $"
...
为了解决获取最小值所在数组元素的索引的任务,可以将DI
中地址的当前值保存在一个额外的寄存器中,如SI
并在每次代码偶然发现较小的值时更新:
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
通过引入 SI
地址带来了不再需要单独的 min 变量的机会:
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
请注意,我删除了较慢的 loop chk
指令并将其替换为 dec cx
jnz chk
对.
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...
这篇关于在 tasm 汇编语言中查找数组的索引并打印它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!