I try to covert my c code to assembly by gcc(by typing gcc -S -masm=intel or pg.c or gcc -S prog.c) but it gives me masm code but i need nasm one . i wonder if you could help me to covert my c to nasm assembly
Master C and Embedded C Programming- Learn as you go Here we will see how to generate assembly language output from C or C++ source code using gcc. The gcc provides a great feature to get all intermediate outputs from a source code while executing. To get the assembler output we can use the option '-S' for the gcc.
The Netwide Assembler (NASM) is an assembler and disassembler for the Intel x86 architecture. It can be used to write 16-bit, 32-bit (IA-32) and 64-bit (x86-64) programs. It is considered one of the most popular assemblers for Linux.
It is explained here: How to generate a nasm compilable assembly code from c source code on Linux? but I will give you a full explanation. Step by Step :
Step 1 : Write hello.c:
#include <stdio.h>
int main()
{
printf( "Hello World \n" );
return 0;
}
Step 2 : Create the object file :
gcc -fno-asynchronous-unwind-tables -s -c -o hello.o hello.c
Step 3 : Disassemble the object file
objconv -fnasm hello.o #this creates hello.asm
See the end to install objconv, you really need it because objdumb (installed on linux) only output an human readable and long long output. Now let's look at hello.asm :
; Disassembly of file: hello.o
; Mon Dec 1 13:08:02 2014
; Mode: 32 bits
; Syntax: YASM/NASM
; Instruction set: 80386
global main: function
extern puts ; near
SECTION .text align=4 execute ; section number 1, code
main: ; Function begin
push ebp ; 0000 _ 55
mov ebp, esp ; 0001 _ 89. E5
and esp, 0FFFFFFF0H ; 0003 _ 83. E4, F0
sub esp, 16 ; 0006 _ 83. EC, 10
mov dword [esp], ?_001 ; 0009 _ C7. 04 24, 00000000(d)
call puts ; 0010 _ E8, FFFFFFFC(rel)
mov eax, 0 ; 0015 _ B8, 00000000
leave ; 001A _ C9
ret ; 001B _ C3
; main End of function
SECTION .data align=4 noexecute ; section number 2, data
SECTION .bss align=4 noexecute ; section number 3, bss
SECTION .rodata align=1 noexecute ; section number 4, const
?_001: ; byte
db 48H, 65H, 6CH, 6CH, 6FH, 20H, 57H, 6FH ; 0000 _ Hello Wo
db 72H, 6CH, 64H, 20H, 00H ; 0008 _ rld .
You need to remove the "function" (line 8) and all the "align=? noexecute" where ? represents a digit.
Step 4 Assemble :
nasm -f elf hello.asm #This creates a new hello.o, actually the same :)
gcc hello.o -o hello # this creates a binary hello, use gcc and no ld because of the call of external functions
./hello # output : hello world
Anexe 1 Install objconv :
Anexe 2
You want to make good programs in Nasm, maybe see this package full of examples : http://sourceforge.net/projects/nasmx
Question is a bit unclear , but more or less you can do that by opening your c executable in a debugger and copying the relevant code . That will give you the "Shellcode" , if that is what you are looking for .
But if you are planning to convert a full fledged C code to NASM you should take up that MASM code and rewire it for NASM .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With