我一直在阅读“从头开始编程”这本书来学习 Linux 中的汇编编程。我在解决第 3 章末尾的一个练习时遇到了麻烦。练习说修改以下程序以使用数据集末尾的地址来终止循环。这只是一个在一组数据中查找最大数的简单程序,它目前只是使用数字零来标记数据的结尾。
#PURPOSE: This program finds the maximum number of a
# set of data items.
#
#VARIABLES: The registers have the following uses:
#
# %edi -- Holds the index of the data item being examined
# %ebx -- Largest data item found
# %eax -- Current data item
#
# The following memory locations are used:
#
# data_items -- Contains the item data. A 0 is used
# to terminate the data.
#
.section .data
data_items:
.long 3, 67, 34, 14, 45, 75, 54, 187, 44, 87, 22, 11, 66, 0
.section .text
.globl _start
_start:
movl $0, %edi # Move 0 into the index register
movl data_items (, %edi, 4), %eax # Load the first byte of data
movl %eax, %ebx # The biggest
start_loop:
cmpl $0, %eax # Check to see if we've hit the end
je loop_exit
incl %edi # Load next value
movl data_items (, %edi, 4), %eax
cmpl %ebx, %eax # Compare values
jle start_loop # Jump to the beginning if new value
# Isn't larger
movl %eax, %ebx # Move the value as the largest
jmp start_loop # Jump to the beginning of loop
loop_exit:
# %ebx is the status code for the exit system call
# and it contains the maximum number
movl $1, %eax # 1 is the exit() system call
int $0x80
我知道我可以硬编码数据列表的长度,或者我可以将它存储在数据的第一个字节中,但练习要求使用最后一个元素的地址来终止循环。书中提到使用符号来标记结束。我认为我的问题是我只是不明白如何获取地址。如果我知道如何获得它,我可以将它存储在寄存器中。任何帮助表示赞赏。