Memory 这两行汇编代码是做什么的? 我在炸弹实验室的第2阶段,我似乎无法弄清楚这两条汇编线是如何影响代码的整体,以及它们是如何在循环中起作用的。 以下是两行代码: add -0x24(%ebp,%ebx,4),%eax cmp %eax,-0x20(%ebp,%ebx,4)

Memory 这两行汇编代码是做什么的? 我在炸弹实验室的第2阶段,我似乎无法弄清楚这两条汇编线是如何影响代码的整体,以及它们是如何在循环中起作用的。 以下是两行代码: add -0x24(%ebp,%ebx,4),%eax cmp %eax,-0x20(%ebp,%ebx,4),memory,assembly,binary,hex,Memory,Assembly,Binary,Hex,下面是完整的代码: 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 &l

下面是完整的代码:

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
功能阶段2的汇编程序代码转储: 0x08048ba4:推送%ebp 0x08048ba5:mov%esp,%ebp 0x08048ba7:推送%ebx 0x08048ba8:子$0x34,%esp 0x08048bab:lea-0x20(%ebp),%eax 0x08048bae:mov%eax,0x4(%esp) 0x08048bb2:mov 0x8(%ebp),%eax 0x08048bb5:mov%eax,(%esp) 0x08048bb8:调用0x804922f 0x08048bbd:cmpl$0x0,-0x20(%ebp) 0x08048bc1:jns 0x8048be3 0x08048bc3:调用0x80491ed 0x08048bc8:jmp 0x8048be3 0x08048bca:mov%ebx,%eax 0x08048bcc:添加-0x24(%ebp,%ebx,4),%eax 0x08048bd0:cmp%eax,-0x20(%ebp,%ebx,4) 0x08048bd4:je 0x8048bdb 0x08048bd6:调用0x80491ed 0x08048bdb:inc%ebx 0x08048bdc:cmp$0x6,%ebx 0x08048bdf:jne 0x8048bca 0x08048be1:jmp 0x8048bea 0x08048be3:mov$0x1,%ebx 0x08048be8:jmp 0x8048bca 0x08048bea:添加$0x34,%esp 0x08048床:弹出%ebx 0x08048bee:弹出%ebp 0x08048bef:ret
我注意到inc命令将%ebx增加1,并将其用作循环中的%eax。但是每次加法和cmp都会让我绊倒。如果我将%eax作为1加入到add和cmp中,那么%eax会出现什么?谢谢我还知道,一旦%ebx达到5,循环就结束了,它结束了整个代码。

这两条指令处理两个位置的内存,由
ebp
ebx
索引。特别是,add指令将保存到目前为止检查的所有数字的运行总数,而comparison指令将检查这是否等于下一个数字。比如:

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

这两条指令处理两个位置的内存,分别由
ebp
ebx
索引。特别是,add指令将保存到目前为止检查的所有数字的运行总数,而comparison指令将检查这是否等于下一个数字。比如:

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

你有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)

你有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)