Использование конечного адреса для остановки цикла - PullRequest
0 голосов
/ 29 апреля 2020

Здравствуйте, я новичок в ассемблере. Я читаю книгу, чтобы улучшить свои знания (программирование с нуля).

Я понимаю, что в следующем примере возникает вопрос, который требует изменить программу и заставить ее остановиться, когда достигнет конечного адреса. Я не знаю, как напечатать текущий адрес в сборке или сравнить его с номером. И правильно ли использовать 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

1 Ответ

0 голосов
/ 06 мая 2020

Похоже, вы пытаетесь определить, достиг ли вы конца data_items, сравнивая его с числом. Что вы можете сделать, это использовать al oop, чтобы помочь с этим. Вот что я придумал:

.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 в качестве счетчика для l oop. Я попробовал это, и я получил 252 в качестве кода состояния выхода, и когда я добавил 253 в конце, я все еще получил 252. Так что, похоже, работает. Надеюсь, я вам помог:).

...