装配程序错误的结果

时间:2012-10-17 18:51:43

标签: assembly x86-64 gas

有人可以帮我找到以下程序的错误吗?我正在阅读“从头开始编程”并尝试将示例转换为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 个答案:

答案 0 :(得分:1)

您正在使用movq从列表中移动包含32位值的64位值。这会给你错误的结果。定义你的列表来保存64位值:用.quad替换.long并在movs中用8替换4。