Como faço para usar corretamente o operador de mod em MIPS?
Em MIPS, estou confuso sobre como fazer o mod funcionar. Abaixo está o código que eu inventei até agora. Eu posso ter mais erros além do mod, mas eu sinto que esses erros são um resultado do mal-entendido mod. Tudo o que estou tentando fazer é obter o código de trabalho (python) aqui:
i = 1
k = 0
while i < 9:
if i % 2 != 0:
k = k + i
i += 1
print(k)
para ser correctamente traduzido em MIPS. Esta é a minha primeira tentativa de montagem, por isso pode haver mais do que erros de mod que estão me enganando no código abaixo:
# 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
1
2 answers
Estás a ver registos de SPIM em hex. Hexadecimal 10 é decimal 16.
3
Author: Michael Foukarakis, 2014-02-11 08:37:54
Depois de resolver os problemas, o código abaixo funciona como um encanto. Para usar corretamente o operador mod em MIPS, deve-se utilizar HI e LO. Eu precisava de i % 2 = = 0 para a declaração, então mfhi veio a calhar. Referência abaixo do código para os resultados de trabalho:
# 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
1
Author: kcmallard, 2014-02-11 09:05:02