5 Replies - 261 Views - Last Post: 21 March 2012 - 10:46 AM Rate Topic: -----

#1 ramlapam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 27-June 11

Grouping parts of assembly code with c source code

Posted 20 March 2012 - 09:57 AM

Hi there,

I'm having a little trouble grouping parts of assembly code generated in gdb with its corrosponding c code. I have a function called swap_n_add:

int swap n add ( int xp , int yp )
{
int x = xp ;
int y = yp ;

xp = y ;
yp = x ;
return x + y ;
}


I used the command disas swap_n_add to find a dumb of the assembly code, but I'm not really sure how to group parts of the assembly code with the given c code side by side.

Any help or pointers in the right direction would be much appreciated!

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Grouping parts of assembly code with c source code

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,240
  • Joined: 31-December 10

Re: Grouping parts of assembly code with c source code

Posted 20 March 2012 - 10:03 AM

gdb doesn't generate assembly, the compiler does. Could you post the assembly code you are trying to compare to the above code?
Was This Post Helpful? 0
  • +
  • -

#3 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: Grouping parts of assembly code with c source code

Posted 20 March 2012 - 02:35 PM

One way you could find that code, is use inline assembly and put a bunch of NOPs at the beginning and end of the procedure, turn off whatever optimizations in the compiler that would remove the NOPs and when you view the Assembly output, just look for the NOP's.
Was This Post Helpful? 0
  • +
  • -

#4 ramlapam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 27-June 11

Re: Grouping parts of assembly code with c source code

Posted 20 March 2012 - 04:12 PM

View Postvividexstance, on 20 March 2012 - 10:03 AM, said:

gdb doesn't generate assembly, the compiler does. Could you post the assembly code you are trying to compare to the above code?


Thank you for the replies!

Here is the assembly code I am trying to compare, sorry for not posting it before.
Dump of assembler code for function swap_n_add:
0x0000000000400448 <swap_n_add+0>:	push   rbp
0x0000000000400449 <swap_n_add+1>:	mov    rbp,rsp
0x000000000040044c <swap_n_add+4>:	mov    QWORD PTR [rbp-0x18],rdi
0x0000000000400450 <swap_n_add+8>:	mov    QWORD PTR [rbp-0x20],rsi
0x0000000000400454 <swap_n_add+12>:	mov    rax,QWORD PTR [rbp-0x18]
0x0000000000400458 <swap_n_add+16>:	mov    eax,DWORD PTR [rax]
0x000000000040045a <swap_n_add+18>:	mov    DWORD PTR [rbp-0x8],eax
0x000000000040045d <swap_n_add+21>:	mov    rax,QWORD PTR [rbp-0x20]
0x0000000000400461 <swap_n_add+25>:	mov    eax,DWORD PTR [rax]
0x0000000000400463 <swap_n_add+27>:	mov    DWORD PTR [rbp-0x4],eax
0x0000000000400466 <swap_n_add+30>:	mov    rdx,QWORD PTR [rbp-0x18]
0x000000000040046a <swap_n_add+34>:	mov    eax,DWORD PTR [rbp-0x4]
0x000000000040046d <swap_n_add+37>:	mov    DWORD PTR [rdx],eax
0x000000000040046f <swap_n_add+39>:	mov    rdx,QWORD PTR [rbp-0x20]
0x0000000000400473 <swap_n_add+43>:	mov    eax,DWORD PTR [rbp-0x8]
0x0000000000400476 <swap_n_add+46>:	mov    DWORD PTR [rdx],eax
0x0000000000400478 <swap_n_add+48>:	mov    eax,DWORD PTR [rbp-0x4]
0x000000000040047b <swap_n_add+51>:	add    eax,DWORD PTR [rbp-0x8]
0x000000000040047e <swap_n_add+54>:	leave  
0x000000000040047f <swap_n_add+55>:	ret    
End of assembler dump.

Was This Post Helpful? 0
  • +
  • -

#5 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,240
  • Joined: 31-December 10

Re: Grouping parts of assembly code with c source code

Posted 21 March 2012 - 09:18 AM

So what exactly is your problem? Is it that you want to know which lines of the assembly output match the lines in the C code?
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1635
  • View blog
  • Posts: 3,111
  • Joined: 30-May 10

Re: Grouping parts of assembly code with c source code

Posted 21 March 2012 - 10:46 AM

1. Start by disassembling an empty function, so you understand the standard prolog/epilog.

2. Add ONE line of code at a time, so you can easily track incremental changes.

3. Do NOT use any optimisation options. Unoptimised assembler might be looking like fruit salad to you, but if you optimise it, it will just be a fruit smoothie.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1