Is it possible to use a register other than %rax for returning from a function?

Viewed 322

In the textbook Computer Systems A Programmer's Perspective 3e p259, there is a C code:

short dw_loop(short x)
{
    short y = x/9;
    short *p = &x;
    short n = 4*x;
    do
    {
        x += y;
        (*p) += 5;
        n -= 2;
    } while ( n > 0);
    return x;
}

Here is the equivalent assembly code:

dw_loop:
    movq %rdi, %rbx
    movq %rdi, %rcx
    idivq $9, %rcx
    leaq (, %rdi, 4), %rdx
.L2:
    leaq 5(%rbx, %rcx), %rcx
    subq $1, %rdx
    testq %rdx, %rdx
    jg .L2
    rep; ret

My questions are: Shouldn't C code be like y += x instead of x +=y;

Also there is no usage of %rax, as far as I know functions must return their value in register %rax in the x86_64 architecture ? Am I wrong ?

0 Answers
Related