11

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 although I need NASM one. I wonder if you could help me to convert my c to NASM assembly.

Duke William
  • 151
  • 6
Abtin
  • 301
  • 2
  • 3
  • 7
  • Can you just manually convert the masm output to nasm? See http://stackoverflow.com/questions/2035747/masm-nasm-differences and http://left404.com/2011/01/04/converting-x86-assembly-from-masm-to-nasm-3/. – lurker Jul 16 '13 at 12:15
  • I think gcc outputs as code, not masm code. – Jens Björnhager Jul 17 '13 at 15:47
  • For anyone coming from Google, the answer can be found here http://stackoverflow.com/questions/20737947/how-to-generate-a-nasm-compilable-assembly-code-from-c-source-code-on-linux/20743090#20743090 /shame – Babken Vardanyan Apr 05 '14 at 18:29

3 Answers3

23

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 :

  • 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

Tinmarino
  • 3,693
  • 24
  • 33
  • 1
    Objconv indeed is useful, thanks. But to NASMX, I must add, that it is maybe better to first learn the basic NASM syntax and instructions for 80386+ before going for NASMX.. It is somewhat true. Instead of using the same coded over and over, it is better to use existing libraries for maintainability and NASMX is quite powerful, maybe too powerful and high level for ASM even – christopher westburry Aug 02 '18 at 21:07
1

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 .

oldnoob
  • 123
  • 5
1

Although this is an indirect way of solving OP's problem, it is presented as someone might find it useful. A python script to convert MASM to NASM: Ooops! Looks like script exceeds the character limit imposed by "stackoverflow" so I had to delete it.

Instead here is the direct download link: MASM to NASM

The reference web page for the download: masm32.com

Duke William
  • 151
  • 6