Location and Highest value in array

做~自己de王妃 提交于 2021-02-10 12:18:41

问题


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?

code:

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

I enter 1,2,3,4,5,6,7,8,9,10

out : high value is 10 high is in location 1


回答1:


Your FindGreatest loop should do all the work instead of having a second loop (unfinished TestLoop) find out the position. Here's my suggestion:

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

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!




回答2:


First there are some readability / performance improvements:

Replace

mov ax,[ebx]
movsx eax,ax

with

movsx  eax, word [ebx]

And in this code:

FindLoop:
  cmp ax,[ebx]
  jge FindCont
  mov ax,[ebx]
FindCont:
  add ebx,2
  loop FindLoop     ; leave an empty line after the loop, not in the middle of the loop and then no space after.

  mov edx,offset HiMsg

In your loop, you're already set up to do more things when you find out that you've found a new max. e.g. save ebx into another reg, like edx. Do this with another mov instruction next to the mov ax,[ebx], so the jge will skip it, too.

At the end of the loop, you can subtract pointers and right-shift (to divide by two) to find the index where the max element was.

Or instead of storing the pointer, you could store the current value of ecx. You're using a loop-counter and a pointer increment. The loop instruction is slow, and not recommended. Your code would be better if you compared ebx to see if it's past the end of the array yet, as a loop condition.



来源:https://stackoverflow.com/questions/34032813/location-and-highest-value-in-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!