найти наибольшее по модулю значение элемента массива. У меня удалось осуществить только поиск наибольшего значения. Что нужно исправить ? NASM x86. Код: 
                  
                  
                  
                  
                  
                  section .data
                  
                  
                  
                  
                  
                  array:
                  
                  
                      dd 3,67,34,222,45,75,54,34,44,33,22,11,66,0
                  
                  
                  
                  
                  
                  section .text
                  
                  
                  global _start
                  
                  
                  
                  
                  
                  _start:
                  
                  
                      mov edi, 0 ; set the index to 0
                  
                  
                      mov eax, [edi * 4 + array] ; set eax to the first element
                  
                  
                      mov ebx, eax ; set the max to the first element to start with
                  
                  
                  
                  
                  
                  start_loop:
                  
                  
                      cmp eax, 0 ; see if we're at the last element, 0
                  
                  
                      je loop_exit ; if we are, jump to the loop exit
                  
                  
                      inc edi ; increment the index
                  
                  
                      mov eax, [edi * 4 + array] ; set the current element to the new element
                  
                  
                      cmp eax, ebx ; compare eax to ebx
                  
                  
                      jle start_loop ; if the new element is less than the max, continue the looping
                  
                  
                      mov ebx, eax ; otherwise set the new max to the current element
                  
                  
                      jmp start_loop ; whether or not we executed the previous instruction, we
                  
                  
                      ; still need to go back to the beginning of the loop
                  
                  
                  
                  
                  
                  loop_exit: ; return the max element using the exit system call
                  
                  
                      mov eax, 1 ; set the system call to exit (1)
                  
                  
                      int 80h ; ebx is already set to the max element
                  
                  
                
Тут числа положительные все, какие модули?
Короче, я бы инициализировал ebx нулём, а не первым элементом, чтобы два раза кусок с neg не писать. И ещё я бы заменил jle на jbe. И тут je вместо jne.
да, наконец-то до меня дошло
xor edi, edi xor ebx, ebx _loop: mov eax, [edi×4+array] test eax, eax jns _positive neg eax _positive: cmp eax, ebx cmova ebx, eax inc edi or eax, eax jne _loop ret array: dd 3, -67, -34, 222, -45, 75, 777, -443, 634, -666, 32, 0
О, а что делает инструкция cmova? тоже флаги устанавливает ?
можно избавиться от страшной строчки: edi×4+array... xor ebx, ebx mov esi, array _loop: lodsd and eax, eax jns _positive neg eax _positive: cmp eax, ebx cmova ebx, eax or eax, eax jne _loop ret array: dd 1,2,-3,-5,4,0
Обсуждают сегодня