Advertisement
Guest User

Untitled

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