Reputation: 1229
I am trying to write assembly language code for the formula in the comment. I am having trouble because the pointers to the arrays are 64 bit registers, and I am supposed to store the final result in a 32 bit register, so I am obviously missing some fundamental understanding of how registers work. I included my attempted solution, but I get errors when I try to use a movl or subl where one argument is a 64 bit register and the other is 32 bits. I'm also not certain if my reasoning is even correct. Any help would be appreciated.
# 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
Upvotes: 1
Views: 638
Reputation: 4914
movl %rbx, %eax
means: 'copy the contents of rbx into eax', however since rbx is a 64-bit register, while eax is a 32-bit register this will fail. I think what you mean to do is 'copy the contents of memory pointed to by rbx into eax': movl rbx %eax
Upvotes: 3