Python 如何在 MIPS 中正确使用 mod 运算符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21695333/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:25:03  来源:igfitidea点击:

How do I correctly use the mod operator in MIPS?

pythonassemblymipsmodulo

提问by kcmallard

In MIPS, I am confused on how to get the mod to work. Below is the code I have come up with thus far. I may have more errors besides the mod, but I feel those errors are a result of the mod misunderstanding. All I'm trying to do is to get the working code (python) here:

在 MIPS 中,我对如何让 mod 工作感到困惑。下面是我到目前为止想出的代码。除了mod之外我可能还有更多错误,但我觉得这些错误是mod误解的结果。我要做的就是在此处获取工作代码(python):

i = 1
k = 0
while i < 9:
if i % 2 != 0:
    k = k + i
i += 1
print(k)

to be correctly translated into MIPS. This is my first shot at assembly, so there may be more than mod errors that are tripping me up in the code below:

正确转换为 MIPS。这是我第一次尝试组装,所以在下面的代码中可能有比 mod 错误更多的错误:

# Takes the odd integers from 1 to 9, adds them,
#  and spits out the result.
# main/driver starts here
    .globl main

main:

#data segment
.data

Li:     .byte 0x01  # i = 1
Lj:     .byte 0x09  # j = 9
Lk:     .byte 0x00  # k = 0
Ltwo:   .byte 0x02  # 2 for mod usage

# text segment
.text

lb $t0, Li      # temp reg for i
lb $t1, Lj      # j
lb $t2, Lk      # k
lb $t3, Ltwo        # 2

L1:  beq $t0, $t1, L2   # while i < 9, compute
     div $t0, $t3       # i mod 2
     mfhi $t6        # temp for the mod
     beq $t6, 0, Lmod   # if mod == 0, jump over to L1
     add $t2, $t2, $t0  # k = k + i
Lmod:    add $t0, $t0, 1        # i++
     j L1           # repeat the while loop


L2: li $v0, 1       # system call code to print integer
lb $a0, Lk      # address of int to print
syscall

li $v0, 10
syscall

采纳答案by Michael Foukarakis

You are viewing SPIM registers in hex. Hexadecimal 10 is decimal 16.

您正在查看十六进制的 SPIM 寄存器。十六进制 10 是十进制 16。

回答by kcmallard

After working out the kinks, the below code works like a charm. To correctly use the mod operator in MIPS, one must utilize HI and LO. I needed i % 2 == 0 for the statement, so mfhi came in handy. Reference below code for working results:

解决这些问题后,下面的代码就像一个魅力。要在 MIPS 中正确使用 mod 运算符,必​​须使用 HI 和 LO。我需要 i % 2 == 0 来声明,所以 mfhi 派上用场了。工作结果参考以下代码:

# Takes the odd integers from 1 to 9, adds them,
#  and spits out the result.
# main/driver starts here
    .globl main

main:

#data segment
.data

Li:     .byte 0x01  # i = 1
Lj:     .byte 0x0A  # j = 10
Lk:     .byte 0x00  # k = 0
Ltwo:   .byte 0x02  # 2 for mod usage


# text segment
.text

lb $t0, Li      # temp reg for i
lb $t1, Lj      # j
lb $t2, Lk      # k
lb $t3, Ltwo        # 2

L1:     beq $t0, $t1, L2    # while i < 9, compute
        div $t0, $t3        # i mod 2
        mfhi $t6           # temp for the mod
        beq $t6, 0, Lmod    # if mod == 0, jump over to Lmod and increment
        add $t2, $t2, $t0   # k = k + i
Lmod:   add $t0, $t0, 1     # i++
        j L1               # repeat the while loop


L2:     li $v0, 1       # system call code to print integer
        move $a0, $t2       # move integer to be printed into $a0
        syscall

        li $v0, 10     # close the program
        syscall