Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode and NASM coding

Tags:

xcode

nasm

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

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.

like image 419
user1477988 Avatar asked Dec 12 '13 23:12

user1477988


People also ask

Can you code assembly on Xcode?

You can step into your assembly function with Xcode by using CTRL-F7 or CTRL+click -ing the Step in button in the Xcode GUI. The LLDB command line is also very useful in order to inspect registers since, unlike Visual Studio, Xcode lacks a Registers window.

What is Nasm programming?

The Netwide Assembler (NASM) is an assembler and disassembler for the Intel x86 architecture. It can be used to write 16-bit, 32-bit (IA-32) and 64-bit (x86-64) programs. It is considered one of the most popular assemblers for Linux.

How do I run an assembly language program on a Mac?

To be able to run an assembly program we need first to create an executable. This is a two step process. First you compile your source code by using the as command line tool, this will produce an intermediate object code that would need to be linked via the ld tool in the second step.


1 Answers

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.

Xcode 9.4.1

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 (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:

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()

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. 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

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

Terminal

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.

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

like image 186
Buggus Mageevers Avatar answered Oct 05 '22 09:10

Buggus Mageevers