简单的x86-64汇编算法

时间:2012-11-30 20:38:06

标签: assembly

我正在尝试为评论中的公式编写汇编语言代码。我遇到了麻烦,因为指向数组的指针是64位寄存器,我应该将最终结果存储在32位寄存器中,所以我显然缺少对寄存器如何工作的一些基本理解。我包含了我尝试过的解决方案,但是当我尝试使用movl或subl时,我得到错误,其中一个参数是64位寄存器而另一个是32位。我也不确定我的推理是否正确。任何帮助,将不胜感激。

#    WRITEME: At this point, %r12 and %rbx are each pointers to two arrays
#   of 3 ints apiece, a0 and a1.  Your job is to write assembly language code
#   in the space below that evaluates the following expression, putting the
#   result in register %esi (the 32 bit form of register %rsi):
#   
#    (a1[0]-a0[0]) * (a1[0]-a0[0]) + 
#    (a1[1]-a0[1]) * (a1[1]-a0[1]) + 
#    (a1[2]-a0[2]) * (a1[2]-a0[2])
#   
#    
#   It is possible to do this using 11 instructions. You need to use extra
#   registers, of course. Since  you are not calling any functions, you have a
#   lot of choices. %r12 and %rbx are already occupied, but you could use
#   %r13d through %r15d (32 bit forms of %r13 through r15) safely, and also
#   %eax, %ecx, %edx and of course %esi, since that is where the result will
#   be stored.

###########

# Your code here
movl $0, %esi
movl %rbx, %eax #errors here
subl %r12, %eax #and here
imull %eax, %eax
movl 4(%rbx), %ecx
subl 4(%r12), %ecx
imull %ecx, %ecx
movl 8(%rbx), %edx
subl 8(%r12), %edx
imull %edx, %edx
movl %eax, %esi
addl %ecx, %esi
addl %edx, %esi

1 个答案:

答案 0 :(得分:3)

movl %rbx, %eax表示:'将rbx的内容复制到eax',但是由于rbx是64位寄存器,而eax是32位寄存器,因此会失败。我认为你要做的是'将rbx指向的内存内容复制到eax':movl rbx %eax