Deadstripping an assembly code PROC in MASM - masm

I have some code that I've written in Assembly and assembled with MASM. Is it possible to have functions in an .obj file that was assembled with MASM be deadstripped if they are not referenced by any calling code?

Related

What assembly language does C code compile into in Visual Studio?

When I debug a C project, I can see all the assembly codes it compiles into. I want to know what assembly language that it. Is it NASM or MASM or something else? And if I use inline assembly, will I be able to use some other assembly language?
The code it compiles to is not assembly, but straight machine code, at least after link-time optimizations. What you see while debugging is on-the-fly disassembly of the machine code that is currently executing. As such, it has no additional structure, such as labels, macros, etc. such that you would expect to find in high-level assemblers, because this extra information is lost (or, more accurately, never present), when producing machine code.
If you meant the syntax, Visual Studio shows the assembly directives in Intel syntax, which is different from AT&T syntax, which is a default with GCC and GNU assembler.
In fact, it may also be gibberish. If you jmp out of alignment (x86 instructions are variable-length), or to a region that does not contain executable code, but rather data, the disassembler will try to make sense of the data, producing random assembly directives that don't mean anything.
This is actually quite common; see for example this image:
add byte ptr [rax], al is the attempted disassembly of bytes 00 00, which obviously does not represent actual executable code.
MSVC can compile binaries for x86, x86_64, ARM and Itanium. So it depends on your target and project settings
For X86 or X86-X64, Visual Studio includes ML.EXE (MASM 6.00 and later versions were renamed to ML.EXE) for 32 bit code and ML64.EXE for 64 bit code. On a VS project, you can right click on a file name, then properties, ... output files, ... assembly listing ... . The command line option for assembly listing is /Fa. Although called assembly only listing, it produces assembly code.

How do I view the 8086 machine code of C and assembly programs?

I have a program in 8086 and C languages now i want to compare the machine code of each program. How can I get machine code of C and assembly 8086 code
I use emu8086 and DEV .
How can I get machine code of C and assembly 8086 code
Compile the C code using the standard compiler
Assemble the C code using the standard assembler
That gives you the machine code.
Then, assuming you are really wanting to compare the code ... :
use a hex dump tool to look at / compare the two code files, or
use a disassembler and compare the dissasembled versions of the C code with your original assembly code, or
look up in your compiler documentation how to get it to output assembly language ...

I am unable to compile a program which has assembly language instructions

I compiled a C program (which has some assembly language instructions) like this.
TCC -Emasm.exe protect.c
It gives an error Unable to execute masm.exe.
What should I do or where can I find masm.exe?
You need to get microsoft assembly compiler, which is called masm.

A C Project containing both C Code and Assembly Code

I have a sample project that contains C code and Assembly Code
There are Main.c, Main.h and convert.S.
Inside the assembly code convert.S there is the following code:
.global
.section .bss
.section .text
.global _FIL_2ORD
_FIL_2ORD:
inside the convert.h file:
extern int FIL_2ORD(
tFIL2HISTORY *history;
tFIL2COEFF *coeff;
int input;
);
Inside the Main.c function if it calls FIL_2ORD(); then would it be resolved through the function inside the assembly code as declared in convert.h file?
My question is whether the assembly code would get compiled and linked, and whenever the main.c calls the function would it be referenced and resolved?
Compile the C, assemble the ASM, and link the two together into an executable. The linker will find FIL_2ORD() inside the ASM's object file after it sees that the C's object file needs it.
The object files are created by the C compiler and the Assembler for each source file respectively.
My question is whether the assembly code would get compiled and
linked, and whenever the main.c calls the function would it be
referenced and resolved?
I'm assuming you're using the GCC compiler - Yes the .global directive in the assembly file makes the _FIL_2ORD symbol public to the linker, so it will become callable from outside the assembly source code.
This is an example of how you can compile, assemble and link using the command-line
gcc -o myexe Main.c convert.S
The extern declaration in convert.h is hinting the C compiler on what parameters the external function is expecting. The assembly source code should honor this declaration. You should look-up the standard C calling convention of your target platform to see the rules of how parameters are passed, and write your assembly code accordingly.
Depending on the target platform, the leading underscore char in the _FIL_2ORD declaration(inside convert.S) may or may not be necessary (this is part of the platform-specific C calling convention I referred to in the previous paragraph). If the program fails to link, try again, this time removing the leading underscore.

how code is executed and gcc

I'm very interesting in compilation and I've got a question about gcc.
I know that a tree is generated from the code to compile, then ASM code is
generated and I need some explanations about this point.
ASM code is added in a file and executed later or ASM code is directly loaded in memory with asm functions ? I'm working on a small compiler and I don't know how to execute the tree generated, and I didn't find any documentation about that.
GCC's front-end parses the source files in different languages (C, C++, Fortran, ObjectiveC, Java etc.). Then the code (AST) is translated to internal representation, the RTL (register transfer language). This is a close-to-assembly representation.
Then this RTL code is transformed to target machine's assembly and written to .o (object) file.
The linker then combines generated .o-files to the executable.
The "inline" assembly snippets are also supported by GCC in C/C++.
The workflow is
Source file ->
AST ->
RTL representation ->
machine codes (with _optional_ text output of the ASM code) ->
Executable (produced by linker)
For the interpreter you may directly interpret the AST or produce you own opcodes for the virtual machine since such an interpreter (virtual machine) would be simpler than the AST interpreter.
If you want all the details you should look at LCC (with a book by Chris Fraser and David Hanson). All the details of code generation for real-world architectures are provided in the accompanying book.
And to know what can be done with the generated code you should read the Linkers and Loaders by John Levine book.
Finally, to avoid asking everything about scripting/interpreters, refer to Game Scripting Mastery by Alex Varanese.
Quite vague a question, and I don't think I fully understood what your exact problem is, but here you are an answer anyway: assembly is not put in the executable. Assembly is written to an intermediate assembly file, from which the assembler generates true binary machine code (called an object file), then the linker merges them (along with the needed libraries) to the final executable. When the application is run, the executable is loaded directly into the RAM by the OS and executed natively by the processor.
HOW A SOURCE CODE TRANSLATED TO EXECUTABLE CODE ?
We provide Source Code to the compiler and it gives us Executable code .But this is not a single step operation .This follow some predefined steps to convert the Source Code to Executable Code.
steps followed for conversion from source code to executable code
1.Preprocessor
It is very useful part of the compiler as it does lots of job before translated to machine code. It is a text processor which dose the below text editing operation
It removes the comment lines in source code which are written in the Source code for more readability/easy understanding .
It add the content of header files to the source code. Header files always contains function prototypes and declarations.(Header files never contain any executable code )
A very important property of Preprocessor is Conditional Compile. It is very required for scalable design. This property also remove the unnecessary burden from compiler.
Macros are replaced by this preprocessor.
The final output of this stage is known as pure C code.
2.Translator
This part of complier is responsible for converting of pure C code to assembly language code.
Step by step mapping of C language code to assembly language code done here.
The prototypes of the functions and declarations are used by this part for translation of C code.
The out put of this stage known as assembly code.
3.Assembler
It generate Object code from assembly language code.It converts the assembly language codes to machine language code(i.e in 0's and 1's format).It is not directly run as we take the help of OS to execute our code in processor.
The out put of this stage known as object code.
4.Linker
It give the final executable code which is going to be run in our machine. The output of this stage is known as executable code. Which is a combination of object code and supporting files.
The supporting files may be user defined function definitions ,predefined library function definitions ...etc.

Resources