2021年4月5日星期一

movslq messed up the summation?

I have the following code, it is so strange and I do not know what is going on:

I have a struct in C, and I initialized the device in C, and calling the assembly function "calibrate" that take the point of device in the parameter as following:

struct Device  {      char name[20];      short adjustments[8];      short avg;  };    // asm function  unsigned short calibrate(struct Device *thing);  

the assembly function calibrate calculates the average of the adjustment array, and update avg with the calculated average and returns the sum. The following is my implementation:

    movq $0, %r10   #this is the index of the array      movq $0, %r11   #this stores the sum  loop:      movslq 20(%rdi, %r10, 2), %rax      addq %rax, %r11      incq %r10      cmpq $8, %r10      je exit  jmp loop  exit:      movq %r11, %rax      sarq $3, %rax      movl %eax, 36(%rdi)  # update the average with sum/8      movq %r11, %rax      # return the sum  

Strangely, the sum is correctly returned as 65276, but the average is 24543 instead of the correct value of 8159, any idea why? I think the problem might be that movslq messed up the summation? but why the sum is correct?

I am really lost here, and any help is really appreciated!

https://stackoverflow.com/questions/66960438/movslq-messed-up-the-summation April 06, 2021 at 06:14AM

没有评论:

发表评论