I wrote a very simple ARM64 assembler function:
Like this:
.global asmQuickSort \
asmQuickSort: \
1 sub x2, x1, #1 \
2 mov x1, #0 \
3 sub sp, sp, #8 \
4 str w1, [sp] \
5 add sp, sp, #8 \
6 ret
and main cpp to call it:
int main(int argc ,char** argv){ \
int a[7] = {5,4,3,4,3,2,1}; \
asmQuickSort(a,7); \
for(int i=0;i< 7;i ){ \
printf("%d\n",a[i]); \
} \
return 1;\
}
But, when I put it into aarch64 board and run it ,it met
sigbus error at line 4 str w1, [sp]
This executable file ran well using qemu-aarch64 ,but in arm board it received sigbus.
So, why does the app receive sigbus error?
And what should I to check?
CodePudding user response:
It's probably stack alignment.
The manual has this to say on "Load/store addressing modes" (C1.3.3):
When stack alignment checking is enabled by system software and the base register is the SP, the current stack pointer must be initially quadword aligned, that is aligned to 16 bytes. Misalignment generates a Stack Alignment fault. The offset does not have to be a multiple of 16 bytes unless the specific load/store instruction requires this.
It sounds like stack alignment checking is enabled on your OS, and you misalign the stack pointer by adding 8 to it. So change that value to 16 and you should be good.
And I'm assuming you're aware of this, but just in case: your current stack usage inside asmQuickSort
serves no purpose anyway.