2

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 ?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Dot.Volare
  • 118
  • 11
  • While I can understand some of the optimizations that a compiler would have made, I am hard pressed to believe that GCC generated the code. For instance `idivq $9, %rcx` isn't even valid as IDIV takes a single parameter. RAX would usually be part of the dividend in a division but it is never set. RAX would normally be the quotient of division but the division itself makes no sense. – Michael Petch Nov 22 '20 at 15:45
  • 4
    Although some may not necessarily see this as a duplicate of this question (I'd be inclined to actually consider it a duplicate), this other question and the answer are related https://stackoverflow.com/questions/57998998/csapp-example-uses-idivq-with-two-operands . One can observe that they come to the same conclusion I did - this code and what it produces make no sense and is in error. – Michael Petch Nov 22 '20 at 15:49
  • 2
    I've decided to close this as a duplicate since the code produced is nonsense and that is really covered by the question/answer I am making this a duplicate of. If someone disagrees please be my guest to reopen. – Michael Petch Nov 22 '20 at 15:53
  • I have reviewed the question you provided, and I understood that this problem has many issues (Thank you for that). Although what I would like to learn is that is it certain that %rax will contain the return value of a function? – Dot.Volare Nov 22 '20 at 18:47
  • 3
    In x86-64 code, any integer class return type that can fit in 64-bits (including pointers) will be returned in RAX. Floats and doubles will be return in XMM0. Returning structures by value that are greater than 64 bits in size (8 bytes) have a set of rules for them that you would have to read the [AMD64 System V ABI](https://www.uclibc.org/docs/psABI-x86_64.pdf). The ABI tells you how values are returned from a function. – Michael Petch Nov 22 '20 at 19:27
  • 1
    @Dot.Volare: You *can* invent your own calling conventions for "private" functions in hand-written asm that don't need to be called from compiler-generated code. [When to use a certain calling convention](https://stackoverflow.com/q/64149600). Otherwise, with a good calling convention like x86-64 System V, usually not worth the trouble to do anything other than following the ABI. – Peter Cordes Nov 23 '20 at 02:25

0 Answers0