我如何在OS X上运行这个汇编代码? [英] How can I run this assembly code on OS X?

查看:283
本文介绍了我如何在OS X上运行这个汇编代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始学习汇编,我得到了一些在Linux上的类中创建的Hello World汇编代码。我想让它适用于64位Mac OS X.

Starting to learn assembly, I was given some Hello World assembly code created during the class on Linux. I would like to get it to work for 64-bit Mac OS X.

code.asm

code.asm:

SECTION .data       
    hola:   db "Hola!",10   
    tam:    equ $-hola      

SECTION .text       
    global main     

main:               

    mov edx,tam     
    mov ecx,hola        
    mov ebx,1       
    mov eax,4       
    int 0x80        

    mov ebx,0       
    mov eax,1       
    int 0x80        

这就是我所做的:

This is what I do:

nasm -f macho32 -o object.o code.asm
gcc -m32 -o program object.o

它告诉我:
$ b

Which tells me:


架构i386的未定义符号:_main,引用来自:
开始于crt1.10.6.o ld:symbol(s)not found for architecture i386

Undefined symbols for architecture i386: "_main", referenced from: start in crt1.10.6.o ld: symbol(s) not found for architecture i386

寻找这个错误,我发现了这个问题: nasm和gcc:32位链接失败(64位Mac OS X)

Searching for this error, I found this question: nasm and gcc: 32 bit linking failed (64 bit Mac OS X)

一个答案说:

One answer says


你遇到的问题是你正在创建一个32位Linux(ELF)
对象文件,它与Mac OS X对象格式不兼容。
尝试将'-f elf'切换为'-f macho32'。

The problem you're having is that you're creating a 32-bit Linux(ELF) object file which isn't compatible with the Mac OS X object format. Try switching '-f elf' to '-f macho32'.

但是我绝对使用 -f macho32 。那么问题是什么呢?

But I'm definitely using -f macho32. So what would the problem be then?

推荐答案

我也一直在尝试自学一些入门级的汇编程序,我遇到了类似的问题。我最初编译时使用 nasm elf ,但是当我尝试使用 ld 来链接目标文件并创建可执行文件。

I've been trying to teach myself some entry-level Assembly programming too, and I ran into similar issues. I had originally compiled using nasm with elf, but that didn't work when I tried to use ld to link the object file and create the executable.

我想你主要问题的答案那么问题就出现了吗? [让这个在64位MacOSX上运行] 是:您正在使用 -f macho32 ,但希望它在64位机器上运行,您需要将命令选项更改为 -f macho64 当然,这不会解决您的汇编代码是针对不同体系结构编写的事实(更多关于这一点)。

I think the answer you main question "what would the problem be then?" [to get this to run on 64bit MacOSX] is: You are using -f macho32 but expecting it to run on a 64bit machine, you need to change the command option to be -f macho64. Of course, this will not resolve the fact that your assembly code is written for a different architecture (more on that in a bit).

我发现这个方便的答案在这个实例中用于编译和链接你的代码(),在重构汇编代码以使用正确的语法而不是* nix duskwuff 陈述): nasm -f macho64 main.asm -o main.o &安培;&安培; ld -e _main -macosx_version_min 10.8 -arch x86_64 main.o -lSystem

I found this handy answer on the right command to use in this instance to compile and link your code (after you refactor your assembly code to use the proper syntax instead of *nix as duskwuff stated): nasm -f macho64 main.asm -o main.o && ld -e _main -macosx_version_min 10.8 -arch x86_64 main.o -lSystem


  1. 在Mac 64bit上,最好使用作为汇编程序而不是 nasm (如果你想要更本地化的东西),但是如果你想要更多的可移植代码(学习差异)。 > nasm 没有默认安装的macho64输出类型
  2. 在keister中这是一件痛苦的事情(这边)

  1. On Mac 64bit, it might be better to use the as assembler instead of nasm (if you want something more native), but if you want more portable code (learn the differences).
  2. nasm doesn't come with the macho64 output type installed by default
  3. Assembly is a pain in the keister (this aside)

现在我的学习咆哮已经完成了......

Now that my learning rant is out of the way...

下面是代码在MacOSX 64上使用 nasm 操作(如果已使用更新 nasm > macho64 ,记入 Dustin Schultz ):

Here is the code which should operate on MacOSX 64 using nasm (if you have updated nasm with macho64, credit to Dustin Schultz):

section .data
hello_world     db      "Hello World!", 0x0a

section .text
global start

start:
mov rax, 0x2000004      ; System call write = 4
mov rdi, 1              ; Write to standard out = 1
mov rsi, hello_world    ; The address of hello_world string
mov rdx, 14             ; The size to write
syscall                 ; Invoke the kernel
mov rax, 0x2000001      ; System call number for exit = 1
mov rdi, 0              ; Exit success = 0
syscall                 ; Invoke the kernel

一起使用的工作代码为汇编程序原生MacOSX64:

Working code I used with the as assembler native to MacOSX64:

.section __TEXT,__text

.global start

start:
  movl $0x2000004, %eax           # Preparing syscall 4
  movl $1, %edi                   # stdout file descriptor = 1
  movq str@GOTPCREL(%rip), %rsi   # The string to print
  movq $100, %rdx                 # The size of the value to print
  syscall

  movl $0, %ebx
  movl $0x2000001, %eax           # exit 0
  syscall

.section __DATA,__data
str:
  .asciz "Hello World!\n"

编译命令: as -arch x86_64 -o hello_as_64.o hello_as_64.asm

链接命令: ld -o hello_as_64 hello_as_64.o

执行命令: ./ hello_as_64

AS OSX汇编程序参考 https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/Assembler/Assembler.pdf

AS OSX Assembler Reference: https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/Assembler/Assembler.pdf

在Mac OSX上编写64位汇编 http://www.idryman.org/blog/2014/12/02/writing-64-bit-assembly- on-mac-os-x /

无法使用 ld
无法连结使用ld的目标文件 - Mac OS X

OSX i386 SysCalls http://www.opensou rce.apple.com/source/xnu/xnu-1699.26.8/osfmk/mach/i386/syscall_sw.h

OSX主系统致电定义 http ://www.opensource.apple.com/source/xnu/xnu-1504.3.12/bsd/kern/syscalls.master

OSX Syscall https ://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/syscall.2.html

这篇关于我如何在OS X上运行这个汇编代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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