将C转换为nasm程序集 [英] Converting C to nasm assembly

查看:205
本文介绍了将C转换为nasm程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过gcc(通过键入gcc -S -masm = intel或pg.c或gcc -S prog.c)将我的C代码转换为汇编代码,但是它给了我Masm代码,但我需要使用nasm一个.我想知道您是否可以帮助我将C转换为nasm程序集

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

推荐答案

此处解释如下:

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 :

第1步:编写hello.c:

Step 1 : Write hello.c:

#include <stdio.h>
int main()
{
printf( "Hello World \n" );
return 0;
}


第2步:创建目标文件:


Step 2 : Create the object file :

gcc -fno-asynchronous-unwind-tables -s -c -o hello.o hello.c


步骤3 :反汇编目标文件


Step 3 : Disassemble the object file

objconv -fnasm hello.o   #this creates hello.asm

看到最后安装objconv,您确实需要它,因为objdumb(安装在linux上)仅输出人类可读且很长的输出.现在让我们来看一下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 .

您需要删除功能"(第8行)和所有"align =?noexecute",其中?代表一个数字.

You need to remove the "function" (line 8) and all the "align=? noexecute" where ? represents a digit.

第4步:组装:

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 安装objconv:


Anexe 1 Install objconv :

  • 转到此网站 http://www.agner.org/optimize/#objconv
  • 单击下载并解压缩objconv.zip
  • 解压缩source.zip并为Linux运行build.sh(为window运行objconv.exe),这将创建一个可执行的objconv
  • 将objconv移动到您的二进制文件中(立即执行!!)或只运行./objconv(也许如果不允许,您必须先运行chmod 777 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

您想使用Nasm制作好的程序,也许会看到此示例包,其中包括: http://sourceforge. net/projects/nasmx

You want to make good programs in Nasm, maybe see this package full of examples : http://sourceforge.net/projects/nasmx

这篇关于将C转换为nasm程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆