3 Replies - 486 Views - Last Post: 29 July 2017 - 09:50 AM Rate Topic: -----

#1 YazanAyman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-July 17

can anyone please help to convert this java code to mips assembly!

Posted 29 July 2017 - 04:23 AM

i tried and i wrote this but it keeps giving me -4 ,

.data
pi: .word 0

.text

main:
add $t5,$zero,0
add $t6,$zero,1
add $t0,$zero,1
 add $t3,$zero,1
 
 while:
 
 	bgt $t0,1000,exit
 	
 	div $t8,$t0,2
 	mfhi $s1
 	bne $s1,0,else
 	div $t7,$t3,$t6
 	add $t5,$t5,$t7
 	 
 	 else: 
 div $t7,$t3,$t6
 	sub $t5,$t5,$t7
  
  
  add $t6,$t6,2
  	add $t0,$t0,1
  	j while
 
 exit: mul $t5,$t5,4 
 
 
	li $v0,1
	addi $a0,$t5,0
 syscall
 
 li $v0,10
  syscall




and here's the java code
 int count = 999999999;
      double pi = 0;
      double denominator = 1;
 
      for (int x = 0; x < count; x++) {
 
         if (x % 2 == 0) {
            pi = pi + (1 / denominator);
         } else {
            pi = pi - (1 / denominator);
         }
         denominator = denominator + 2;
      }
      pi = pi * 4;
      System.out.println(pi);



the out put should be in double i tried to do this but im bad in mips

Is This A Good Question/Topic? 0
  • +

Replies To: can anyone please help to convert this java code to mips assembly!

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2130
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: can anyone please help to convert this java code to mips assembly!

Posted 29 July 2017 - 05:42 AM

Your if / else is broken.
if:  # not necessary, but shows what you're doing
 	div $t8,$t0,2
 	mfhi $s1
 	bne $s1,0,else
 	div $t7,$t3,$t6
 	add $t5,$t5,$t7
 	b endif  # Your code just fell through into the else part! 
else: 
 div $t7,$t3,$t6
 	sub $t5,$t5,$t7
endif:



Also, you're only using integer registers, so integer division of 1/something is going to give you a whole bunch of zeros.

See https://www.doc.ic.a...pim/node20.html
Was This Post Helpful? 1
  • +
  • -

#3 YazanAyman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-July 17

Re: can anyone please help to convert this java code to mips assembly!

Posted 29 July 2017 - 07:58 AM

.data
pi: .double 0.0
cons:	.double 1.000
cons2:	.double 1.000
cons3:	.double 2.00
cons4:	.double 4.00
zeroc:	.double 0.0
.text

main:
	lwc1 $f1,pi
	lwc1 $f3,cons
	lwc1 $f2,cons2
	lwc1 $f4,cons3
	lwc1 $f5,cons4
	lwc1 $f6,zeroc
add $t0,$zero,1
 
 while:
 
bgt $t0,1000,exit

    div $t8,$t0,2

    mfhi $s1

    bne $s1,0,else

    div.d $f10,$f3,$f2

    add.d $f1,$f1,$f10

    

else:

 div.d $f10,$f3,$f2

    sub.d $f1,$f1,$f10




	
	add.d $f2,$f2,$f4
	add $t0,$t0,1


  	j while
 
 exit: mul.d $f1,$f1,$f5

	
  add.d $f12,$f1,$f6
	li $v0,3
	
 syscall
 
 




i did this but it keeps giving me this message " Odd FP double register number "
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2130
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: can anyone please help to convert this java code to mips assembly!

Posted 29 July 2017 - 09:50 AM

1. There is no reason for the stupidly large font for your text. In fact, it makes it worse to read than if you'd just left it alone.

2. The clue is in the error message - stop using odd numbered registers.

Especially when the very first paragraph of the link I gave you also says this.

Quote

Because these registers are only 32-bits wide, two of them are required to hold doubles. To simplify matters, floating point operations only use even-numbered registers--including instructions that operate on single floats.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1