Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Comment this Assembly code please. As many lines as possible: factorial(int): st

ID: 3866046 • Letter: C

Question

Comment this Assembly code please. As many lines as possible:

factorial(int):

stp x29, x30, [sp, -48]!

  add x29, sp, 0

  str x19, [sp, 16]

  str w0, [x29, 44]

  ldr w0, [x29, 44]

  cmp w0, wzr

  beq .L2

  ldr w0, [x29, 44]

  cmp w0, 1

  bne .L3

.L2:

  mov x0, 1

  b .L4

.L3:

  ldrsw x19, [x29, 44]

  ldr w0, [x29, 44]

  sub w0, w0, #1

  bl factorial(int)

  mul x0, x19, x0

.L4:

  ldr x19, [sp, 16]

  ldp x29, x30, [sp], 48

  ret

power(float, int):

  stp x29, x30, [sp, -32]!

  add x29, sp, 0

  str s0, [x29, 28]

  str w0, [x29, 24]

  ldr w0, [x29, 24]

  cmp w0, wzr

  bne .L6

  fmov s0, 1.0e+0

  b .L7

.L6:

  ldr w0, [x29, 24]

  cmp w0, 1

  bne .L8

  ldr s0, [x29, 28]

  b .L7

.L8:

  ldr w0, [x29, 24]

  sub w0, w0, #1

  ldr s0, [x29, 28]

  bl power(float, int)

  fmov s1, s0

  ldr s0, [x29, 28]

  fmul s0, s1, s0

.L7:

  ldp x29, x30, [sp], 32

  ret

.LC0:

  .string "Enter a value of X between -5 and 5: "

.LC1:

  .string "%lf"

.LC2:

  .string "Please enter a new value of X between -5 and 5: "

.LC3:

  .string "Enter the value of A less than 16: "

.LC4:

  .string "%lli"

.LC5:

  .string "Please enter a new value of A less than 16: "

.LC6:

  .string "The exponential value is: %lf "

main:

  stp x29, x30, [sp, -64]!

  add x29, sp, 0

  str d8, [sp, 16]

  adrp x0, .LC0

  add x0, x0, :lo12:.LC0

  bl printf

  add x1, x29, 40

  adrp x0, .LC1

  add x0, x0, :lo12:.LC1

  bl scanf

.L12:

  ldr d1, [x29, 40]

  fmov d0, 5.0e+0

  fcmpe d1, d0

  cset w0, gt

  uxtb w0, w0

  eor w0, w0, 1

  uxtb w0, w0

  cmp w0, wzr

  beq .L10

  ldr d1, [x29, 40]

  fmov d0, -5.0e+0

  fcmpe d1, d0

  cset w0, mi

  uxtb w0, w0

  eor w0, w0, 1

  uxtb w0, w0

  cmp w0, wzr

  bne .L11

.L10:

  adrp x0, .LC2

  add x0, x0, :lo12:.LC2

  bl printf

  add x1, x29, 40

  adrp x0, .LC1

  add x0, x0, :lo12:.LC1

  bl scanf

  b .L12

.L11:

  adrp x0, .LC3

  add x0, x0, :lo12:.LC3

  bl printf

  add x1, x29, 32

  adrp x0, .LC4

  add x0, x0, :lo12:.LC4

  bl scanf

.L14:

  ldr x0, [x29, 32]

  cmp x0, 16

  ble .L13

  adrp x0, .LC5

  add x0, x0, :lo12:.LC5

  bl printf

  add x1, x29, 32

  adrp x0, .LC4

  add x0, x0, :lo12:.LC4

  bl scanf

  b .L14

.L13:

  str wzr, [x29, 52]

.L16:

  ldrsw x1, [x29, 52]

  ldr x0, [x29, 32]

  cmp x1, x0

  bgt .L15

  ldr d0, [x29, 40]

  fcvt s0, d0

  ldr w0, [x29, 52]

  bl power(float, int)

  fmov s8, s0

  ldr w0, [x29, 52]

  bl factorial(int)

  scvtf s0, x0

  fdiv s0, s8, s0

  fcvt d0, s0

  ldr d1, [x29, 56]

  fadd d0, d1, d0

  str d0, [x29, 56]

  ldr w0, [x29, 52]

  add w0, w0, 1

  str w0, [x29, 52]

  b .L16

.L15:

  adrp x0, .LC6

  add x0, x0, :lo12:.LC6

  ldr d0, [x29, 56]

  bl printf

  mov w0, 0

  ldr d8, [sp, 16]

  ldp x29, x30, [sp], 64

  ret

Explanation / Answer


factorial(int):
stp x29, x30, [sp, -48]! ; this is to read and initialize parameter value
add x29, sp, 0
str x19, [sp, 16]
str w0, [x29, 44]
ldr w0, [x29, 44] ; loading parameter value
cmp w0, wzr ; comparing value, mostly if condition
beq .L2
ldr w0, [x29, 44]
cmp w0, 1 ; comparing with value 1; if(n==1), return 0
bne .L3 ; recursively calling function
.L2:
mov x0, 1 ;moving pointer
b .L4
.L3:
ldrsw x19, [x29, 44] ; loading current stack pointer value
ldr w0, [x29, 44]
sub w0, w0, #1 ; subtract value -1 and call recursively (n-1)
bl factorial(int) ; recursively calling factorial value
mul x0, x19, x0 ; multiplying previous result ... n*fact(n-1)
.L4:
ldr x19, [sp, 16]
ldp x29, x30, [sp], 48
ret ; finally return answer
power(float, int): ; power function
stp x29, x30, [sp, -32]! ; next five statements are to load params into stack
add x29, sp, 0
str s0, [x29, 28]
str w0, [x29, 24]
ldr w0, [x29, 24]
cmp w0, wzr ; comparing with 0.. if(exponent == 0)
bne .L6
fmov s0, 1.0e+0
b .L7
.L6:
ldr w0, [x29, 24]
cmp w0, 1 ; if comparing with 0 success, then return 1
bne .L8
ldr s0, [x29, 28]
b .L7
.L8:
ldr w0, [x29, 24]
sub w0, w0, #1 ;subtracting exponent value .. (exponent-1)
ldr s0, [x29, 28] ;loading base value
bl power(float, int) ;calling recursively .. power(base,exponent-1)
fmov s1, s0
ldr s0, [x29, 28]
fmul s0, s1, s0 ; multiplying with previous result ... base * power(base, exponent - 1);
.L7:
ldp x29, x30, [sp], 32
ret ; return final answer
; #### these statements are to read inputs from user #####
.LC0:
.string "Enter a value of X between -5 and 5: "
.LC1:
.string "%lf"
.LC2:
.string "Please enter a new value of X between -5 and 5: "
.LC3:
.string "Enter the value of A less than 16: "
.LC4:
.string "%lli"
.LC5:
.string "Please enter a new value of A less than 16: "
.LC6:
.string "The exponential value is: %lf "
main:
stp x29, x30, [sp, -64]!
add x29, sp, 0
str d8, [sp, 16]
adrp x0, .LC0
add x0, x0, :lo12:.LC0
bl printf
add x1, x29, 40
adrp x0, .LC1
add x0, x0, :lo12:.LC1
bl scanf
.L12:
ldr d1, [x29, 40]
fmov d0, 5.0e+0
fcmpe d1, d0
cset w0, gt
uxtb w0, w0
eor w0, w0, 1
uxtb w0, w0
cmp w0, wzr
beq .L10
ldr d1, [x29, 40]
fmov d0, -5.0e+0
fcmpe d1, d0
cset w0, mi
uxtb w0, w0
eor w0, w0, 1
uxtb w0, w0
cmp w0, wzr
bne .L11
.L10:
adrp x0, .LC2
add x0, x0, :lo12:.LC2
bl printf
add x1, x29, 40
adrp x0, .LC1
add x0, x0, :lo12:.LC1
bl scanf
b .L12
.L11:
adrp x0, .LC3
add x0, x0, :lo12:.LC3
bl printf
add x1, x29, 32
adrp x0, .LC4
add x0, x0, :lo12:.LC4
bl scanf
.L14:
ldr x0, [x29, 32]
cmp x0, 16
ble .L13
adrp x0, .LC5
add x0, x0, :lo12:.LC5
bl printf
add x1, x29, 32
adrp x0, .LC4
add x0, x0, :lo12:.LC4
bl scanf
b .L14
.L13:
str wzr, [x29, 52]
.L16:
ldrsw x1, [x29, 52]
ldr x0, [x29, 32]
cmp x1, x0
bgt .L15
ldr d0, [x29, 40]
fcvt s0, d0
ldr w0, [x29, 52]
bl power(float, int)
fmov s8, s0
ldr w0, [x29, 52]
bl factorial(int)
scvtf s0, x0
fdiv s0, s8, s0
fcvt d0, s0
ldr d1, [x29, 56]
fadd d0, d1, d0
str d0, [x29, 56]
ldr w0, [x29, 52]
add w0, w0, 1
str w0, [x29, 52]
b .L16
.L15:
adrp x0, .LC6
add x0, x0, :lo12:.LC6
ldr d0, [x29, 56]
bl printf
mov w0, 0
ldr d8, [sp, 16]
ldp x29, x30, [sp], 64
ret