Q:

Segmentation-fault caused by cmp

I currently trying to learn assembly language. But I'm stuck.
Suppose I have this C code:
for ( int i = 100; i > 0; i-- ) {
  // Some code
}

Now I want to do the same in assembly language. I tried it like this:
__asm__ ("movq $100, %rax;"
        ".loop:"
        //Some code
        "decq %rax;"
        "cmpq $0, (%rax);"
        "jnz .loop;"
);

Compile and run results in a seg fault. It does not seg fault if I remove the cmpq line. But then of course the program will not terminate.
So basically my question is what am I doing wrong here?
Thanks in advance.

A:

The following instruction: 
cmpq $0, (%rax)

is accessing the memory address specified by the rax register, whose value is 99.
The first memory page is not mapped. That memory address, 99, belongs to the first memory page. Therefore, the access above results in a segmentation fault.

You don't want the indirection, instead you want:
cmpq $0, %rax

That is, you want to compare against the contents of rax, not the contents at the memory address specified by rax.

Consider however optimizing the cmp instruction away: 
decq %rax is immediately preceding the cmp $0, %rax instruction, which sets ZF if rax is zero. The conditional jump is then performed based on the state of the ZF flag:
decq %rax
cmpq $0, %rax
jnz .loop

The dec instruction affects the ZF flag (as cmp does), so if decrementing rax results in zero, ZF will be set. You can take advantage of that fact and place jnz directly after dec. You don't need cmp at all:
decq %rax
jnz .loop

A:

cmpq $0, (%rax)

This instruction will try to read memory at the address in rax.
rax will be 99 the first time. Address 99 is not mapped in, so your program segfaults.
You are intending to compare the value in rax to 0, so remove the parentheses.
cmpq $0, %rax

A:

Remove parenthesis from %rax to get the value of rax. Adding the parenthesis basically tells the assembler, "hey! rax holds an address, please return the contents in that address".
Therefore,
cmpq $0, %rax

is what you need to do.

