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 answer: .float 0.0 .text .globl main main: add $t2,$zero,0 #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 add $t2,$zero,1 exit: move $a0,$t9 li $v0,2 syscall li $v0,10 syscall 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 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 jr $ra # jump to 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 jr $ra # jump to parent call