Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- define(fd_r, w19)
- define(nread_r, x20)
- define(buf_base_r, x21)
- buf_size = 8
- alloc = -(16 + buf_size) & -16
- dealloc = -alloc
- buf_s = 16
- AT_FDCWD = -100
- pn: .string "input.bin" // Name of file
- fmt1: .string "| Input | e^x | e^-x |" // header
- fmt2: .string "| %13.10f \t| %13.10f \t| %13.10f \t|\n" // Values
- fmt3: .string "Error opening file %s \n Aborting \n"
- fmt4: .string "\n_________________________________________________________________________\n" //divider
- value: .double 0r1.0e-10
- .balign 4
- .global main
- counter .req d19 // +1
- accum .req d20 // Accumulator
- factorial .req d21 // Factorial
- power .req d22 // power
- quotient .req d23 // Quotient
- numerator .req d24 // Numerator
- main: stp x29, x30, [sp, alloc]!
- mov x29, sp
- // Printing formatted header
- adrp x0, fmt1 // Header
- add x0, x0, :lo12:fmt1 // print header
- bl printf
- adrp x0, fmt4
- add x0, x0, :lo12:fmt4
- bl printf
- // Open file
- mov w0, AT_FDCWD // arg1 (cwd)
- adrp x1, pn // arg2 (pathname)
- add x1, x1, :lo12:pn
- mov w2, 0 // arg3 (read only)
- mov w3, 0 // arg4 (not used)
- mov x8, 56 // openat I/O
- svc 0 // Call sys func
- mov fd_r, w0 // Record file descriptor
- // Error check for openat
- cmp fd_r, 0
- b.ge openok
- adrp x0, fmt3 // Error
- add x0, x0, :lo12:fmt3
- bl printf // print
- mov w0, -1
- b done
- openok: add buf_base_r, x29, buf_size // calculate buf base
- top: // read ints from binary file
- mov w0, fd_r // 1st arg (fd)
- mov x1, buf_base_r // 2nd arg (buf)
- mov w2, buf_size // 3rd arg (n)
- mov x8, 63 // Read I/O
- svc 0 // Call sys func
- mov nread_r, x0 // # of bytes
- // Error check
- cmp nread_r, buf_size // nread != 8
- b.ne end // read failed - exit loop
- bl exp
- // e^x / e^-x
- exp: ldr d0, [buf_base_r] // d is x
- bl calc // go to calculations
- fmov d1, d0 // Mov arg1
- ldr d0, [buf_base_r] // d is x
- fneg d0, d0 // d is -x
- bl calc
- fmov d2, d0 // moves to arg2
- // Print
- adrp x0, fmt2 // Printing the values %13.10f
- add x0, x0, :lo12:fmt2 // print
- ldr d0, [buf_base_r] // arg
- bl printf
- b top
- calc: stp x29, x30, [sp, -16]! // Allocate
- mov x29, sp
- fmov counter, 1.0 // It's a counter
- fmov power, 1.0
- fmov accum, 1.0 // Accumulator is 1
- fmov numerator, d0 // Num = x
- fmov factorial, power
- fdiv quotient, numerator, factorial // x / factorial
- fadd accum, accum, quotient // accum + quotient
- loop: adrp x27, value
- add x27, x27, :lo12:value
- ldr d3, [x27] // d3 = 0r1.0e-10
- fmul numerator, numerator, d0 // d0 * num
- fadd power, power, counter // power++
- fmul factorial, factorial, power // factor * power
- fdiv quotient, numerator, factorial // quotient = num / fact
- fadd accum, accum, quotient // accum += quotient
- fabs quotient, quotient
- fcmp quotient, d3 // Compare
- b.ge loop // If quotient >= d3
- fmov d0, accum
- ldp x29, x30, [sp], 16
- ret
- end: mov w0, fd_r // 1st arg (fd)
- mov x8, 57 // closing I/O
- svc 0 // call sys func
- mov w0, 0 // ret
- done: ldp x29, x30, [sp], dealloc
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement