Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 7.99 KB | None | 0 0
  1.  
  2. define(fd, w19)                                             // File descriptor
  3. define(n_read, x20)                                         // Number of bytes read
  4. define(buf_base, x21)                                       // Buf base
  5.  
  6. buf_size = 8                                                // Size of the buffer
  7. alloc = -(16 + buf_size) & -16                              // Allocation
  8. dealloc = -alloc                                            // Deallocation
  9. buf_s = 16                                                  // Not even sure tbh
  10. increment = 1                                               // Increment by 1
  11.  
  12. power           .req    d19                                 // Exponent/Power
  13. numerator       .req    d20                                 // Numerator (x^n)
  14. factor          .req    d21                                 // Factorial (n!)
  15. term            .req    d22                                 // term = numerator/denominator
  16. accumulator     .req    d23                                 // accumulator = 1 + term + term + ...
  17. incr            .req    d24                                 // increment = 1.0
  18.                
  19. constant:       .double 0r1.0e-10                           // Constant value
  20.  
  21. pn:     .string "input.bin"                                 // Name of the input file
  22. header: .string "Input:                     e^x:                   e^-x:\n"     // Header of output columns
  23. values: .string "%13.10f             %13.10f          %13.10f\n"                // Values of output columns
  24. error:  .string "Error :("                                  // Error :(
  25.  
  26.         .global main                                        // Make main a global function
  27.         .balign 4                                           // Word align
  28. main:                                                       // Allocate, open file, print table header
  29.     stp x29, x30, [sp, alloc]!                              // Allocate
  30.     mov x29, sp                                             // FP = SP
  31.  
  32.     adrp    x0, header                                      // Print header
  33.     add x0, x0, :lo12:header                                // ^
  34.     bl  printf                                              // ^
  35.  
  36.     // Open the file
  37.     mov w0, -100                                            // 1st arg: cwd
  38.     adrp    x1, pn                                          // 2nd arg: pathname
  39.     add x1, x1, :lo12:pn                                    // ^
  40.     mov w2, 0                                               // 3rd arg: read-only
  41.     mov w3, 0                                               // 4th arg: n/a
  42.     mov w8, 56                                              // Openat I/O Req
  43.     svc 0                                                   // Call system function
  44.     mov fd, w0                                              // Record the file descriptor
  45.  
  46.     // Error checking (if doesn't open properly)
  47.    cmp fd, 0                                               // Error check. If file open successfully:
  48.    b.ge    openok                                          // ... branch to openok
  49.  
  50.    // If there's an error:
  51.     adrp    x0, error                                       // Only happens if file doesnt open
  52.     add x0, x0, :lo12:error                                 // ^
  53.     bl  printf                                              // ^
  54.     mov w0, -1                                              // Return -1 to OS
  55.     b   done                                                // Exit the program
  56.  
  57. openok:                                                     // If it does open succesfully
  58.     add buf_base, x29, buf_s                                // buf_base now equals FP + buf_s
  59.  
  60. top:
  61.     // Read the file
  62.     mov w0, fd                                              // 1st arg: fd
  63.     mov x1, buf_base                                        // 2nd arg: buf
  64.     mov w2, buf_size                                        // 3rd arg: n
  65.     mov x8, 63                                              // Read I/O Req (code: 63)
  66.     svc 0                                                   // Call system function
  67.     mov n_read, x0                                          // # of bytes read
  68.  
  69.     // Error checking if read properly
  70.     cmp n_read, buf_size                                    // If nread =/= 8
  71.     b.ne    endFile                                         // ... exit
  72.  
  73.     // Calculate e^x:
  74.     ldr d0, [buf_base]                                      // d0 = x
  75.     bl exponent                                             // Calculate e^x
  76.     fmov    d1, d0                                          // 2nd arg: e^x
  77.  
  78.     // Calculate e^-x
  79.     ldr d0, [buf_base]                                      // d0 = x
  80.     fneg    d0, d0                                          // d0 = -x
  81.     bl  exponent                                            // Calculate e^-x
  82.     fmov    d2, d0                                          // 3rd arg: e^-x
  83.    
  84.     // Print out the values
  85.     adrp    x0, values                                      // Print values
  86.     add x0, x0, :lo12:values                                // ^
  87.     ldr d0, [buf_base]                                      // 1st arg: input
  88.     bl  printf                                              // Print the values
  89.  
  90.     b   top                                                 // Loop all 100 times
  91.  
  92. endFile:                                                    // Close the binary file
  93.     mov w0, fd                                              // 1st arg: fd
  94.     mov x8, 57                                              // Close I/O request
  95.     svc 0                                                   // Call system function
  96.     mov w0, 0                                               // Return 0 to OS
  97.  
  98. done:
  99.     ldp x29, x30, [sp], dealloc                             // Deallocate
  100.     ret                                                     // Return to calling code
  101.  
  102.             .balign 4                                       // Align by 4
  103.             .global exponent                                // Make exponent a global
  104. exponent:
  105.     stp x29, x30, [sp, -16]!                                // Allocate memory
  106.     mov x29, sp                                             // fp = sp
  107.  
  108.     adrp    x22, constant                                   // Load constant
  109.     add x22, x22, :lo12:constant                            // ^
  110.     ldr d3, [x22]                                           // d3 = constant
  111.    
  112.     fmov    power, 1.0                                      // power = 1.0
  113.     fmov    incr, 1.0                                       // incr = 1.0
  114.     fmov    accumulator, 1.0                                // accumulator = 1.0
  115.  
  116.     fmov    numerator, d0                                   // numerator = x
  117.     fmov    factor, power                                   // denominator = power
  118.     fdiv    term, numerator, factor                         // term = numerator / factor
  119.     fadd    accumulator, accumulator, term                  // accumulator += term
  120.  
  121. loop:                                                       // Loop for number
  122.     fmul    numerator, numerator, d0                        // numerator = numerator * x
  123.     fadd    power, power, incr                              // power += 1
  124.     fmul    factor, factor, power                           // factor = factor * power
  125.     fdiv    term, numerator, factor                         // term = numerator / factor
  126.     fadd    accumulator, accumulator, term                  // accumulator += term
  127.  
  128.     fabs    term, term                                      // term = |term|
  129.     fcmp    term, d3                                        // if term >= constant:
  130.     b.ge    loop                                            // ... do loop
  131.  
  132.     fmov    d0, accumulator                                 // d0 = accumulator
  133.     ldp x29, x30, [sp], 16                                  // Deallocate memory
  134.     ret                                                     // Return to caller
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement