Advertisement
asharma

swap

Feb 26th, 2019
1,208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. addi x2, x0, 1600 //initialize the stack to 1600, x2= stack pointer
  2. ecall x6, x0, 5 //read the input to x5
  3. add x10, x6, x0 //put the parameter in x10
  4. addi x2, x2, -8 //make room to store x5
  5. sd x6, 0(x2) // place x6 at the top
  6. jal x1, myswap
  7.  
  8. ld x6, 0(x2) //restore x5 rom the stack
  9. addi x2, x2, 8 //pop the stack
  10. ecall x0, x10, 2 //print the value returned from the functions
  11. ecall x0, x6, 2 //print the original value
  12. # ORG 96 //stops infinite recurring
  13. beq   x0, x0, exit  # if $t0 == $t1 then target
  14.  
  15.  
  16. myswap:
  17.     addi x2, x2, -8
  18.     sd x8, 0(x2)
  19.    
  20.     addi x5, x0, 255 // x5 is a 0000 ..011111111
  21.     and x6, x10, x5 // x6 is m=i&a, m has lest significant byte
  22.     srai x7, x10, 8 //x7 is I shifted to left by 8
  23.     and x7, x7, x5 //x7 is the second least sig. Byte of I
  24.  
  25.     srai x8, x10, 16 // shift I by 16 put it in x8
  26.     slli x8, x8, 8
  27.     or x8, x8, x6 //append the second least significant byte
  28.     slli x8, x8, 8
  29.     or x8, x8, x7
  30.     add x10, x0, x8
  31.     ld x8, 0(x2) // pop x8 frm the stack
  32.     addi x2, x2, 8
  33.     jalr x0, 0(x1)
  34.  
  35. exit:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement