0

I'm having issues with my code, I'm trying to find the max value in an array but my logic isn't working. Any ideas?

; Parameter 1: RDI -- address of array (pass by reference)
; Parameter 2: RSI -- array size        (pass by value)
; Parameter 3: RDX -- maximum of an array (pass by reference)

; Register usage: RAX -- temporary maximum value
; RBX -- address of array for “register indirect memory addressing”
; RCX -- counter for “loop” instruction
findMax:
        push rbx                ; save registers
        mov rdi, 0              ; address of array (Parameter 1)-input (reference)
        mov rsi, 0              ; array size (Parameter 2)-input (value)
        mov [temp-max],ar[0]    ; initialize max (temp-max) to array[0]
myLoop:
        cmp rbx, [temp-max]     ; compare array element to temp-max
        jle next                ; jump if not greater
        mov rbx, rdx            ; new max found, update temp-max
next:   add rbx, 8              ; get address of next array element
        loop myLoop             ; repeat

        mov rdx,[temp-max]      ; store max in memory(Parameter 3)- output(reference)
        pop rbx                 ; restore registers
        ret                     ; return
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 2
    The code is incomplete. `AR` isn't defined. You cant `MOV` memory to memory (you have to move to a register and then to another memory operand). `TEMP-MAX` will appear as an expression (equation of `TEMP` minus `MAX`), maybe you meant to use `TEMP_MAX`? I don't think your can actually asemble correctly. – Michael Petch Oct 29 '22 at 01:01
  • If `temp` and `max` are two separate symbols, `[temp - max]` would take the distance between those addresses and use that number as an absolute address. Which makes no sense. If you were trying to subtract the contents of two separate variables, you need to load one into a register and then subtract the other from it, with `mov` and `sub` instructions. But the comments hint that Michael Petch's guess is correct, that you tried to include a `-` in a symbol name. Unclear why you'd want static storage in memory for that; x86-64 has 15 registers, use them. e.g. R8 is call-clobbered. – Peter Cordes Oct 29 '22 at 03:08
  • A proper [mcve] needs to quote any error messages you get, or if it runs then describe what happens and what you see with a debugger. – Peter Cordes Oct 29 '22 at 03:10
  • This code has a *many* unrelated problems, and isn't asking about any specific one. Closing as a duplicate that shows how to loop over and array and find its min or max, with lots of explanation along the way that should help you figure out most of what you were doing wrong here. – Peter Cordes Oct 29 '22 at 07:09

0 Answers0