这两行汇编代码做了什么?

时间:2016-02-21 19:52:23

标签: memory assembly binary hex

我正处于第2阶段的炸弹实验室中间,我似乎无法弄清楚这两行程序如何影响整体代码以及它们如何在循环中发挥作用。 这是两行代码:

add -0x24(%ebp,%ebx,4),%eax
cmp %eax,-0x20(%ebp,%ebx,4)

这是整个代码:

Dump of assembler code for function phase_2:
   0x08048ba4 <+0>:     push   %ebp
   0x08048ba5 <+1>:     mov    %esp,%ebp
   0x08048ba7 <+3>:     push   %ebx
   0x08048ba8 <+4>:     sub    $0x34,%esp
   0x08048bab <+7>:     lea    -0x20(%ebp),%eax
   0x08048bae <+10>:    mov    %eax,0x4(%esp)
   0x08048bb2 <+14>:    mov    0x8(%ebp),%eax
   0x08048bb5 <+17>:    mov    %eax,(%esp)
   0x08048bb8 <+20>:    call   0x804922f <read_six_numbers>
   0x08048bbd <+25>:    cmpl   $0x0,-0x20(%ebp)
   0x08048bc1 <+29>:    jns    0x8048be3 <phase_2+63>
   0x08048bc3 <+31>:    call   0x80491ed <explode_bomb>
   0x08048bc8 <+36>:    jmp    0x8048be3 <phase_2+63>
   0x08048bca <+38>:    mov    %ebx,%eax
   0x08048bcc <+40>:    add    -0x24(%ebp,%ebx,4),%eax
   0x08048bd0 <+44>:    cmp    %eax,-0x20(%ebp,%ebx,4)
   0x08048bd4 <+48>:    je     0x8048bdb <phase_2+55>
   0x08048bd6 <+50>:    call   0x80491ed <explode_bomb>
   0x08048bdb <+55>:    inc    %ebx
   0x08048bdc <+56>:    cmp    $0x6,%ebx
   0x08048bdf <+59>:    jne    0x8048bca <phase_2+38>
   0x08048be1 <+61>:    jmp    0x8048bea <phase_2+70>
   0x08048be3 <+63>:    mov    $0x1,%ebx
   0x08048be8 <+68>:    jmp    0x8048bca <phase_2+38>
   0x08048bea <+70>:    add    $0x34,%esp
   0x08048bed <+73>:    pop    %ebx
   0x08048bee <+74>:    pop    %ebp
   0x08048bef <+75>:    ret

我注意到inc命令将%ebx递增1并在循环中将其用作%eax。但添加 cmp 每次都会让我感到震惊。如果我有%eax作为1进入添加 cmp %eax出来了?谢谢!我也知道,一旦%ebx达到5,那么循环结束并结束整个代码。

2 个答案:

答案 0 :(得分:1)

你有一个包含6个号码的列表。这意味着您最多可以比较5对数字。因此,使用%ebx的循环执行5次迭代。

在每次迭代中,较低地址的值被添加到当前循环计数,然后与下一个较高地址的值进行比较。只要它们匹配炸弹就不会爆炸!

这循环5次:

add -0x24(%ebp,%ebx,4),%eax
cmp %eax,-0x20(%ebp,%ebx,4)

使用这些数字:

with %ebx=1 numbers are at -0x20(%ebp) and -0x1C(%ebp)
with %ebx=2 numbers are at -0x1C(%ebp) and -0x18(%ebp)
with %ebx=3 numbers are at -0x18(%ebp) and -0x14(%ebp)
with %ebx=4 numbers are at -0x14(%ebp) and -0x10(%ebp)
with %ebx=5 numbers are at -0x10(%ebp) and -0x0C(%ebp)

答案 1 :(得分:-1)

这两条指令处理两个位置的内存,由ebpebx编制索引。特别是,add指令保持到目前为止检查的所有数字的运行总和,并且比较指令检查是否等于下一个数字。如下所示:

int total = 0;
for (i=0; ..., i++) {
    total += array[i];
    if (total != array[i+])
        explode_bomb();
}
相关问题