Click here to Skip to main content
14,429,635 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have write a c code and I want to convert the code to assembly , and also how I can write a external file and execute the code to the application exe, and how I can send the code to machine code.

// bin.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
# include <stdio.h>
int main()
{
    char a = 'a';
    int num1 = 0 / 8;
    int num2 = 1 / 8;
    int sum;

    switch(a) {
        case '+':
            if(num1 + num2)
            {
            sum = num1 + num2;
            }
            else
            {
             sum = num2 + num1;
            }
            break;
        case '-':
            if(num1 - num2)
            {
            sum = num1 - num2;
            }
            else
            {
             sum = num2 - num1;
            }
            break;
        case '*':
            if(num1 * num2)
            {
            sum = num1 * num2;
            }
            else
            {
             sum = num2 * num1;
            }
            break;
        case '/':
            if(num1 / num2)
            {
            sum = num1 / num2;
            }
            else
            {
             sum = num2 / num1;
            }
            break;
        default:

            break;
            }

    return 0;
}
Posted
Comments
Jochen Arndt 10-Jul-14 14:02pm
   
To generate assembly use the /FA compiler option. This can be also set in the Visual Studio project settings at C/C++ - Output files.
elmutasim23 10-Jul-14 14:08pm
   
but I want to compile the code using fasm and all the C/C++ output give x86 assembly
Albert Holguin 10-Jul-14 16:58pm
   
This smells strangely like homework...
Rate this:
Please Sign up or sign in to vote.

Solution 1

Um. It's not that simple: a C instruction does not necessarily translate to a single machine (or assembly) instruction - and case in particular is a more complex operation than you might think.

Probably the best place to start is to use Google to find a disassembler for your chosen processor (you don't specify it, and assembly is processor or processor family specific unlike C) and look at what that compiles to.

However, if you don't "know assembler" then it probably won't help much.
At a minimum, you may find that your C compiler supports something like
#pragma ASM
which allows you to include assembly code in your C source file - obviating the need for a separate assembler for small tasks.
   
Comments
elmutasim23 10-Jul-14 14:14pm
   
I tried to use CFF Explorer but when I try to compile the code the compiler give me a error
Rate this:
Please Sign up or sign in to vote.

Solution 2

Well, Visual Studio offer a functionality to convert C/C++ code to ASM.

To do that you only follow under instruction!

1. Make a break point at line
switch(a){

2. Debug program.
3. When cursor is at debug point, press Ctrl+F11.
You can get assembly code from your Editor.
4. You have to write
__asm{
}

5. Copy & Paste assembly code into __asm{};
6. Delete
switch(a){
...
default:
break;
}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100