在 MIPS 中,我对如何让 mod 工作感到困惑。下面是我到目前为止想出的代码。除了mod之外我可能还有更多错误,但我觉得这些错误是mod误解的结果。我要做的就是在此处获取工作代码(python):
i = 1
k = 0
while i < 9:
if i % 2 != 0:
k = k + i
i += 1
print(k)
Run Code Online (Sandbox Code Playgroud)
正确转换为 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 …Run Code Online (Sandbox Code Playgroud) 问题发生在Go代码的第17行.下面是python和Go中的程序,所以你可以看到我正在尝试做什么.Python工作,我的Go尝试都失败了.已经连续阅读了golang.org,谷歌也没有看到任何内容.
def my_filter(x):
if x % 5 == 0:
return True
return False
#Function which returns a list of those numbers which satisfy the filter
def my_finc(Z, my_filter):
a = []
for x in Z:
if my_filter(x) == True:
a.append(x)
return a
print(my_finc([10, 4, 5, 17, 25, 57, 335], my_filter))
Run Code Online (Sandbox Code Playgroud)
现在,Go版本我遇到了麻烦:
package main
import "fmt"
func Filter(a []int) bool {
var z bool
for i := 0; i < len(a); i++ {
if a[i]%5 == 0 {
z = true …Run Code Online (Sandbox Code Playgroud)