assembly - MIPS 汇编作业。好像找不到bug

标签 assembly mips

好的,问题如下:使用 MARS,开发一个程序:

  • 提示用户输入一个整数,
  • 从键盘读取整数(比如 n),
  • 计算并打印出1的奇数和和偶数之和 到 n.

这是我的尝试,请不要解决我的家庭作业。我想从我的错误中吸取教训。我的问题是,我哪里出错了,寄存器似乎转到了 -MAXINT 运行它,你会看到:(

    # Assignment 3, part 1
    # NAME  
    #The Formulas used in this assignments are visible in a text document 
    #attached in the zip. and their mathematical proofs. 
        # Odd or Even Checker by parity bit in binary format.
        # by anding it with the number N
        # if it is odd, the anding will result in 00000001
        # if it is even the ANDing will result in all 0
        # we can check if the result is ==0 or not
        # then use mathematical formulas to derive the sums
        # using the least number of instructions    
        # Without using the not-yet-learnt instructions.

-------------------------------------------- --------------------------

.data       #Data Declaration Section
    msg1: .asciiz "Hello, Enter an Integer:\n->"
    msgodd: .asciiz "Odd Sum is :\n"
    msgeven: .asciiz "Even Sum is :\n"  
.text       
        #START

    la $a0, msg1    #Load the address of label msg1
    li $v0,4    #Load int 4 to v0, and address the system call. 
    syscall

   #Request a Syscall for integer input
    li $v0,5    #Load int 5 <int inp>
    syscall 

    move $a0,$v0    #copy the int entered
    parity:
    move $t0,$a0    #will be used to test for odd or even
    move $a3,$a0    #will be used later
    andi $t0,00000001 # this is the parity bit i talked about above.
    beq $t0,$zero,even_n
    #s1 hold odd sum, s2 hold even sum, t1 is temp for odd, t2 is temp for even
    li $s5,1
    odd_n:
move $t1,$a3
move $t2,$a3
addi $t2,$t2,-1

ol:
    add $s1,$s1,$t1
    addi $t1,$t1,-2
    bne $t1,$s5,ol

el:
    add $s2,$s2,$t2
    addi $t2,$t2,-2
    bne $t2,$zero,el

    even_n:
move $t1,$a3
move $t2,$a3
addi $t1,$t1,-1

eol:
    add $s1,$s1,$t1
    addi $t1,$t1,-2
    bne $t1,$s5,eol

eel:
    add $s2,$s2,$t2
    addi $t2,$t2,-2
    bne $t2,$zero,eel

    print:
    li $v0,4
    la $a0,msgodd
    syscall
    li $v0,1
    move $a0,$s1
    syscall

    la $a0,msgeven
    syscall
    li $v0,1
    move $a0,$s2
    syscall

    exit:
    li $v0,10
    syscall

最佳答案

您的代码有一些问题,但应用这些更改后应该没问题。

  1. 您在分支后初始化 $s5,当您的起始编号为偶数时,$s5 仍未初始化。

  2. 当起始数字为奇数并且您的程序处理完它时,控制流会无条件地转到偶数情况。 odd_n 完成计算后,您需要跳转到程序的输出部分。

  3. 在进行syscall 显示msgeven 之前,您需要将4 加载到$v0 (当你这样做的时候,考虑修复两条消息中换行符的位置)。

关于assembly - MIPS 汇编作业。好像找不到bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9775697/

相关文章:

C 使用汇编 : operand type mismatch for push

assembly - 对多个位进行高效的 assembly 检查

linux - libc_p.so 的源代码在哪里?

gcc - “small-data section exceeds 64KB..” “additional relocation overflows omitted from the output 1>collect2.exe: error: ld returned 1 exit status”

file - 汇编文件 : Difference between . a .s .asm

c++ - C/С++。为什么 volatile 上的简单整数加法可以转换为 gcc 和 clang 上的不同 asm 指令?

c - 汇编函数 FPU with c

gcc - 对标上交所指令

assembly - itoa 负数?

gcc - C/C++ 到 MIPS 汇编