In: Computer Science
**Add comments to existing ARM code to explain steps**
Question that my code answers: Write an ARM assembly program to calculate the value of the following function:
f(y) = 3y^2 – 2y + 10 when y = 3.
My Code below, that needs comments added:
FunSolve.s
LDR R6,=y
LDR R1,[R6]
MOV R2,#5
MOV R3,#6
MUL R4,R1,R1
MUL R4,R4,R2
MUL R5,R1,R3
SUB R4,R4,R5
ADD R4,R4,#8
st B st
y DCD 3
Your code and its meaning is as follows
LDR R6,=y // Load the address for the value of 'y' as R6
LDR R1,[R6] // Load the value at the address found in R6 to register R1 (R1 = 3)
MOV R2,#5 // Move the number 5 to R2 (R2 = 5)
MOV R3,#6 // Move the number 6 to R3 (R3 = 6)
MUL R4,R1,R1 // R4 = R1 * R1 (R4 = 3 * 3 = 9)
MUL R4,R4,R2 // R4 = R4 * R2 ( R4 = 9 * 5 = 45)
MUL R5,R1,R3 // R5 = R1 * R3 (R5 = 3 * 6 = 18)
SUB R4,R4,R5 // R4 = R4 - R5 (R4 = 45 - 18 = 27)
ADD R4,R4,#8 // R4 = R4 +8 (R4 = 27 + 8 = 35)
st B st // ***********I didn't get it **********
y DCD 3 // Defining a constant data 3 to 'y' ( y = 3)
The constant values you entered in this code will not produce the correct result.
The function given here is
f(y) = 3y^2 - 2y + 10
and then putting y=3
f(3) = 3 * (3 * 3) - 2 * 3 +10
= 31
The correct code is
LDR R6,=y // Load the address for the value of 'y' as R6
LDR R1,[R6] // Load the value at the address found in R6 to register R1 (R1 = 3)
MOV R2,#3 // Move the number 3 to R2 (R2 = 3)
MOV R3,#2 // Move the number 2 to R3 (R3 = 2)
MUL R4,R1,R1 // R4 = R1 * R1 (R4 = 3 * 3 = 9; this indicate y^2)
MUL R4,R4,R2 // R4 = R4 * R2 ( R4 = 9 * 3 = 27; this indicate 3y^2)
MUL R5,R1,R3 // R5 = R1 * R3 (R5 = 3 * 2 = 6; this indicate 2y)
SUB R4,R4,R5 // R4 = R4 - R5 (R4 = 27 - 6 = 21; this indicate 3y^2-2y)
ADD R4,R4,#10 // R4 = R4 + 10 (R4 = 21 + 10 = 31)
STR R1, R4 // Storing the result 31 in register R1 (R1 = 31)
y DCD 3 // Defining a constant data 3 to 'y' ( y = 3)