Thread: Currently having trouble (assembly and c)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    33

    Currently having trouble (assembly and c)

    I have this assembly code that I'm trying to convert to C. I'm currently having trouble understanding it though. I've tried to compile it with Nasm, but I get numerous amounts of error. It could be the way I'm compiling it though..because this code is supposed to work. Could anyone help me out understand what this means?

    Code:
        .file    "mystery.c"
        .text
    .globl add
        .type    add, @function
    add:
        pushl    %ebp
        movl    %esp, %ebp
        movl    12(%ebp), %eax
        addl    8(%ebp), %eax
        popl    %ebp
        ret
        .size    add, .-add
    .globl compute_fib
        .type    compute_fib, @function
    compute_fib:
        pushl    %ebp
        movl    %esp, %ebp
        pushl    %ebx
        subl    $28, %esp
        movl    8(%ebp), %eax
        movl    num(,%eax,4), %eax
        cmpl    $-1, %eax
        je    .L4
        movl    8(%ebp), %eax
        movl    num(,%eax,4), %eax
        movl    %eax, -24(%ebp)
        jmp    .L6
    .L4:
        movl    $-1, -8(%ebp)
        cmpl    $0, 8(%ebp)
        jne    .L7
        movl    $0, -8(%ebp)
        jmp    .L9
    .L7:
        cmpl    $1, 8(%ebp)
        jne    .L10
        movl    $1, -8(%ebp)
        jmp    .L9
    .L10:
        movl    8(%ebp), %eax
        subl    $2, %eax
        movl    %eax, (%esp)
        call    compute_fib
        movl    %eax, %ebx
        movl    8(%ebp), %eax
        subl    $1, %eax
        movl    %eax, (%esp)
        call    compute_fib
        movl    %ebx, 4(%esp)
        movl    %eax, (%esp)
        call    add
        movl    %eax, -8(%ebp)
    .L9:
        movl    8(%ebp), %eax
        movl    num(,%eax,4), %eax
        cmpl    $-1, %eax
        jne    .L12
        movl    8(%ebp), %edx
        movl    -8(%ebp), %eax
        movl    %eax, num(,%edx,4)
    .L12:
        movl    8(%ebp), %eax
        movl    num(,%eax,4), %eax
        movl    %eax, -24(%ebp)
    .L6:
        movl    -24(%ebp), %eax
        addl    $28, %esp
        popl    %ebx
        popl    %ebp
        ret
        .size    compute_fib, .-compute_fib
        .section    .rodata
    .LC0:
        .string    "Value:   %d\n"
        .text
    .globl main
        .type    main, @function
    main:
        leal    4(%esp), %ecx
        andl    $-16, %esp
        pushl    -4(%ecx)
        pushl    %ebp
        movl    %esp, %ebp
        pushl    %ecx
        subl    $36, %esp
        movl    4(%ecx), %eax
        addl    $4, %eax
        movl    (%eax), %eax
        movl    %eax, (%esp)
        call    atoi
        movl    %eax, -12(%ebp)
        movl    $0, -8(%ebp)
        jmp    .L16
    .L17:
        movl    -8(%ebp), %eax
        movl    $-1, num(,%eax,4)
        addl    $1, -8(%ebp)
    .L16:
        cmpl    $199, -8(%ebp)
        jle    .L17
        movl    -12(%ebp), %eax
        movl    %eax, (%esp)
        call    compute_fib
        movl    %eax, 4(%esp)
        movl    $.LC0, (%esp)
        call    printf
        movl    $0, %eax
        addl    $36, %esp
        popl    %ecx
        popl    %ebp
        leal    -4(%ecx), %esp
        ret
        .size    main, .-main
        .comm    num,800,32
        .ident    "GCC: (GNU) 4.1.2 20080704 (Red Hat 4.1.2-51)"
        .section    .note.GNU-stack,"",@progbits

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    No wonder why you can't understand it and it's giving you errors. This assembly is the output of GCC, which is then fed into GASM to assemble. It's not meant for NASM.

    EDIT: Pass this assembly to GCC as *.s in order to assemble and link it.
    Last edited by GReaper; 04-05-2013 at 08:49 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    Okay, so I trying to do this, but the command I'm putting in may be wrong.

    command: "gcc -c mystery.s"

    I keep getting these errors..
    error: suffix or operands invalid for 'push'
    error: suffix or operands invalid for 'pop'

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    But are you trying to compile this 32-bit code on a 64 bit machine?

    I get the same errors when compiling in 64-bit mode.

    However,
    Code:
    $ gcc -Wa,--32  bar.s
    /usr/bin/ld: i386 architecture of input file `/tmp/ccyHtwVV.o' is incompatible with i386:x86-64 output
    collect2: ld returned 1 exit status
    shows it will compile OK as 32-bit code, but without the necessary libraries, it still won't create an executable file.

    Besides, how much "mystery" can there be in a file containing a function called compute_fib which calls itself recursively.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    haha you're right. I was too busy looking at the first few lines of main that I didn't realize that this program actually computes fibonacci values. Thanks so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert assembly>machine code, machine code>assembly
    By wenxinleong in forum C Programming
    Replies: 12
    Last Post: 06-23-2011, 10:42 PM
  2. Assembly trouble (not too complicated)
    By ... in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 03-06-2004, 04:51 PM
  3. x86 assembly
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 02-18-2003, 02:29 AM
  4. box in assembly to c
    By jwhitaker3 in forum C Programming
    Replies: 4
    Last Post: 01-27-2003, 12:29 PM
  5. Assembly example
    By Lynux-Penguin in forum C Programming
    Replies: 6
    Last Post: 04-24-2002, 07:45 PM