简体   繁体   English

用于简单 for 循环的 MIPS 汇编

[英]MIPS assembly for a simple for loop

I need to translate this C code to MIPS assembly.我需要将此 C 代码转换为 MIPS 程序集。 Here is the C code:这是 C 代码:

int tmp = 0; 
for (int  j = 0; j < 15; ++j) 
     tmp = tmp * 2 + 3

This is my MIPS assembly code.这是我的 MIPS 汇编代码。 Is it a correct translation?是正确的翻译吗? If you see any mistakes I would really like to know.如果您看到任何错误,我真的很想知道。

# tmp = $v0
# j = $t0

.globl main

 main:
    li $v0,0

loop:
    bgt $t0,15,exit
    addi $t0,$t0,1
    mul $t1,$v0,2
    add $v0,$t1, 3
    j loop  

exit:

Your loop goes from 0 to 14, so your bgt instruction should be: bgt $t0,14,exit I think.你的循环从 0 到 14,所以你的 bgt 指令应该是: bgt $t0,14,exit我想。

. .

您不要在循环之前将 j ($t0) 设置为零。

.data
mensage: asciiz "Text Test"
newline: asciiz "\n"
.text

# tmp = $v0
# j = $t0
 
main:
    li $t0,0
    li $t1,0
    li $t3,0
loop:
    bgt $t0,15,exit
    addi $t0,$t0,1
    j loop
    mul $t1,$t1,2
    add $t3,$t1,3  
exit:
    li $v10,0
    syscall

I also don't know what MIPS simulator you're running, but I know some of them don't constants and they demand you assign those to registers.我也不知道你在运行什么 MIPS 模拟器,但我知道其中一些不是常量,他们要求你将它们分配给寄存器。 So like bgt Rsrc1, Src2, label, normally if you put an integer in src2 the computer will translate that but I know for some you'll get an error doing add $v0, $t1, 3 as it won't translate add into addi.所以像 bgt Rsrc1, Src2, label 一样,通常如果你在 src2 中放入一个整数,计算机会翻译它,但我知道对于一些你会得到一个错误 add $v0, $t1, 3 因为它不会翻译 add阿迪Same with mul.与 mul 相同。 I know my SPIM simulator doesn't allow it.我知道我的 SPIM 模拟器不允许这样做。

    add $vo, $vo, $zero
    add $t0, $t0, $zero
LOOP:
    slti $t1, $t0, 15
    beq $t1, $zero, EXIT
    addi $t0, $t0, 1
    addi $t2, $zero, 2
    mul $t3, $v0, $t2
    addi $v0, $t3, 3
    j    LOOP
EXIT:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM