0

I wrote a simple assembly program, a.c, with the intention of using printf to output a double value. However, when I execute gcc a.c && ./a.out, the program generates a segmentation fault.

.section .data
d1:
    .double 2.3 
mystr:
    .asciz "The value is %f\n"
.section .text
.global main
.type main, @function
main:
    vmovsd d1, %xmm0
    movb $1, %al 
    movq $mystr, %rdi
    call printf

    movl $0, %eax
    call exit

When I debugged the program using gdb, I found that the program generates a SIGSEGV signal at the line 0x7fc09f42a474 <printf+36>: movaps %xmm0,0x50(%rsp). However, this line seems to be simply storing the value from the %xmm0 register onto the stack memory. I don't understand why this is happening.

➜  aux git:(master) ✗ gdb -q a.out core.3963
Reading symbols from a.out...
[New LWP 3963]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007fc09f42a474 in printf () from /lib64/libc.so.6
(gdb) x/15i printf
   0x7fc09f42a450 <printf>: sub    $0xd8,%rsp
   0x7fc09f42a457 <printf+7>:   test   %al,%al
   0x7fc09f42a459 <printf+9>:   mov    %rsi,0x28(%rsp)
   0x7fc09f42a45e <printf+14>:  mov    %rdx,0x30(%rsp)
   0x7fc09f42a463 <printf+19>:  mov    %rcx,0x38(%rsp)
   0x7fc09f42a468 <printf+24>:  mov    %r8,0x40(%rsp)
   0x7fc09f42a46d <printf+29>:  mov    %r9,0x48(%rsp)
   0x7fc09f42a472 <printf+34>:  je     0x7fc09f42a4ab <printf+91>
=> 0x7fc09f42a474 <printf+36>:  movaps %xmm0,0x50(%rsp)
   0x7fc09f42a479 <printf+41>:  movaps %xmm1,0x60(%rsp)
   0x7fc09f42a47e <printf+46>:  movaps %xmm2,0x70(%rsp)
   0x7fc09f42a483 <printf+51>:  movaps %xmm3,0x80(%rsp)
   0x7fc09f42a48b <printf+59>:  movaps %xmm4,0x90(%rsp)
   0x7fc09f42a493 <printf+67>:  movaps %xmm5,0xa0(%rsp)
   0x7fc09f42a49b <printf+75>:  movaps %xmm6,0xb0(%rsp)
(gdb)
Jester
  • 56,577
  • 4
  • 81
  • 125
  • 2
    `movaps` is a hint. The `a` means aligned. You misaligned the stack. You need to adjust it by 8 to account for the return address to maintain 16 byte alignment as required by the ABI. – Jester Jun 12 '23 at 10:04

0 Answers0