Xcode和NASM编码 [英] Xcode and NASM coding

查看:127
本文介绍了Xcode和NASM编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用Xcode的汇编语言编写和构建程序?

How can i write and build programs in assembly language in Xcode?

我正在搜索,但没有成功.你能帮助我吗?如果无法用xcode编码NASM,请推荐一些不错的IDE.

I search for it but I wasn't successfull. Can you help me? If it isn't possible to code NASM in xcode please recommend some good IDE.

推荐答案

自您提出此问题以来,这可能已经有所更改,但是当前正在安装

This may have been a change since you asked this question, but currently, installing the Xcode command line tools (after installing Xcode) installs NASM (Netwide Assembler) and GASM (GNU Assembler). To start coding in assembly, you have a couple options depending on what you are doing: namely, building in Xcode, or building in Terminal using NASM or GASM directly.

如果要使用IDE,可以通过单击文件>新建文件",然后搜索装配"将装配文件添加到Xcode中,然后将显示装配文件类型.或者,您可以添加一个空白文件,然后从文件检查器的类型"下拉列表中手动选择文件类型.除非您的应用程序需要Cocoa框架,否则应在创建项目/目标时创建命令行应用程序而不是Cocoa应用程序.作为示例命令行程序:

If you want to work with an IDE, you may add assembly files into Xcode by clicking "File > New File" and then searching for "Assembly" you'll be presented with an assembly file type. Alternatively, you may add a blank file and then manually select the file type from the "Type" drop down in the File Inspector. Unless you need a the Cocoa framework for your app, you should create a command line app instead of a Cocoa app during project/target creation. As an example Command Line program:

hello.asm(来自参考文献中列出的教程网站):

hello.asm (from tutorial site listed in references):

global    _start

section   .text

_start: mov       rax, 0x02000004         ; system call for write
        mov       rdi, 1                  ; file handle 1 is stdout
        mov       rsi, message            ; address of string to output
        mov       rdx, 13                 ; number of bytes
        syscall                           ; invoke operating system to do the write
        mov       rax, 0x02000001         ; system call for exit
        xor       rdi, rdi                ; exit code 0
        syscall                           ; invoke operating system to exit

        section   .data
message:  db        "Hello, World", 10      ; note the newline at the end

main.swift:

main.swift:

import Foundation

//  Generate a "name" for the assembler operation that may be used
//  as a Swift function.
@_silgen_name("start") func start() -> String

//  Create a fake struct to use our function.  We return 0 so that we
//  can call `variable()` below without any warnings (because we're
//  we're setting something).
struct Test {
    func variable() -> Int32 {
        print(start())
        return 0
    }
}

//  Declare a test instance and call `variable`.  `x` is merely acting 
//  as a placeholder so we can call variable and not get warnings for
//  this test example.
let x = Test().variable()

如果您希望对汇编操作使用C而不是Swift,则需要创建头文件而不是使用@_silgen_name:

In the case that you wish to use C for your Assembly operations instead of Swift, you'll need to create header file instead of using @_silgen_name:

#ifndef Bridging_Header_h
#define Bridging_Header_h

const char *start(void);

#endif /* Bridging-Header_h */

装配体建造规则

重要的是,您还应为目标提供构建规则".为此:

Assembly Build Rule

It is important that you also provide a "build rule" for the target. To do so:

  1. 在项目浏览器"中单击项目图标
  2. 在目标列表中选择适当的目标
  3. 点击构建规则"标签
  4. 在搜索字段中搜索NASM
  5. 单击复制到目标",并确保将进程"设置为"NASM程序集文件",并将使用"设置为自定义脚本"
  6. 在下面的自定义脚本"部分中,键入以下命令(确保路径指向您的 NASM汇编器的位置):
    /usr/local/bin/nasm -f macho64 ${INPUT_FILE_PATH} -o ${SCRIPT_OUTPUT_FILE_0} 这是终端命令-要了解更多信息,请在终端中键入man nasm.
  7. 然后在输出文件"部分中单击加号并添加以下内容:
    $(DERIVED_FILE_DIR)/${INPUT_FILE_BASE}.o
  1. Click on the project icon in the Project Navigator
  2. Select the appropriate Target in the Target List
  3. Click on the "Build Rules" tab
  4. Search for NASM in the search field
  5. Click on "Copy to Target", and make sure "Process" is set to "NASM assembly files", and "Using" is set to "Custom script"
  6. In the "Custom script" section below, type in the following command (making sure the path directs to the location of your NASM assembler):
    /usr/local/bin/nasm -f macho64 ${INPUT_FILE_PATH} -o ${SCRIPT_OUTPUT_FILE_0} This is a Terminal command--to find out more, type man nasm in Terminal.
  7. Then click on the plus sign in the "Output Files" section and add the following:
    $(DERIVED_FILE_DIR)/${INPUT_FILE_BASE}.o

此构建规则对于避免编译器错误至关重要,该错误指出对于体系结构x86_64找不到符号".

This build rule is essential to avoid a compiler error that states, "Symbol(s) not found for architecture x86_64".

如果您不介意,或者可能更喜欢在Terminal中工作,则可以使用自己选择的文本编辑器(vimnanoemacs内置在Terminal中,而TextEdit内置在macOS中)以创建您的程序集文件.然后使用nasmgasm命令来汇编文件.键入man nasmman gasm作为您可用的各种选项.

If you don't mind, or perhaps prefer to work in Terminal, you may utilize your text editor of choice (vim, nano, and emacs are built into Terminal, and TextEdit is built into macOS) to create you assembly file. Then use nasm or gasm commands to assemble your files. Type man nasm or man gasm for the various options available to you.

参考文献:
汇编代码示例-
hello.asm
从Swift或C引用程序集(需要桥接标题)- Daniel Tran
构建规则-公制熊猫

References:
Assembly code example - hello.asm
Referencing Assembly from Swift or C (requires Bridging Header) - Daniel Tran
Build Rule - Metric Panda

这篇关于Xcode和NASM编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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