2012-11-30 41 views
1

我想在評論中寫公式的彙編語言代碼。我遇到了麻煩,因爲指向數組的指針是64位寄存器,我應該將最終結果存儲在一個32位寄存器中,所以我顯然缺少對寄存器工作原理的一些基本理解。我包含了我的嘗試解決方案,但當我嘗試使用movl或subl時,一個參數爲64位寄存器,另一個參數爲32位時出現錯誤。我也不確定我的推理是否正確。任何幫助,將不勝感激。簡單的x86-64彙編算術

# 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 
+0

聽起來好像你在混淆64位寄存器的內容和那些寄存器中地址所指向的內容。如果我正確理解你的問題,如果你使用64位寄存器來獲取數據(這可能只有32位)並總結,你應該沒問題。 – RonaldBarzell

回答

3

movl %rbx, %eax指:「RBX的內容複製到EAX」,但是由於RBX是一個64位寄存器,而EAX是一個32位寄存器,這將失敗。我認爲你的意思是'將rbx指向的內存內容複製到eax'中:movl rbx %eax