# help with mips code please

Page 1 of 1

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

### #1 YazanAyman

Reputation: 0
• 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 !

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

.text
.globl main
main:

#enters value x
li \$v0,4
la \$a0 , mess1
syscall

li \$v0,5
syscall
move \$t1,\$v0

#enters value n

li \$v0,4
la \$a0 , mes2
syscall

li \$v0,5
syscall

move \$t3,\$v0

#--------

while:
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
exit:
move \$a0,\$t9
li \$v0,2
syscall
li \$v0,10
syscall

sw \$ra,0(\$sp)

bne \$a1,\$zero, Else

jr \$ra
Else:    bne \$a1,1, Else1

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)
jr \$ra
jal Power
lw \$ra, 0(\$sp)
mul \$v0,\$v0,\$a0
jr \$ra

factorial:

# 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

.text

#return 1;

returnOne:

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

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

```

Is This A Good Question/Topic? 0

## Replies To: help with mips code please

### #2 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,480
• 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.

### #3 Salem_c

• void main'ers are DOOMED

Reputation: 2143
• Posts: 4,207
• 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.