zoukankan      html  css  js  c++  java
  • 汇编: Linux中汇编程序的编译

    NASM在Linux系统上创建汇编程序

    编写汇编程序

    • Code

          ;; 可执行文件名: helloworld.asm
          ;; 程序版本: 0.01
          ;; 创建日期: 2019/1/02
          ;; 最后修改日期: 2019/1/02
          ;; 作者: ieeqc
          ;; 描述:
          ;; - 汇编语言helloworld程序实现
          ;; 
          ;; 编译指令
          ;; - nasm -f elf64 -g -F stabs helloworld.asm
          ;; - ld -o helloworld helloworld.o
      
          SECTION .data               ; 包含已经初始化的数据段
          
      EatMsg: db "Hello World!", 10   ; 10 代表 ASCII回车等同于c语言"
      "
      EatLen: equ $-EatMsg            ; 计算EatMsg的串长度
          
          SECTION .bss                ; 包含未初始化的数据段
          SECTION .text               ; 包含代码段
      
          global _start               ; 连接器根据此寻找程序入口点
          
      _start:
          nop                         ; 方便gdb调试
          mov eax, 4                  ; 系统调用编号: sys_write
          mov ebx, 1                  ; 指定文件描述符为1, 标准输出
          mov ecx, EatMsg             ; 传递打印信息
          mov edx, EatLen             ; 传递打印信息的长度
          int 80H                     ; 中断执行系统调用
          mov eax, 1                  ; 系统调用编号: Exit
          mov ebx, 0                  ; 返回一个0代表运行成功
          int 80H                     ; 中断执行系统调用
      
      

    使用NASM编译程序

    NASM与ld不兼容

    • 创建程序

      nasm -f elf -g -F stabs helloworld.asm
      
      ld -o helloworld helloworld.o
      
      • 可能出现错误: ld: i386 architecture of input file 'helloworld.o' is incompatible with i386:x86-64 output
      • 原因:
        • nasm编译器默认输出32位程序, 在而ld默认输出64位,不兼容

    正确创建方法

    • 64位

      nasm -f elf64 -g -F stabs helloworld.asm -o helloworld.o
      
      ld -o helloworld helloworld.o
      
    • 32位

      nasm -f elf -g -F stabs helloworld.asm -o helloworld.o
      
      ld -m elf_i386 -o helloworld helloworld.o
      
  • 相关阅读:
    【转】构建高并发高可用的电商平台架构实践
    【转】深入解析浏览器的幕后工作原理
    【转】解释器,树遍历解释器,基于栈与基于寄存器
    EasyDarwin返回401 Unauthorized解决方法
    【转】SDP file
    音频PCM格式
    H264相关知识
    testNG之异常测试
    testNG之组测试
    testNG之顺序执行
  • 原文地址:https://www.cnblogs.com/ieeqc/p/14531991.html
Copyright © 2011-2022 走看看