Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
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.

C#
// 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
Updated 23-Apr-23 21:00pm
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...

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
C++
#pragma ASM
which allows you to include assembly code in your C source file - obviating the need for a separate assembler for small tasks.
 
Share this answer
 
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
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;
}
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900