使用结束地址来停止循环 [英] Using ending address to stop a loop
问题描述
你好,我是汇编语言的新手.我读了一本书来提高我的知识(从头开始编程).
Hello I'm new in assembly language. I read a book to improve my knowledge (Programming from the ground up).
我理解下面的例子,有一个问题要求修改程序并使其在到达结束地址时停止.我不知道如何在汇编中打印当前地址或将其与数字进行比较.使用 cmpl $13, %edi
检测何时到达 data_items
结尾是否正确?
I understand the following example, there is a question which demand to modify the program and make it stop when reaches an ending address. I don't know how to print the current address in assembly or to compare it with a number. And is it correct to use cmpl $13, %edi
to detect when end of data_items
has been reached ?
.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
推荐答案
您似乎在尝试通过将 data_items
与一个数字进行比较来确定是否已到达末尾.你可以做的是使用循环来帮助解决这个问题.这是我想出的:
It seems like you're trying to determine whether you've reached the end of data_items
by comparing it to a number. What you can do is use a loop to help with that. Here's what I came up with:
.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,看来是可以的了.希望我能帮到你:)
The data_items_size
label contains the size of data_items
. In this example, I used the %ecx
register as a counter for the loop.
I tried it and I got 252 as the exit status code, and when I added 253 at the end, I still got 252. So it seems to be working. Hope I helped you out :).
这篇关于使用结束地址来停止循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!