3

我正在尝试在 Mac(10.7)上为学校项目运行 .asm 文件。但是我似乎无法弄清楚如何实际运行它。我知道我可以从终端运行它,但如何?

还是我必须使用 xcode 来实际运行 .asm 文件?还是我应该手动将汇编代码转换为另一种格式?

作为参考,我尝试运行的程序是

# ************************************************************************
# * 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 $0, %eax # initialize 'i' to 0.
   loop1: movb $1, NUMBERS(%eax) # set number table entry 'i' to 'true'
   incl %eax # increment 'i'
   cmpl $1000, %eax # while 'i' < 1000
   jl loop1 # go to start of loop1
   # The sieve algorithm:
   pushl $2 # initialize 'number' to 2 on stack   
   loop2: movl -4(%ebp), %eax # load 'number' into a register
   cmpb $1, 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 $4, %esp # pop the format string
   movl -4(%ebp), %eax # 'multiple' := 'number'
   shl $1, %eax # multiply 'multiple' by 2
   loop3: cmp $1000, %eax # compare 'multiple' to 1000
   jge lp2end # goto end of loop2 if greater/equal
   movb $0, 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 $1000, %eax # compare 'number' to 1000
   jl loop2 # if smaller, repeat loop2
   end: movl $0,(%esp) # push program exit code
   call exit # exit the program
4

2 回答 2

1
  1. 将文件另存为 .s 文件。
  2. 在 Web 浏览器中访问此 URL: https ://developer.apple.com/downloads/index.action=Command%20Line%20Tools%20%28OS%20X%20Mountain%20Lion%29
  3. 安装 Xcode 命令行工具
  4. 打开 Terminal.app (/Applications/Utilities/Terminal.app)。
  5. 使用“cd”导航到您保存程序的目录。
  6. 执行这些命令:

    $ gcc -o sieve sieve.s
    $./sieve
    
于 2014-03-23T19:57:39.847 回答
1

要在命令行上构建和运行它,即在 Mac 上的终端应用程序中,将文件保存到sieve.S并执行以下操作:

$ clang -m32 -g sieve.S -o sieve
$ ./sieve 
2
3
5
<...>

我不知道如何在 Xcode 中临时构建它。只需创建一个空项目并添加sieve.S为源文件即可。

于 2013-05-27T14:00:56.723 回答