2 Replies - 674 Views - Last Post: 09 March 2012 - 04:41 PM Rate Topic: -----

#1 kynn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-March 12

Direction for looking at Assembly Code

Posted 08 March 2012 - 11:10 PM

I have to translate this assembly code into C code:

int fun(int *ap, int *bp)
int a;
return _________;

pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%edx
movl 12(%ebp),%eax
movl %ebp,%esp
movl (%edx),%edx
addl %edx,(%eax)
movl %edx,%eax
popl %ebp

I'm not quite sure which direction I should go about reading this. I know where the body of the code is, but I have trouble with what movl %ebp, %esp means.

Is This A Good Question/Topic? 0
  • +

Replies To: Direction for looking at Assembly Code

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 783
  • View blog
  • Posts: 2,767
  • Joined: 31-December 10

Re: Direction for looking at Assembly Code

Posted 09 March 2012 - 08:09 AM

I found this book to be pretty good explaining assembly and how to use both asm and C. PC Assembly Language. It's FREE btw.

Since you need to translate it into C, you might want to read chapter 4. This part explains a little of that code fragment:


The C calling convention mandates that a subprogram first save the value of EBP on the stack and then set EBP to be equal to ESP. At the end of a subprogram, the original value of EBP must be restored. The following shows the general form of a subprogram that shows these conventions:
        push    ebp        ; save EBP value on the stack
        mov     ebp, esp   ; EBP = ESP
        ; subprogram code
        pop     ebp        ; restore original EBP value

This post has been edited by vividexstance: 09 March 2012 - 02:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon

Reputation: 910
  • View blog
  • Posts: 2,357
  • Joined: 28-March 11

Re: Direction for looking at Assembly Code

Posted 09 March 2012 - 04:41 PM

This is what the proc in Assembly should look like including the "int a". Sorry, but I really hate that AT&T syntax crap. This is INTEL syntax:
    push    ebp                 ; save base pointer to stack
    mov     ebp, esp            ; move stack pointer to ebp
    sub     esp, 4              ; make room for local a
    mov     edx, [ebp + 8]      ; first param on stack - ap
    mov     eax, [ebp + 12]     ; second param on stack - bp
    ; this is what the stack looks like now, stack is dword aligned 4 bytes.
    ; bp    [ebp + 12]
    ; ap    [ebp + 8]
    ; ret address [ebp + 4]
    ; ebp
    ; local a [ebp - 4]
    mov     esp, ebp            ; remove locals from stack
    mov     edx, [edx]          ; dereference pointer
    add     edx, [eax]          ; add contents of eax to edx
    mov     eax, edx            ; move edx into eax for ret value
    pop     ebp                 ; restore ebp
    ret     4 * 2               ; clean up stack 2 dwords pushed as params

Why would you want to convert Assembly to C? Usually the other way around! Well, with my comments added to the code, you should be able to see what it does and convert to C.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1