Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 4.79 KB | None | 0 0
  1.  define(fd_r, w19)
  2.         define(nread_r, x20)
  3.         define(buf_base_r, x21)
  4.  
  5.         buf_size = 8
  6.         alloc = -(16 + buf_size) & -16
  7.         dealloc = -alloc
  8.         buf_s = 16
  9.         AT_FDCWD = -100
  10.  
  11. pn:     .string "input.bin"                                                     // Name of file
  12. fmt1:   .string "|     Input             |     e^x               |     e^-x              |\n"           // header
  13. fmt2:   .string "|     %13.10f     |     %13.10f     |     %13.10f     |\n"     // Values
  14. fmt3:   .string "Error opening file %s \n Aborting \n"
  15. value:  .double 0r1.0e-10
  16.  
  17.         .balign 4
  18.         .global main
  19.  
  20.         counter         .req d19        // +1
  21.         accum           .req d20        // Accumulator
  22.         factorial       .req d21        // Factorial
  23.         power           .req d22        // power
  24.         quotient        .req d23        // Quotient
  25.         numerator       .req d24        // Numerator
  26.  
  27.  
  28.  
  29. main:   stp     x29, x30, [sp, alloc]!
  30.         mov     x29, sp
  31.  
  32.  
  33.         // Printing formatted header
  34.         adrp    x0, fmt1                        // Header
  35.         add     x0, x0, :lo12:fmt1              // print header
  36.         bl      printf
  37.  
  38.  
  39.         // Open file
  40.         mov     w0, AT_FDCWD                    // arg1 (cwd)
  41.         adrp    x1, pn                          // arg2 (pathname)
  42.         add     x1, x1, :lo12:pn
  43.         mov     w2, 0                           // arg3 (read only)
  44.         mov     w3, 0                           // arg4 (not used)
  45.         mov     x8, 56                          // openat I/O
  46.         svc     0                               // Call sys func
  47.         mov     fd_r, w0                        // Record file descriptor
  48.  
  49.  
  50.         // Error check for openat
  51.         cmp     fd_r, 0
  52.         b.ge    openok
  53.  
  54.         adrp    x0, fmt3                        // Error
  55.         add     x0, x0, :lo12:fmt3
  56.         bl      printf
  57.         mov     w0, -1
  58.         b       done
  59.  
  60.  
  61. openok: add     buf_base_r, x29, buf_size       // calculate buf base
  62.  
  63. top:    // read ints from binary file
  64.         mov     w0, fd_r                        // 1st arg (fd)
  65.         mov     x1, buf_base_r                  // 2nd arg (buf)
  66.         mov     w2, buf_size                    // 3rd arg (n)
  67.         mov     x8, 63                          // Read I/O
  68.         svc     0                               // Call sys func
  69.         mov     nread_r, x0                     // # of bytes
  70.  
  71.         // Error check
  72.         cmp     nread_r, buf_size               // nread != 8
  73.         b.ne    end                             // read failed - exit loop
  74.  
  75.         bl      exp
  76.  
  77.         // e^x / e^-x
  78. exp:    ldr     d0, [buf_base_r]                // d is x
  79.         bl      calc                            // go to calculations
  80.         fmov    d1, d0                          // Mov arg1
  81.         ldr     d0, [buf_base_r]                // d is x
  82.         fneg    d0, d0                          // d is -x
  83.         bl      calc
  84.         fmov    d2, d0                          // moves to arg2
  85.  
  86.         // Print
  87.         adrp    x0, fmt2                        // Printing the values %13.10f
  88.         add     x0, x0, :lo12:fmt2              // print
  89.         ldr     d0, [buf_base_r]                // arg
  90.         bl      printf
  91.  
  92.         b       top
  93. calc:   stp     x29, x30, [sp, -16]!            // Allocate
  94.         mov     x29, sp
  95.         adrp    x22, value
  96.         add     x22, x22, :lo12:value
  97.         ldr     d3, [x22]                       // d3 is now 1.0e-10
  98.  
  99.         fmov    counter, 1.0                    // Increments by 1
  100.         fmov    power, 1.0
  101.         fmov    accum, 1.0                      // Accumulator is 1
  102.  
  103.         fmov    numerator, d0                   // Num = x
  104.         fmov    factorial, power
  105.         fdiv    quotient, numerator, factorial // x / factorial
  106.         fadd    accum, accum, quotient
  107.  
  108.  
  109. loop:   fmul    numerator, numerator, d0
  110.         fadd    power, power, counter           // power++
  111.         fmul    factorial, factorial, power     // factor * power
  112.         fdiv    quotient, numerator, factorial  //
  113.         fadd    accum, accum, quotient          //  accum += term
  114.  
  115.         fabs    quotient, quotient
  116.         fcmp    quotient, d3                    // Compare
  117.         b.ge    loop                            // If quotient >= d3
  118.  
  119.         fmov    d0, accum
  120.         ldp     x29, x30, [sp], 16
  121.         ret
  122.  
  123. end:    mov     w0, fd_r                        // 1st arg (fd)
  124.         mov     x8, 57                          // closing I/O
  125.         svc     0                               // call sys func
  126.         mov     w0, 0                           // ret
  127.  
  128. done:   ldp     x29, x30, [sp], dealloc
  129.  
  130.         ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement