本文介绍了怎样正确地使用在MIPS mod运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MIPS,我如何获得国防部工作的困惑。下面是code我想出迄今。我可以有更多的错误,除了国防部,但我觉得这些错误是MOD误解的结果。所有我想要做的是让工作code(蟒蛇)位置:

  I = 1
K = 0
而I< 9:
如果我%2 = 0!
    K = K +我
I + = 1
打印(K)

要正确地翻译成MIPS。这是我在组装第一炮,所以有可能会超过所绊倒我到下面的code MOD错误:

 #注意到从1奇数9,增加了他们,
#和吐出的结果。
#主/驾驶员从这里开始
    .globl主主要:#DATA段
。数据李:.BYTE 0×01#I = 1
LJ:.BYTE×09#J = 9
路:.BYTE为0x00#K = 0
Ltwo:.BYTE 0X02#2对mod用法#文本段
。文本磅$ T0,李#临时章第我
磅$ T1,LJ#Ĵ
磅$ T2,路#ķ
磅$ T3,Ltwo#2L1:BEQ $ T0,T1 $,L2#而I< 9,计算
     DIV $ T0,T3 $#我MOD 2
     MFHI $#T6的温度国防部
     BEQ $ T6,0,#LMOD如果MOD ​​== 0,跳转到L1
     加$ T2,T2 $,$#T0 K = K +我
LMOD:加$ T0,T0 $ 1#我++
     ĴL1#重复while循环
L2:李$ V0,1#系统调用code打印整数
磅$ A0,INT打印的路#地址
系统调用李$ V0,10
系统调用


解决方案

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

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:

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:

# 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
解决方案

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

这篇关于怎样正确地使用在MIPS mod运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:39