【问题标题】:Using NASM and ld on Mac OSX在 Mac OSX 上使用 NASM 和 ld
【发布时间】:2012-12-22 22:05:38
【问题描述】:

我是汇编的初学者(事实上这是我第一次尝试),我想知道如何使用 NASM 和 ld 链接器让这个 x86 汇编代码在我的 Mac 上运行。

SECTION .data           ; Section containing initialised data

    EatMsg: db "Eat at Joe's!",10
    EatLen: equ $-EatMsg    

SECTION .bss            ; Section containing uninitialized data 

SECTION .text           ; Section containing code

global  _start          ; Linker needs this to find the entry point!

_start:
    nop         ; This no-op keeps gdb happy...
    mov eax,4       ; Specify sys_write call
    mov ebx,1       ; Specify File Descriptor 1: Standard Output
    mov ecx,EatMsg      ; Pass offset of the message
    mov edx,EatLen      ; Pass the length of the message
    int 80H         ; Make kernel call

    MOV eax,1       ; Code for Exit Syscall
    mov ebx,0       ; Return a code of zero 
    int 80H         ; Make kernel call

这个汇编代码来自一本为 linux 编写的书,但是由于 linux 和 mac 都是基于 unix 的操作系统,我认为汇编代码通常是相同的。我现在意识到我可能无法通过 nasm 和 ld 将它转换为 mac 可执行文件,但如果可以,我该怎么做?如果不是,我将如何更改此汇编代码以使其正常工作,但通常做同样的事情?

【问题讨论】:

  • 谢谢,但我还是个初学者,您介意解释一下我应该如何调用标准库吗?

标签: macos assembly x86 nasm ld


【解决方案1】:

以下示例代码在使用 nasm 组装并使用 gcc 链接时应该可以工作(需要与标准 c 库链接!)

SECTION .data           ; Section containing initialised data

    EatMsg: db "Eat at Joe's!",10
    EatLen: equ $-EatMsg

SECTION .bss            ; Section containing uninitialized data

SECTION .text           ; Section containing code

global main
extern write

main:
    sub esp, 12                 ; allocate space for locals/arguments
    mov dword [esp], 1          ; stdout
    mov dword [esp+4], EatMsg   ; Pass offset of the message
    mov dword [esp+8], EatLen   ; Pass the length of the message
    call write                  ; Make library call

    mov eax,0                   ; Return a code of zero
    add esp, 12                 ; restore stack
    ret

请注意,“Hello world”是一个特别糟糕的第一个 asm 程序,通常不建议从 asm 进行 I/O 和其他高级操作。

【讨论】:

  • 很好的答案。组装和链接的语法和方法看起来与 linux 完全不同,我似乎找不到任何专注于 mac 组装的好的和全面的资源,有人知道吗?
【解决方案2】:

int 80H 在 OS X 上已弃用,无论如何它使用与 Linux 不同的选择器。您可能应该使用 syscall 或调用标准 C 库。

请参阅:thexploit.com/secdev/mac-os-x-64-bit-assembly-system-calls,获取在 Mac OS X 上使用 nasm 的有用“Hello World”教程。

有关使用 nasm 构建 OS X 可执行文件的更多信息,另请参阅:daveeveritt.tumblr.com/post/67819832/webstraction-2-from-unix-history-and-assembly-language

【讨论】:

  • thexploit 链接是 404'd。 thexploit.com 主页似乎是一个空的 apache 服务器。
  • 谢谢 - 我会删除死链接。
猜你喜欢
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 2014-12-10
  • 1970-01-01
相关资源
最近更新 更多