Jonah Ruffer
Jonah Ruffer

Reputation: 1901

Assembly Code to C code

I'm trying to translate this assembly code to C and I need help. It has something to do with a while loop but i don't know what happens in the while loop. I've looked at it for a while and i'm sure it contains "while(something =! null)" then do something but I don't know what happens when the code does "movl" into %eax.

This section is the x86 assembly code that was compiled:

whilecode:
        pushl   %ebp
        movl    %esp, %ebp
        jmp     .L20
.L22:
        movl    8(%ebp), %eax
        movl    16(%eax), %eax
        movl    %eax, 8(%ebp)
.L20:
        cmpl    $0, 8(%ebp)
        je      .L21
        movl    8(%ebp), %eax
        movl    4(%eax), %eax
        cmpl    12(%ebp), %eax
        jne     .L22
.L21:
        cmpl    $0, 8(%ebp)
        setne   %al
        movzbl  %al, %eax
        popl    %ebp
        ret

This is the definition of a node:

typedef enum {CHAR,SHORT,INT} Type;

typedef struct node {
  Type   thetype;
  int     data;
  void   *opaque;
  struct node *ptr1, *ptr2;
} Node;

This is function definition for the while loop:

/* a while loop */
int whilecode(Node *somenode, int data)
{
  // FIX ME
  return 0;
}

Upvotes: 1

Views: 1535

Answers (2)

Chris Dodd
Chris Dodd

Reputation: 126536

Commenting on what the assembly does:

whilecode:
    pushl   %ebp            // save caller's frame pointer
    movl    %esp, %ebp      // set up our frame pointer
                            // no local variables set up
    jmp     .L20            // jump to the entry point of the function body

.L22:                       // NOT the beginning of the function -- probably a loop body
    movl    8(%ebp), %eax   // %eax = first argument
    movl    16(%eax), %eax  // %eax = %eax->fifth field
    movl    %eax, 8(%ebp)   // first argument = %eax
.L20:
    cmpl    $0, 8(%ebp)     // compare first argument to 0
    je      .L21            // branch to exit if they're equal 
    movl    8(%ebp), %eax   // %eax = first argument
    movl    4(%eax), %eax   // %eax = %eax->second field
    cmpl    12(%ebp), %eax  // compare %eax to second argument
    jne     .L22            // loop if not equal
.L21:
    cmpl    $0, 8(%ebp)     // compare first argument to 0
    setne   %al             // set %al = 1 if they're not equal (0 otherwise)
    movzbl  %al, %eax       // zero extend %al to %eax
    popl    %ebp            // restore the callers stack frame
    ret

Now you have a struct definition and a prototype, so this ends up being:

int whilecode(Node *somenode, int data)
{
    while (somenode != 0 && somenode->data != data)
        somenode = somenode->ptr2;
    return somenode != 0;
}

searching a linked list for a node that contains a particular data value and returning true if it is found.

Upvotes: 5

Guy P
Guy P

Reputation: 1423

FIXED

whilecode:
        pushl   %ebp         `Push EBP to stack`
        movl    %esp, %ebp   `EBP = ESP`
        jmp     .L20         `goto L20`
.L22:
        movl    8(%ebp), %eax  `EAX = (EBP+8)`
        movl    16(%eax), %eax `EAX = (EAX+16)`
        movl    %eax, 8(%ebp)  `(EBP+8) = EAX`
.L20:
        cmpl    $0, 8(%ebp)
        je      .L21           `if (EBP+8) == 0 goto L21`
        movl    8(%ebp), %eax  `EAX = (EBP+8)`
        movl    4(%eax), %eax  `EAX = (EAX+4)`
        cmpl    12(%ebp), %eax 
        jne     .L22           `if (EBP+12) != EAX goto L22`
.L21:
        cmpl    $0, 8(%ebp)    
        setne   %al            `if 0 != (EBP+8) Sets the byte in the AL to 1`
        movzbl  %al, %eax      `EAX = AL (zero ext)`
        popl    %ebp           `POP from stack to EBP (recover it)`
        ret                    `return`

EBP, ESP, EAX are 32 bit registers, AL is 8 bit register.

(EBP+8) is the value in the address of EBP plus 8 BYTES.

Just follow it and you'll understand the code, sorry I don't have time, good luck!

Upvotes: 0

Related Questions