在ubuntu下用nasm和gcc的ld链接程…

我们先写一个.asm文件
[SECTION .data]
StrHello db "Hello, kaito!", 0Ah
HelloLen equ $ - StrHello

[SECTION .text]
global _start ;must be decleared for linker(ld)

_start: ;tell linker entry point
mov edx, HelloLen ;message length
mov ecx, StrHello ;message to write
mov ebx, 1 ;file descriptor(stdout, standard output)
mov eax, 4 ;system call number(sys_write)
int 0x80 ;call kernel
mov ebx, 0
mov eax, 1 ;system call number(sys_exit)
int 0x80 ;call kernel

在nasm中可以指定输出文件格式,其中有如下
valid output formats for -f are (`*' denotes default): 
  * bin       flat-form binary files (e.g. DOS .COM, .SYS) 
    ith       Intel hex 
    srec      Motorola S-records 
    aout      Linux a.out object files 
    aoutb     NetBSD/FreeBSD a.out object files 
    coff      COFF (i386) object files (e.g. DJGPP for DOS) 
    elf32     ELF32 (i386) object files (e.g. Linux) 
    elf64     ELF64 (x86_64) object files (e.g. Linux) 
    as86      Linux as86 (bin86 version 0.3) object files 
    obj       MS-DOS 16-bit/32-bit OMF object files 
    win32     Microsoft Win32 (i386) object files 
    win64     Microsoft Win64 (x86-64) object files 
    rdf       Relocatable Dynamic Object File Format v2.0 
    ieee      IEEE-695 (LADsoft variant) object file format 
    macho32   NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files 
    macho64   NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files 
    dbg       Trace of all info passed to output stage 
    elf       ELF (short name for ELF32) 
    macho     MACHO (short name for MACHO32) 
    win       WIN (short name for WIN32) 

由于我的虚拟机是i386:x86-64的,所以在编译的时候要选择elf64

nasm -f elf64 hello.asm -o hello.o
ld -s hello.o -o hello
运行hello文件
./hello
在ubuntu下用nasm和gcc的ld链接程序


如果想要在64位Linux上编译出32位.o文件,则在编译时加上编译选项   -melf_i386


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值