Advertisement
asharma

notReallyFact

Feb 26th, 2019
1,233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. constants:  addi x31, x0, 4                 # conditional immediate that if i >= 4 go to true part
  2.             addi x30, x0, 2                 # the 2' in 2'*rec_func(i-2)+1
  3.  
  4. addi x2, x0, 1600               // initialize the stack to 1600, x2= stackpointer
  5. ecall x5, x0, 5                 // read the input to x5, the 5 signalling its an integer input
  6. jal x1, rec_func                // main thread jump point (head to rec_func block)
  7. ecall x0, x10, 2                // print the result
  8. beq x0, x0, exit
  9.  
  10. rec_func:
  11.     addi x2, x2, -8                 // make room in stack
  12.     sd x1, 0(x2)                    // store pointer and result in stack
  13.     bge x5, x31, true               // if i > 3, then go to true branch
  14.     ld x1, 0(x2)
  15.     addi x10, x0, 1                 // if i <= 3, then return 1
  16.     addi x2, x2, 8                  // reset stack point
  17.     jalr x0, 0(x1)                  // link back to register x1 (main jump point)
  18.  
  19. true:
  20.     addi x5, x5, -2                 // compute i-2
  21.     jal x1, rec_func                // call recursive func for i-2
  22.     ld x1, 0(x2)                    // load the return address
  23.     addi x2, x2, 8                  // reset stack point
  24.     mul x10, x10, x30               // multiply by 2
  25.     addi x10, x10, 1                // add 1
  26.     jalr x0, 0(x1)                  // return
  27.  
  28. exit:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement