MIPS中的Euler项目

时间:2014-06-06 17:25:11

标签: assembly mips

我的背景是python编程,我有兴趣学习汇编以了解最新情况"引擎盖"。我已经解决了python中前四十多个euler问题,我认为它们可以很好地锻炼我的装配技能。我还没有超越问题1(3和5的所有因素在1000以下的总和是多少?)。

这是我到目前为止所得到的。请注意,这是不完整的,只是程序的第一部分,应该只找到三个低于1000的因子

.data
sum:        .float 0.0
currentnum: .float 1000.0
num1:       .float 3.0
num2:       .float 5.0
zero:       .float 0.0
one:        .float 1.0

.text
main:
lw    $t0  currentnum       # move all variables into temporary registers t0 - t5
lw    $t1  num1
lw    $t2  num2
lw    $t3  zero
lw    $t4  one
lw    $t5  sum       

factors_of_3:                # finds all factors of 3, adds to sum
sub    $t0, $t0 $t4          #begin by subtracting 1 from currentnum, 1st val of currentnum = 999
beqz   $t0, end              # escape loop if currentnum == 0
div    $t0, $t1              # divides the current number by 3, places answer in lo    remainder in hi
mfhi   $t6                   # moves the remainder to temporary register t6             
bne    $t6, $t3 factors_of_3  # if the remainder =/= 0, jump to factors_of_3
add    $t5, $t5, $t0          #otherwise, sum = sum + currentnum
j factors_of_3
    syscall 
end:

由于某种原因,每次运行时,sum所在的寄存器保持为0,而所有其他寄存器(我定义的寄存器,t0-t5)都显示无意义的大十六进制数。仅供参考,在python shell中运行行sum(x for x in range (1000) if (x % 3 ==0))会给出166833,这应该是正确的答案。

0 个答案:

没有答案