2012-10-17 100 views
0

有人可以幫我找出下列程序有什麼問題嗎?我正在閱讀「從頭開始編程」,並試圖將這些示例翻譯成x86-64彙編。以下程序將查找一組數據中的最大數字。但是當我組裝,鏈接和運行時,我得到了0.顯然這不是最大的數字。它使用32位寄存器/指令運行正常,但不是64位。彙編程序錯誤結果

# PURPOSE: This program finds the largest value in a set of data. 
# 
# 
# VARIABLES: %rax holds the current value. %rdi holds the largest 
#   value. %rbx holds the current index. data_items is the 
#   actual set of data. The data is terminated with a 0. 
# 

.section .data 

data_items: 
    .long 76, 38, 10, 93, 156, 19, 73, 84, 109, 12, 21, 0 

.section .text 
.globl _start 
_start: 
    movq $0, %rbx 
    movq data_items(, %rbx, 4), %rax 
    movq %rax, %rdi 

loop_start: 
    cmpq $0, %rax      # Have we reached the end? 
    je loop_end 
    incq %rbx       # Increment the index. 
    movq data_items(, %rbx, 4), %rax # Load the next value. 
    cmpq %rdi, %rax     # Is new value larger? 
    jle loop_start 
    movq %rax, %rdi     # New val is larger, store 
             # it. 
    jmp loop_start 

loop_end: 
    # The largest value is already in %rdi and will be returned as 
    # exit status code. 
    movq $60, %rax 
    syscall 

回答

1

您正在使用movq從您的列表中移動包含32位值的64位值。這會給你錯誤的結果。定義你的列表來保存64位值:用.quad替換.long,並在movs中用8替換4。

+0

好的,謝謝。我認爲這可能是問題,但我不知道64位值的指令是什麼。在這種情況下使用movq代替movl和.long值有沒有什麼好處? –