简体   繁体   中英

Bug in my MIPS assembly code

I have this code but is it not executing exactly how i want it to. It does the math correctly but prints out the wrong values at certain points.

For example if i enter 15 it should print out the following:

15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1

I expect this output because its been coded based of this

start : if (n == 1) stop ;
else if (n is even ) n = n/2;
else n = 3*n+1;
go to start ;

But it does

15 46 46 70 106 160 80 40 20 10 16 16 8 4 2

It also doesn't print out the 1 value, could someone please help? Also i suck at MIPS so please keep it to a basic level

As stated in your last question, the problem is a bug in your original C program -- namely that you have n being divided by 2 after it is outputted.

To fix this move

# n = n / 2
srl $t0 $t0 1

Above:

# print n
move $a0 $t0
addi $v0 $zero 1
syscall

I can verify that this gives you the results that you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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