2 Replies - 2936 Views - Last Post: 30 July 2017 - 06:12 AM

#1 YazanAyman  Icon User is offline

  • New D.I.C Head

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

help with mips code please

Posted 29 July 2017 - 01:41 PM

exp(x) = 1 + x + x2/2 + x3/3! + x4/4! + ...
why it always print zero !

mess1: .asciiz "Enter the base nom X  "
mes2 : .asciiz  "enter the second nom N "
mes3 : .asciiz  "answer is :   "
  Xnom:  .word 0
  Nnom:  .word 0
   answer:  .float 0.0

.globl main
add $t2,$zero,0

	#enters value x
li $v0,4 
la $a0 , mess1
	li $v0,5
move $t1,$v0
#enters value n

li $v0,4
la $a0 , mes2

	li $v0,5
	move $t3,$v0


bgt $t2,$t3,exit

	move $a0,$t1    #$a1=x    
	move $a1,$t3   #$a2=n
jal Power
 move $s0,$t3
move $a2,$t3   #$a2=n
jal factorial
move $s1,$v0

	div $s3,$s0,$s1
	move $t9,$s3
 add $t2,$zero,1
	move $a0,$t9
    li $v0,2
    li $v0,10


Power: addi $sp,$sp,-4
        sw $ra,0($sp)
        bne $a1,$zero, Else
        addi $v0,$zero,1
        addi $sp, $sp,4
        jr $ra
    Else:    bne $a1,1, Else1
        add $v0,$zero, $a0
        addi $sp,$sp,4
        jr $ra
    Else1:    move $t1,$a1
        andi $t0, $t1,1        #check if N is even or odd
        bne $t0,$zero, Else2    #odd goto Else2
        srl $a1, $a1,1        #even N/2
        jal Power        #recursive
        mul $v0,$v0,$v0        # Power(x,n/2)*Power(x,n/2)
        lw $ra, 0($sp)
        addi $sp,$sp,4
        jr $ra
    Else2:    addi $a1,$a1,-1        #odd X*power(x,n-1)
        jal Power
        lw $ra, 0($sp)
                addi $sp,$sp,4
        mul $v0,$v0,$a0
        jr $ra


  # base  case - still in parent's stack segment

  lw      $t0, 0($sp)       # load input from top of stack into register $t0

  #if (x == 0)

  beq     $t0, 0, returnOne # if $t0 is equal to 0, branch to returnOne

  addi    $t0, $t0, -1      # subtract 1 from $t0 if not equal to 0

  # recursive case - move to this call's stack segment

  addi    $sp, $sp, -12     # move stack pointer up 3 words

  sw      $t0, 0($sp)       # store current working number into the top of the stack segment

  sw      $ra, 8($sp)       # store counter at bottom of stack segment

  jal     factorial         # recursive call

  # if we get here, then we have the child return value in 4($sp)

  lw      $ra, 8($sp)       # load this call's $ra again(we just got back from a jump)

  lw      $t1, 4($sp)       # load child's return value into $t1

  lw      $t2, 12($sp)      # load parent's start value into $t2

# return x * factorial(x-1); (not the return statement, but the multiplication)

  mul     $t3, $t1, $t2     # multiply child's return value by parent's working value, store in $t3.

  sw      $t3, 16($sp)      # take result(in $t3), store in parent's return value.

  addi    $sp, $sp, 12      # move stackpointer back down for the parent call

  jr      $ra               # jump to parent call


#return 1;


  li      $t0, 1            # load 1 into register $t0

  sw      $t0, 4($sp)       # store 1 into the parent's return value register

  jr      $ra               # jump to parent call

Is This A Good Question/Topic? 0
  • +

Replies To: help with mips code please

#2 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6448
  • View blog
  • Posts: 26,087
  • Joined: 12-December 12

Re: help with mips code please

Posted 29 July 2017 - 11:45 PM

Question moved to Assembly forum, please post there in future.

I don't do MIPS (or assembly) but debugging steps are:

print out intermediate results, not just the final (zero) value;
if a debugger is available then step through the code exploring the values of variables/registers.
Was This Post Helpful? 0
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

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

Re: help with mips code please

Posted 30 July 2017 - 06:12 AM

So how much testing did you do before throwing in a whole mass of code only to find it didn't work?

  move $a0,$0   # whatever load with literal value is
  move $a1,$0 
  jal Power
  move $s0,$t3

Anything to the power 0 is 1
Zero to the power of anything but 0 is zero
Anything (except 0) to the power of 1 is itself.

When you're happy that Power works, then do something similar for Factorial.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1