使用结束地址停止循环

问题描述 投票:0回答:1

你好,我是汇编语言的新手。我读了一本书以提高自己的知识(从头开始编程)。

我理解下面的示例,有一个问题,要求修改程序并使其在到达结束地址时停止。我不知道如何在汇编中打印当前地址或将其与数字进行比较。使用cmpl $13, %edi检测何时到达data_items的末尾是否正确?

.section .data
data_items:             #These are the data items
.long 3,67,34,222,45,75,54,34,44,33,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                 # since this is the first item, %eax is
                                # the biggest
start_loop:                     # start loop
#cmpl $22, %eax                 # check to see if we’ve hit the end using Value
#cmpl $13, %edi                 # Using Length to break loop

#I have to add a condition here  to use an ending address 
#rather than the number 0 to know when to stop.

je loop_exit
incl %edi                       # load next value
movl data_items(,%edi,4), %eax
cmpl %ebx, %eax                 # compare values
jle start_loop                  # jump to loop beginning if the new
                                # one isn’t bigger
movl %eax, %ebx                 # move the value as the largest
jmp start_loop                  # jump to loop beginning
loop_exit:
    # %ebx is the status code for the exit system call
    # and it already has the maximum number
            movl $1, %eax   #1 is the exit() syscall
            int $0x80
assembly x86 att
1个回答
0
投票

似乎您正在尝试通过将其与数字进行比较来确定是否已达到data_items的末尾。您可以做的是使用循环来帮助解决此问题。这是我想出的:

.section .data
data_items:
        .long 4,73,121,133,236,251,252
data_items_size:
        .byte 7

.section .text
.globl _start
_start:
        movl $0, %edi
        movl data_items(,%edi,4), %eax
        movl %eax, %ebx
        movl $1, %ecx

        loop:
                cmpl data_items_size, %ecx
                je   exit
                incl %ecx

                incl %edi
                movl data_items(,%edi,4), %eax
                cmpl %eax, %ebx
                jge  loop
                movl %eax, %ebx
                jmp  loop

        loop_exit:
                movl $1, %eax
                int  $0x80

data_items_size标签包含data_items的大小。在此示例中,我将%ecx寄存器用作循环的计数器。我尝试了一下,得到了252作为退出状态代码,当我在末尾添加253时,我仍然得到252。所以它似乎正在工作。希望我能帮到你:)。

© www.soinside.com 2019 - 2024. All rights reserved.