OS X中的汇编语言 [英] assembly language in os x

查看:85
本文介绍了OS X中的汇编语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我逐步使用汇编语言来学习Linux上的汇编语言编程.我最近有一台Mac,在int 0x80上似乎无法使用(非法指令).

I used assembly language step by step to learn assembly language programming on linux. I recently got a Mac, on which int 0x80 doesn't seem to work (illegal instruction).

所以只是想知道是否有一个很好的参考书(书/网页),它给出了标准unix程序集和darwin程序集之间的区别.

So just wanted to know if there is a good reference (book/webpage) which gives the differences b/w the standard unix assembly and darwin assembly.

推荐答案

出于实际目的,此答案显示

For practical purposes, this answer shows how to compile a hello world application using nasm on OSX.

此代码可以按原样为linux编译,但是用于编译它的cmd-line命令可能会有所不同:

This code can be compiled for linux as is, but the cmd-line command to compile it would probably differ:

section .text

global mystart                ; make the main function externally visible

mystart:

; 1 print "hello, world"

    ; 1a prepare the arguments for the system call to write
    push dword mylen          ; message length                           
    push dword mymsg          ; message to write
    push dword 1              ; file descriptor value

    ; 1b make the system call to write
    mov eax, 0x4              ; system call number for write
    sub esp, 4                ; OS X (and BSD) system calls needs "extra space" on stack
    int 0x80                  ; make the actual system call

    ; 1c clean up the stack
    add esp, 16               ; 3 args * 4 bytes/arg + 4 bytes extra space = 16 bytes

; 2 exit the program

    ; 2a prepare the argument for the sys call to exit
    push dword 0              ; exit status returned to the operating system

    ; 2b make the call to sys call to exit
    mov eax, 0x1              ; system call number for exit
    sub esp, 4                ; OS X (and BSD) system calls needs "extra space" on stack
    int 0x80                  ; make the system call

    ; 2c no need to clean up the stack because no code here would executed: already exited

section .data

  mymsg db "hello, world", 0xa  ; string with a carriage-return
  mylen equ $-mymsg             ; string length in bytes

将源文件(hello.nasm)组装到目标文件中:

Assemble the source (hello.nasm) to an object file:

nasm -f macho hello.nasm

链接以生成可执行文件:

Link to produce the executable:

ld -o hello -e mystart hello.o

这篇关于OS X中的汇编语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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