0

So I'm working on my Binary bomb lab and i've gotten to the point where I used gdb to pull out this bit of assembly code. However I'm having trouble turning it into workable C code.

0x0000000000400dd0 <+0>:     push   %rbp
0x0000000000400dd1 <+1>:     push   %rbx
0x0000000000400dd2 <+2>:     sub    $0x28,%rsp
0x0000000000400dd6 <+6>:     mov    %rsp,%rsi
0x0000000000400dd9 <+9>:     callq  0x4013a3 <read_six_numbers>
0x0000000000400dde <+14>:    cmpl   $0x1,(%rsp)
0x0000000000400de2 <+18>:    jne    0x400ded <phase_2+29>
0x0000000000400de4 <+20>:    mov    %rsp,%rbx
0x0000000000400de7 <+23>:    lea    0x14(%rbx),%rbp
0x0000000000400deb <+27>:    jmp    0x400e02 <phase_2+50>
0x0000000000400ded <+29>:    callq  0x401381 <explode_bomb>
0x0000000000400df2 <+34>:    jmp    0x400de4 <phase_2+20>
0x0000000000400df4 <+36>:    callq  0x401381 <explode_bomb>
0x0000000000400df9 <+41>:    add    $0x4,%rbx
0x0000000000400dfd <+45>:    cmp    %rbp,%rbx
0x0000000000400e00 <+48>:    je     0x400e0d <phase_2+61>
0x0000000000400e02 <+50>:    mov    (%rbx),%eax
0x0000000000400e04 <+52>:    add    %eax,%eax
0x0000000000400e06 <+54>:    cmp    %eax,0x4(%rbx)
0x0000000000400e09 <+57>:    je     0x400df9 <phase_2+41>
0x0000000000400e0b <+59>:    jmp    0x400df4 <phase_2+36>
0x0000000000400e0d <+61>:    add    $0x28,%rsp
0x0000000000400e11 <+65>:    pop    %rbx
0x0000000000400e12 <+66>:    pop    %rbp
0x0000000000400e13 <+67>:    retq

Right now this is what I have :

push rbp
push rbx
rsp = rsp - 40
rsi = rsp
(read_six_numbers)

if rsp != 1 goto <phase_2+29>(AKA EXPLODE)
else{
rbx = rsp                                             
rbp = rbx + 20                                        
goto <phase_2+50>                                     
}

eax = rbx (THIS IS <phase_2+50>)                      
eax = eax + eax                                       

if eax = rbx + 4 goto <phase_2+41>(AKA REPEAT)        
else{
goto <phase_2+36> (AKA EXPLODE)
}

rbx = rbx + 4 (THIS IS <phase_2+41>)
if rbp == rbx goto <phase_2+61> (AKA END)
else{ 
goto <phase_2+50>

All I can figure out right now is that when it loops we +4 to the value, but I'm pretty sure I'm missing something very important. If you could help me out that would be amazing. Thank you.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • It adds 4 because that's the size of an `int`. It's just moving to the next number. As such it's just checking that `input[i+1]==2*input[i]`. – Jester Oct 12 '18 at 01:17
  • Where did the multiplication come from? Sorry I'm pretty new at this. – Timothy Oct 12 '18 at 01:23
  • 1
    at your target platform the `int` is of 32 bit size. And your memory is addressable by 8 bits (one byte). So to move 32 bits further in memory, you have to modify address by +4 (four bytes). It's "pointer math" in C which is hiding the type-size from C programmer. (if you are asking about `2*`, it's the `add eax,eax` doubling the `[i]` value after reading it from `input[]`) – Ped7g Oct 12 '18 at 06:04
  • Just a tip on the side: `cmpl $0x1,(%rsp)` would be translated here into `if *rsp != 1 goto`. The brackets mean: the value at the address that is pointed to by `RSP`. – rkhb Oct 12 '18 at 15:48

0 Answers0