Assembly factorial

Page 1 of 1

2 Replies - 7206 Views - Last Post: 26 April 2010 - 05:25 AM

#1 szacik

Reputation: 0
• Posts: 5
• Joined: 25-April 10

Assembly factorial

Posted 25 April 2010 - 04:15 PM

Hi everyone, I have problem with this factorial. I am trying to do it with for loop, but this code multiplying the two last numbers. For 3! the result is correct but for 4! and more result is not correct. Do you have any ideas where is bug? Thanks a lot.

```.data   # Data declaration section
promptInt: .asciiz "Write number of factorial"
.text

main:           # Start of code section
#display promptInt
li \$v0, 4
la \$a0, promptInt
syscall

#giving number
li \$v0, 5
syscall
move \$s1, \$v0

#for loop ------------------------
li \$s0, 0               #int i=0
for_loop:               #loop starting

mul \$t1, \$t0, \$s0       #t1=t0*s0

blt \$s0, \$s1, for_loop  #s0>s1
#----------------------
move \$s2, \$t1

#show result
li \$v0, 1
move \$a0, \$s2
syscall

#END OF PROGRAM
```

Is This A Good Question/Topic? 0

Replies To: Assembly factorial

#2 wildgoose

• D.I.C Regular

Reputation: 67
• Posts: 468
• Joined: 29-June 09

Re: Assembly factorial

Posted 26 April 2010 - 01:56 AM

Have you traced your code flow?

You increment your value, you do a multiply between two adjacent numbers, but throw it away.

Shouldn't it be
```5! = 2 * 3 * 4 * 5

So tot = 1
s = 2

tot = tot * s
s++
( s < 5) loop

```

...Not...
0*1
1*2
2*3
3*4
4*5

#3 szacik

Reputation: 0
• Posts: 5
• Joined: 25-April 10

Re: Assembly factorial

Posted 26 April 2010 - 05:25 AM

Thanks, I must focus on syntax of Assembly.