1

我有一个下面的代码

.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 个字节。

4

2 回答 2

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到某个寄存器(不应该用于系统调用)。会是哪一个完全取决于你。

于 2013-09-07T11:39:49.403 回答
1

我使用 addl 而不是使用堆栈,

.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

以上工作。

于 2013-09-10T05:39:22.823 回答