数组中的位置和最大值 [英] Location and Highest value in array
问题描述
所以我正在尝试学习汇编,我的练习表中有一个示例,我必须创建一个程序来一次将 10 个数字输入一个数组.我必须打印最高值和输入时间.我在比较中几乎没有任何经验,但我想以某种方式存储高值并将其与位置进行比较?
So i am trying learn assembly and my practice sheet has an example where i have to create a program to input 10 numbers one at a time into an array. I have to print the highest value and when it was entered. I have barely any exp in comparing but I want to somehow store the high value and compare it the locations?
代码:
include irvine32.inc
.data
num dw 10 dup(0)
count db 1
prompt db "Enter a number: ",0
yesMsg db "hi val is in location ",0
hiMsg db "The high value is ",0
.code
main proc
mov ebx,offset num
mov ecx,10
LOOP1:
mov edx,offset prompt
call writestring
call readint
mov [ebx],ax
add ebx,2
loop LOOP1
mov ecx,10
mov ebx,offset num
sub eax,eax
call crlf
LOOP2:
mov ax,[ebx]
call writeint
call crlf
add ebx,2
loop LOOP2
mov ebx,offset num
mov ecx,lengthof num
FindGreatest:
call crlf
mov ebx,offset num
mov ecx,lengthof num
mov ax,[ebx]
movsx eax,ax
FindLoop:
cmp ax,[ebx]
jge FindCont
mov ax,[ebx]
FindCont:
add ebx,2
loop FindLoop
mov edx,offset HiMsg
call writestring
call writedec
call crlf
TestLoop:
mov eax,ebx
cmp [ebx],ax
je IsHighNum
IsHighNum:
mov edx,offset yesMsg
call writestring
movsx eax,count
call writedec
call crlf
ENDITALL:
exit
main endp
end main
<小时>
我输入 1,2,3,4,5,6,7,8,9,10
I enter 1,2,3,4,5,6,7,8,9,10
out : 高值为 10高在位置 1
out : high value is 10 high is in location 1
推荐答案
你的 FindGreatest 循环应该完成所有工作,而不是有第二个循环(未完成的 TestLoop) find出位置.这是我的建议:
Your FindGreatest loop should do all the work instead of having a second loop (unfinished TestLoop) find out the position. Here's my suggestion:
至少在将 jge
更改为 jg后,强制此代码将当前位置(保存在 EDX 中)存储到 count 变量中代码>.
通过使用当前位置,可以不再使用 ECX 寄存器进行循环控制.
To force this code to store the current position (held in EDX) into the count variable at least once I changed the jge
to a jg
.
By using a current position it was possible to no longer use the ECX register for the loop control.
FindGreatest:
call crlf
mov ebx, offset num
xor edx, edx
mov ax, [ebx]
FindLoop:
inc edx ;Positions 1-10
cmp ax, [ebx]
jg FindCont
mov ax, [ebx]
mov count, edx ;Remember position of greatest
FindCont:
add ebx, 2
cmp edx, lengthof num
jb FindLoop
现在您已准备好在 AX 中输出结果和 count 中的位置.
请记住使用 movsx eax, ax
对 AX 进行符号扩展.你不可能在循环之前 这样做,因为如果第一个数字是负数,而数组的其余部分至少包含一个正数,那么输出就会出错!
Now you're ready to output the result in AX and the position in count.
Remember to sign-extend AX using movsx eax, ax
. You could not have done this before the loop because if the first number were negative and the rest of the array contained at least one positive number then the output would have been wrong!
这篇关于数组中的位置和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!