你好,我是汇编语言新手。我读了一本书来提高我的知识(从头开始编程(。
我理解下面的例子,有一个问题需要修改程序,并在到达结束地址时使其停止。我不知道如何在程序集中打印当前地址,也不知道如何将其与数字进行比较。使用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
您似乎正试图通过将其与数字进行比较来确定是否已到达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。因此,它似乎正在发挥作用。希望我帮了你:(。