I correctly guessed missing part of a function, but gcc generated assembly code doesn't match the answer

ε祈祈猫儿з 提交于 2020-01-24 21:02:09

问题


I'm going though 'Computer Systems: A Programmer's Perspective', chapter 3 which is about machine-level representation. I'm currently on 'Arithmetic and Logic Operations', doing one of the practice problem. The problem gives C code fragment:

short scale3(short x, short y, short z){
    short t = ----------------;
    return t;
}

additionally i'm given an assembly code, generated from the COMPLETE code in scale3 function:

scale3:
    leaq (%rsi, %rsi, 9), %rbx
    leaq (%rbx, %rdx), %rbx
    leaq (%rbx, %rdi, %rsi), %rbx
    ret

From the generated assembly code i'm supposed to full-fill missing part in the C source code. According to X86-64 convention, i followed the rules of memory referencing and easily fond that, t(in the C code) should be assigned to -> t = 10 * y + z + y * x;. I checked the answer and it matches. Out of curiosity, i tested the code and the code generated from the function didn't match the given assembly code.

I'm working on 64-bit Linux machine, and my GCC generated the following code:

scale3:
    leal  10(%rdi), %eax
    imull %eax, %esi
    leal  (%rsi,%rdx), %eax
    ret

Can anybody explain to my why i get different representations? This is probably 15th+ generate code and i've never got different result compared to the book.

来源:https://stackoverflow.com/questions/57772527/i-correctly-guessed-missing-part-of-a-function-but-gcc-generated-assembly-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!