xcode 如何在 Mac 上运行 asm 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16772657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i run asm files on a mac?
提问by Thijser
I'm trying to run a .asm file on a mac (10.7) for a school project. However I can't quite seem to figure out how to actually run it. I know I can run it from the terminal but how?
我正在尝试在 mac (10.7) 上为学校项目运行 .asm 文件。但是,我似乎不太清楚如何实际运行它。我知道我可以从终端运行它,但是如何运行?
Or do I have to use xcode to actually run .asm files? Or should I convert the assembly code into another format manually?
或者我必须使用 xcode 来实际运行 .asm 文件吗?或者我应该手动将汇编代码转换为另一种格式?
For reference the program I'm trying to run is
作为参考,我试图运行的程序是
# ************************************************************************
# * Program name : sieve *
# * Description : this program prints all the prime numbers below 1000 *
# ************************************************************************
.bss
NUMBERS: .skip 1000 # memory space for the number table
.text
formatstr: .asciz "%d\n" # format string for number printing
.global main
# ************************************************************************
# * Subroutine : main *
# * Description : application entry point *
# ************************************************************************
main: movl %esp, %ebp # initialize the base pointer
# Initialize the number table:
movl $ clang -m32 -g sieve.S -o sieve
$ ./sieve
2
3
5
<...>
, %eax # initialize 'i' to 0.
loop1: movb , NUMBERS(%eax) # set number table entry 'i' to 'true'
incl %eax # increment 'i'
cmpl 00, %eax # while 'i' < 1000
jl loop1 # go to start of loop1
# The sieve algorithm:
pushl # initialize 'number' to 2 on stack
loop2: movl -4(%ebp), %eax # load 'number' into a register
cmpb , NUMBERS(%eax) # compare NUMBERS[number] to '1'
jne lp2end # if not equal, jump to end of loop 2
pushl $formatstr # push the format string for printing
call printf # print the number
addl , %esp # pop the format string
movl -4(%ebp), %eax # 'multiple' := 'number'
shl , %eax # multiply 'multiple' by 2
loop3: cmp 00, %eax # compare 'multiple' to 1000
jge lp2end # goto end of loop2 if greater/equal
movb $ gcc -o sieve sieve.s
$./sieve
, NUMBERS(%eax) # set number table entry to 'false'
addl -4(%ebp), %eax # add another 'number' to 'multiple'
jmp loop3 # jump to the beginning of loop 3
lp2end: movl -4(%ebp), %eax # load 'number' into a register
incl %eax # increment 'number' by one
movl %eax, -4(%ebp) # store 'number' on the stack
cmpl 00, %eax # compare 'number' to 1000
jl loop2 # if smaller, repeat loop2
end: movl $ gcc -o sieve sieve.s
$./sieve
,(%esp) # push program exit code
call exit # exit the program
回答by scottt
To build and run this on the command line, i.e. inside the Terminalapplication on a Mac, save the file to sieve.S
and do something like:
要在命令行上构建和运行它,即在Mac 上的终端应用程序中,将文件保存到sieve.S
并执行以下操作:
I don't know how to build this in Xcode offhand. Just creating an empty project and adding sieve.S
as a source file might work.
我不知道如何在 Xcode 中立即构建它。只需创建一个空项目并添加sieve.S
为源文件即可。
回答by rhegler19
- Save the file as a .s file.
- Go to this URL in a web browser: https://developer.apple.com/downloads/index.action=Command%20Line%20Tools%20%28OS%20X%20Mountain%20Lion%29
- Install Xcode Command Line Tools
- Open Terminal.app (/Applications/Utilities/Terminal.app).
- Navigate to the directory you saved your program in using "cd".
Execute these commands:
##代码##
- 将文件另存为 .s 文件。
- 在 Web 浏览器中转到此 URL:https: //developer.apple.com/downloads/index.action=Command%20Line%20Tools%20%28OS%20X%20Mountain%20Lion%29
- 安装 Xcode 命令行工具
- 打开 Terminal.app (/Applications/Utilities/Terminal.app)。
- 使用“cd”导航到您保存程序的目录。
执行这些命令:
##代码##