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
问题:
回答1:
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 ( I need reputation because I want to vote. Anyway ... ). 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 :
- Go to this site http://www.agner.org/optimize/#objconv
- Click on download and extract the objconv.zip
- Extract the source.zip and run build.sh for linux ( run objconv.exe for window ), this creates an executable objconv
- move objconv to your binaries (do it now !!) or just run ./objconv (maybe you must run chmod 777 objconv before if you're not allowed)
Anexe 2
You want to make good programs in Nasm, maybe see this package full of examples : http://sourceforge.net/projects/nasmx
回答2:
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 .