java - MIPS programming basic for-loop -


i writing mips program factorial. have written factorial example in java, , have have mips program below java code. have majority of mips written out, confused why not processing correctly. tips appreciated.

    java code iteratve factorial algorithm: import java.util.scanner; public class factormachine {     public static void main(string[] args) {         int input;         scanner in = new scanner(system.in);         system.out.println("enter integer factored: ");         input = in.nextint();         {             int x, factorial = 1;             (x = input; x > 1; x--)                 factorial *= x;             system.out.println("factorial #" + input + " " + factorial);         }     } } 

mips code:

    .data p1: .asciiz "enter integer factored: " ans1:   .asciiz "factorial # " ans2:   .asciiz " " ans3:   .asciiz "\n\n"      .text     .globl main  main:   li $v0, 4     la $a0, p1     syscall      li $v0, 5     syscall     move $t0, $v0   #this input      li $t1, 1       #initilize factorial     move $t2, $t0       #initilize x   loop:     blt $t2, 1, done     sub $t2, $t2, 1     mul $t3, $t1, $t0     j loop    done:     li $v0, 4     la $a0, ans1     syscall      li $v0, 1     move $a0, $t3     syscall      jr $ra 

let's @ java code (i changed x *= y x = x * y clarity):

    (x = input; x > 1; x--)         factorial = factorial * x; 

now let's @ assembly code:

move $t2, $t0       #initilize x loop:     blt $t2, 1, done     sub $t2, $t2, 1     mul $t3, $t1, $t0     j loop 

and correspond in java:

for (x = input; x >= 1; ) {     x--;     temp = factorial * input; } 

notice differences? you're:

  1. decrementing x before multiplication, instead of after in original java code.
  2. multiplying input ($t0) instead of x ($t2).
  3. storing result of multiplication in different register instead of writing factorial ($t1). you'll product of 1 * input, of course equals input.

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -