2021年4月4日星期日

Updating an array with a For loop in ARMv8

I have an assignment to convert C code into ARMv8 assembly code. The C code snippet I was given is as follows:

int m=5;  int k;  int a[5];  for(int k=0; k<5;k++){     m=m+k;     a[k]=k;  }  printf("m=%d\n",m);  printf("k=%d\n",k);  

So basically I have to run a for loop to populate an array with the corresponding index value, as well as increase the value of M by the index value. This is what I tried:

.text  .global main    main:     adr x1, array     mov x2, array_length     mov x3, #0     mov x4, xzr     adr x5, m    loop:     cmp x3, x2     bge done     add x4, x1, x3, lsl #3     str x3, [x4]     add x5, x5, x3     add x3, x3, #1     b loop    done:     mov x1, x5     adr x0, str1     bl printf       mov x1, x4     adr x0, str2     printf    exit:     mov x8, #93     svc 0    .data  .set array_length, 5  m: .word 5  str1: .asciz "m=%d\n"  str2: .asciz "k=%d\n"  array: .dword 0, 0, 0, 0, 0  

It compiles and executes fine, but the values it gives me are very wrong (in the thousands when they should be single or double digits). I based my code on a similar example given by the professor and those were the values she used so I'm not sure how it's coming out so wrong.

https://stackoverflow.com/questions/66947492/updating-an-array-with-a-for-loop-in-armv8 April 05, 2021 at 10:03AM

没有评论:

发表评论