将Python代码翻译为汇编代码

0 投票
1 回答
2234 浏览
提问于 2025-04-30 00:49

在我上面的例子中,我用了80作为n的值。我的输出结果是n = 91,count = 42。这被称为麦卡锡91(McCarthy 91)。不过我现在想把这段代码用在汇编语言(MIPS)中。有没有人能给我一些建议,告诉我该怎么做?我之前从来没有在MIPS中写过递归类型的程序。下面我提供了我工作的Python代码,还有我正在编写的汇编代码,汇编代码下面有注释。任何关于汇编代码的建议或帮助我都会非常感激。

Python代码:

   def mcc91(n):
        global count
        count +=1
        if n > 100:
            return n -10
        else:
            return mcc91 (mcc91(n + 11))


    def main():
        print mcc91(80)
        print count

    main()

MIPS程序

.data
    p1:
        .asciiz "What is integer n? "
    ans1:
        .asciiz "\nn is "
    ans2:
        .asciiz "\nCount is "


        .text
        .globl main

    main:
        li $v0, 4       # system call code for print_str
        la $a0, p1      # address of string to print
        syscall         # print the first prompt

        li $v0, 5       # system call code for read_int
        syscall         # read the first integer
        move    $t1, $v0    # and store it 'till later

        li $t9, 0       #tracks count

    loop1:
        bgt $t1, 100, target1
        add $t1, $t1, 11    #adds 11 when under 100
        add $t9, $t9, 1     #add 1 to count
        Loop1(loop1     #cluless how to do this

    target1:
        sub $t1, $t1, 10    #subtracts 10 when over 100
        add $t9, $t9, 1     #add 1 to count
        #j done         #cluless here also(how many stacks in is it? If 0, jump to done)

    done:
    #THIS PRINTS N
        li $v0, 4       # system call code for print_str
        la $a0, ans1        #address of string to print
        syscall         # print the answer identifying string

        li $v0, 1       # system call code for print_int
        move $a0, xxxxx     # integer to print (sum)
        syscall         # print it

    #THIS PRINTS Count
        li $v0, 4       # system call code for print_str
        la $a0, ans2        # address of string to print
        syscall         # print the answer identifying string

        li $v0, 1       # system call code for print_int
        move $a0, xxxxx     # integer to print (difference)
        syscall         # print it


        jr $ra          # return from this program to the system
暂无标签

1 个回答

0

把Python代码转换成C语言,然后用你喜欢的C语言编译器把它进一步转换成汇编语言。

撰写回答