x86 64 - x86_64 assembly %rsp vs %esp -
i have been playing assembly recently, , came across strange bug in program. have found if modify %rsp
doing 64-bit math, works fine, if modify %esp
same amount, except 32-bit math, segmentation fault. tried printing out both %esp
, %rsp
, , same every time run.
question: why matter whether 64-bit math or 32-bit math when whole register using 32 bits?
.cstring _format: .asciz "%d\n" .text .globl _main _main: # program setup pushq %rbp movq %rsp, %rbp # program - 16 byte aligned @ point # print stack pointer memory movq %rsp, %rax call bob # prints same value next call bob xorq %rax, %rax movl %esp, %eax call bob # prints same value previous call bob # code breaks subl $16, %esp # bug here if use (32 bit math) subq $16, %rsp # works fine if use (64 bit math) call bob addq $16, %rsp # program cleanup movq %rbp, %rsp popq %rbp ret # assumes 16 byte aligned when called. prints %rax bob: subq $8, %rsp movq %rax, %rsi lea _format(%rip), %rdi call _printf addq $8, %rsp ret
in x86_64, addresses 64 bits, how can expect 32-bit math on , still working fine? 32-bit operations on x86_64 0 out top 32 bits, render address invalid
Comments
Post a Comment