数组中的位置和最高值 [英] Location and Highest value in array

查看:92
本文介绍了数组中的位置和最高值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试学习汇编,而我的练习表中有一个示例,其中我必须创建一个程序以一次将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆