1

I have the following code already written and everything should be good. It suppose to find biggest and smallest values in array of size 4 using loop.

I got the loops setup and everything except my output is not correct. I have a bug in line 46 that when load the array the value into register X11 I get big weird number and causes the whole calculations to be wrong.

Please see if you can suggests a fix in this code.

//X0 Array, X17 number of runs, X11 Biggest, X12 Smallest

ADR X0,v
LDR X17,=4
LDR X11,[X0],#3
MOV X12,X11
loop:
    **LDR X11,[X0],#3 //Line 46**
    //LSL X11,X0,#3
    CMP X12,X11
    BLT loop1
    SUB X17,X17,#1
    CMP X17,#1
    BEQ exit
    BGT loop
loop1:
    MOV X12,X11
    SUB X17,X17,#1
    CMP X17,#1
    BEQ exit
    BGT loop

I'm using the DS-5 IDE, in case that matters.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Zed-4
  • 13
  • 2
  • What is `v`? Make sure it's 64 bit integers not 32. – Jester Mar 19 '21 at 23:06
  • BTW, if you make counter equal 3 (number of actual iterations) `LDR X17,=(4-1)`, than instead of `SUB X17,X17,#1; CMP X17,#1; BGT loop` you could use `SUB X17,X17,#1; CBNZ loop` – user3124812 Mar 19 '21 at 23:29
  • Oh I see thanks, will try! – Zed-4 Mar 19 '21 at 23:33
  • @user3124812: IIRC, `cbnz` can only jump forwards. But you can use `subs x17, x17, #1` / `bne loop` to jump backwards on the flags result of SUBS. – Peter Cordes Mar 20 '21 at 01:47
  • Also note, AArch64 has handy instructions like `csel` to select one of two registers based on a condition. Actually easier to use than jumping over a `mov` to update your min and max candidates. (AArch64 also has SIMD integer min and max instructions, which might even be available for a single 64-bit integer in the bottom of a 16-byte vector.) – Peter Cordes Mar 20 '21 at 03:05
  • 3
    @PeterCordes: `cbnz` can jump backwards or forwards, a good long ways. It takes a sign-extended 19-bit displacement. – Nate Eldredge Mar 20 '21 at 03:40
  • @PeterCordes: The SIMD `smax` and `smaxv` unfortunately don't appear to support the `2D` arrangement so we can't use them on 64-bit values, at least not in a straightforward way. – Nate Eldredge Mar 20 '21 at 03:53
  • @NateEldredge: Oh good, I guess I was thinking of the ARM Thumb version. Is there any reason to prefer cbnz over using subs/bne flags? I guess if you want to keep flags unmodified. – Peter Cordes Mar 20 '21 at 09:32
  • @PeterCordes: Good question. I don't really know whether there'd be a performance difference, for example. – Nate Eldredge Mar 20 '21 at 13:29

1 Answers1

3

LDR X11,[X0],#3

Reading 64bit values (Xregister), and post-incrementing by #3? I have a great suspicions that 3Bytes != 64bit.

user3124812
  • 1,861
  • 3
  • 18
  • 39
  • ‍♂️, ‍♂️, ‍♂️, ‍♂️, ‍♂️ You both are %100 correct. I was suppose to use #8. life savor thanks! – Zed-4 Mar 19 '21 at 23:17