我正在尝试在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
答案 0 :(得分:1)
要在命令行上构建并运行它,即在Mac上的终端应用程序内,将文件保存到sieve.S
并执行以下操作:
$ clang -m32 -g sieve.S -o sieve
$ ./sieve
2
3
5
<...>
我不知道如何在Xcode中随意构建它。只需创建一个空项目并将sieve.S
添加为源文件即可。
答案 1 :(得分:1)
执行以下命令:
$ gcc -o sieve sieve.s
$./sieve