Advertisement
Guest User

Instructional Assembly Demo

a guest
Nov 8th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. contract InstructionalAssemblyDemo {
  2.     function assemblyLoop() public view returns (uint result) {
  3.         assembly {
  4.             // i = 0
  5.             let i := 0
  6.            
  7.             loop:
  8.                 // result += i
  9.                 result := add(result, i)
  10.                
  11.                 // i++
  12.                 i := add(i, 1)
  13.                
  14.                 // if (i < 10) loop again
  15.                 jumpi(loop, lt(i, 10))
  16.         }
  17.     }
  18.    
  19.     function instrAssemblyLoop() public view returns (uint result) {
  20.         assembly {
  21.             // i = 0
  22.             0
  23.             loop:
  24.                 // result += i
  25.                     // make a copy of result
  26.                     dup2
  27.                     // make a copy of i
  28.                     dup2
  29.                     // add them together
  30.                     add
  31.                     // assign the result of op to result
  32.                     swap2
  33.                     // remove the leftovers
  34.                     pop
  35.                    
  36.                 // i++
  37.                     1
  38.                     add
  39.                
  40.                 // if (i < 10) loop again
  41.                     // push constant 10 to the stack
  42.                     10
  43.                     // push i to the top of stack
  44.                     dup2
  45.                     // i < 10
  46.                     lt
  47.                     // push the tag to top of the stack
  48.                     loop
  49.                     // jumpi(loop, lt(i, 10))
  50.                     jumpi
  51.                
  52.             pop
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement