我有以下代码
.section .data
myvar: .long 4,3,2,1
.section .text
.globl _start
_start:
movl $0, %edi
movl $myvar+0, %eax
movl $myvar+16, %ebx
int $0x80
我想继续增加eax,并希望将eax与ebx进行比较,以检查我是否已到达数组的末尾。
如何用4个字节递增寄存器。
答案 0 :(得分:2)
你想要的东西很简单,但你需要在代码中添加一些额外的信息,因为你正在编写x86程序集而不是Java或C#等高级语言。
首先解决方案是继续计算ecx
并将其与数组长度进行比较。
...
arrlen: .long (arrlen - myvar)/4
...
xor %ecx, %ecx
loop:
movl $0, %edi
movl $myvar(, %ecx, 4), %eax
pushl %ecx
...
//fill other registers
...
int $0x80
popl %ecx
incl %ecx
cmpl %ecx, arrlen
jne loop
另一种方法是检查加载的值。在调用系统调用之后,您无法确定它是否会影响寄存器值(某些系统调用在与用于参数的寄存器中返回信息)。你可以简单地将它保存在堆栈中,或者更快,在内存中直接进行比较。
pushl $0x0
loop:
movl $0, %edi
movl (%esp), %ecx
movl $myvar(, %ecx, 4), %eax
...
//fill other registers
...
int $0x80
incl (%esp)
cmpl $myvar(, %ecx, 4), 1
jne loop
为了节省几个字节的内存(使用它的每条指令可能只有4个字节),您可以将$myvar
的值移动到某个寄存器(不应该用于系统调用)。哪一个只取决于你。
答案 1 :(得分:1)
我使用addl而不是使用stack,
.section .data
mydata:
.long 1,2,3,4
.section .text
.globl _start
_start:
movl $0, %edi # initialize edi to 0
movl $mydata+0 , %edx # we are storing address of first element in edx
movl $mydata+16, %ecx # we are storing the address of last element to ecx
movl mydata(,%edi,4), %eax
movl %eax, %ebx
start_loop:
cmpl %edx, %ecx # we are checking if edx has moved to last element
je loop_exit # if starting and ending address are same we exit the loop
addl $4, %edx # We want to move by 4 bytes so we increment edx by 4
incl %edi # We increment %edi by 1
movl mydata(,%edi,4), %eax # Access the next data item
cmpl %ebx, %eax # compare the current item with existing largest value
jle start_loop # if the current item is less, we repeat
movl %eax, %ebx # if current item is highest, we interchange and update ebx
jmp start_loop # we repeat steps till we reach end of the loop
loop_exit:
movl $1, %eax # call exit system call
int $0x80 # Call Bruce Wayne
以上工作。