我正在尝试在 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