85.6k views
2 votes
Write a function (subroutine) that inputs a data value in register r0 and returns value in r0. The function returns y 5 a 1 bx 1 cx2, where a, b, and c are parameters built into the function (i.e., they are not passed to it). The subroutine also performs clipping. If the output is greater than a value d, it is constrained to d (clipped). The input in r0 is a positive binary value in the range 0 to 0xFF. Apart from r0, no other registers may be modified by this subroutine. Write ARM code to implement the following C operation.

Int s=0;
for ( i = 0; i < 10; i++) { s = s + i*i;)

1 Answer

3 votes

Solution :

int f(int x){


\text{return a + b*x +c*x*x};

}*/

int f(
\text{int R0}){

int stack[2] = {
\text{R1,R2}};


\text{R1 = R0};


\text{R1 = R1}* \text{ R1};


\text{R2 = C};

R1 = R1 * C; /*R1 =
cx^2*/


\text{R2 = B};


\text{R0 = R0} * R2; /* R0 = bx */


\text{R0 = R0 + R1}; /*R0 =
bx + cx^2 */


\text{R2 = C};


\text{R0 = R0 + R2}; /*R0 = a+bx+cx^2 */


\text{R1 = stack[0];}


\text{ R2 = stack[1];}


\text{ return R0;}

}

/*
\text{ARM code to implement the following C operation}

int s=0;


\text{for ( i = 0; i < 10; i++)}

{
\text{s = s + i } * i;)

*/

AREA SumSquares, code, readWrite

ENTRY


\text{MOV r0, #0} ;loop
\text{ counter i present at 0}

MOV r1, #0 ; s = 0

Loop
\text{MUL r2, r0, r0} ;calculate i*i


\text{ADD r1, r1, r2} ;s = s+ i*i


\text{ADDS r0, r0}, #1 ; i = i+1


\text{CMP r0},#10 ; test for end


\text{BNE} Loop ;
\text{continue until all added}

END

User Pwaterz
by
5.0k points