Left For Archive
Left For Archive

Reputation: 2666

Segfault running cmp 'A', %al‽

For my own sick pleasure, I'm writing a small program in x86_64 assembly for Linux. However, I've encountered a segfault that makes absolutely no sense to me, in an instruction comparing an immediate operand to a register. What gives?

Here's the code leading up to the crash:

_start:
    sub $8, %rsp
    mov %rsp, %rbx
    lea le_string(%rip), %rsi
    mov %rsi, %rdi
    add $8, %rdi
    mov $26, %cl
    mov (%rsi), %al
    cmp 'A', %al /* This line segfaults */
    /* snip code that never runs */

le_string:
.ascii "YrFgevat"

I'm assembling with gcc -nostdlib, which is calling the GNU assembler.

Dumping the registers after the crash reveals:

Please ignore the lack of normal calling conventions—I'm not calling out to anything besides the syscall interface, and this crashes before it's even gotten that far!

Upvotes: 1

Views: 787

Answers (1)

Left For Archive
Left For Archive

Reputation: 2666

'A' is being interpreted as an address after all. If you want to use it as a constant, you need to write:

cmp $'A', %al

Upvotes: 5

Related Questions