Thread: convert c into asm

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    12

    convert c into asm

    Hi, how to convert this program into assembly

    Code:
    #include <stdio.h>
    
    #define CALORIES_PER_GRAM 9.0   /* there are 9 calories per gram of fat */
    
    int main (void)
    {
            int     grams_of_fat,   /* number of grams of fat in one serving */
                    total_calories; /* number of calories in one serving */
            float   fat_fraction,   /* fraction of calories due to fat */
                    percent;        /* percentage of total calories from fat */
    
            printf("This program will tell you how much of the calories in\n");
            printf("a food are from the food's fat content.\n\n");
    
            printf("How many grams of fat are in one serving?  ");
            scanf("%d",&grams_of_fat);
            printf("How many total calories are in one serving?  ");
            scanf("%d",&total_calories);
    
            fat_fraction = (grams_of_fat * CALORIES_PER_GRAM) / total_calories;
            percent = fat_fraction * 100;
    
            if (grams_of_fat == 1) {
                    printf("\nA food with 1 gram of fat ");
            } else {
                    printf("\nA food with %d grams of fat ",grams_of_fat);
            }
    
            if (total_calories == 1) {
                    printf("and 1 calorie per serving\n");
            } else {
                    printf("and %d calories per serving\n",total_calories);
            }
    
            printf("has %.2f%% of those calories from fat.\n\n",percent);
    
            return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    gcc -s prog.c
    Generates the asm.
    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.

  3. #3
    Registered User
    Join Date
    May 2018
    Posts
    12
    it says: gcc is not regocnized as an internal or external command

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,341
    Which compiler are you using?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2018
    Posts
    12
    dev c++ 5.11, mingw, gcc.
    Last edited by asylumcyclop; 05-20-2018 at 08:01 AM.

  6. #6
    Registered User
    Join Date
    May 2018
    Posts
    12
    saved the code in notepad ++

    .LC0:
    .string "programm ytleb, kui palju on kaloreid on"
    .LC1:
    .string "toidus, arvestades toidu rasvasisaldust.\n"
    .LC2:
    .string "Kui palju rasvasid tarbisid 1 toidukorraga? "
    .LC3:
    .string "%d"
    .LC4:
    .string "Kui palju kaloreid tarbisid 1 toidukorraga? "
    .LC7:
    .string "\nA toit sisaldusega 1g rasva "
    .LC8:
    .string "\n rasva hulk %d grammi "
    .LC9:
    .string "1 kalor per toidukord"
    .LC10:
    .string "ja %d kaloreid per toidukord\n"
    .LC11:
    .string "tuleb %.2f%% kaloreid puhtalt rasvadest.\n\n"
    main:
    push rbp
    mov rbp, rsp
    sub rsp, 16
    mov edi, OFFSET FLAT:.LC0
    call puts
    mov edi, OFFSET FLAT:.LC1
    call puts
    mov edi, OFFSET FLAT:.LC2
    mov eax, 0
    call printf
    lea rax, [rbp-12]
    mov rsi, rax
    mov edi, OFFSET FLAT:.LC3
    mov eax, 0
    call __isoc99_scanf
    mov edi, OFFSET FLAT:.LC4
    mov eax, 0
    call printf
    lea rax, [rbp-16]
    mov rsi, rax
    mov edi, OFFSET FLAT:.LC3
    mov eax, 0
    call __isoc99_scanf
    mov eax, DWORD PTR [rbp-12]
    cvtsi2sd xmm1, eax
    movsd xmm0, QWORD PTR .LC5[rip]
    mulsd xmm0, xmm1
    mov eax, DWORD PTR [rbp-16]
    cvtsi2sd xmm1, eax
    divsd xmm0, xmm1
    cvtsd2ss xmm2, xmm0
    movss DWORD PTR [rbp-4], xmm2
    movss xmm1, DWORD PTR [rbp-4]
    movss xmm0, DWORD PTR .LC6[rip]
    mulss xmm0, xmm1
    movss DWORD PTR [rbp-8], xmm0
    mov eax, DWORD PTR [rbp-12]
    cmp eax, 1
    jne .L2
    mov edi, OFFSET FLAT:.LC7
    mov eax, 0
    call printf
    jmp .L3
    .L2:
    mov eax, DWORD PTR [rbp-12]
    mov esi, eax
    mov edi, OFFSET FLAT:.LC8
    mov eax, 0
    call printf
    .L3:
    mov eax, DWORD PTR [rbp-16]
    cmp eax, 1
    jne .L4
    mov edi, OFFSET FLAT:.LC9
    call puts
    jmp .L5
    .L4:
    mov eax, DWORD PTR [rbp-16]
    mov esi, eax
    mov edi, OFFSET FLAT:.LC10
    mov eax, 0
    call printf
    .L5:
    cvtss2sd xmm0, DWORD PTR [rbp-8]
    mov edi, OFFSET FLAT:.LC11
    mov eax, 1
    call printf
    mov eax, 0
    leave
    ret
    .LC5:
    .long 0
    .long 1075970048
    .LC6:
    .long 1120403456

    now how do I compile the assembly to nasm?


  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    Why are you doing this?
    The compiler calls the assembler for you.
    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.

  8. #8
    Registered User
    Join Date
    May 2018
    Posts
    12
    I just took an example for testing, the code is copied from compile exploser. This bloody thing doesn't work in my pc. I copied the c program into c:\users\lenovo and tried to call out gcc -s program.c in cmd.
    Last edited by asylumcyclop; 05-21-2018 at 04:17 AM.

  9. #9
    Registered User
    Join Date
    May 2018
    Posts
    12
    Can someone help me with teamviewer.
    Last edited by asylumcyclop; 05-21-2018 at 04:13 AM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    You mean this?
    Compiler Explorer

    If you've already got a compiler installed, why are you wasting your time with that online thing?
    It's a toy for education and amusement.

    If you want to write actual software, use the tools you have.
    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.

  11. #11
    Registered User
    Join Date
    May 2018
    Posts
    12
    the compiler doesnt work as I already said.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    What? Your devc++ on your windows machine?

    Fix that, rather than trying this pointless path you're on.
    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.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    Download binary

    Install codeblocks-17.12mingw-setup.exe
    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.

  14. #14
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by asylumcyclop View Post
    it says: gcc is not regocnized as an internal or external command
    That just means that gcc is not in the PATH.

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,063
    Quote Originally Posted by christophergray View Post
    That just means that gcc is not in the PATH.
    Or it has a different name like mingw-gcc

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to convert .ppm into .jpg using C ???
    By ios198 in forum C Programming
    Replies: 3
    Last Post: 11-18-2013, 02:58 PM
  2. How to convert to UT8 ?
    By Tany3450 in forum C Programming
    Replies: 5
    Last Post: 11-15-2013, 11:17 AM
  3. Replies: 17
    Last Post: 07-26-2012, 09:32 PM
  4. cannot convert `int (*)[2]' to `int*
    By luoyangke in forum C Programming
    Replies: 9
    Last Post: 11-12-2009, 11:27 PM
  5. cannot convert
    By opafire in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2003, 08:41 AM

Tags for this Thread