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

Write an ARM assembly function that takes an array of integers and insures that

ID: 3553235 • Letter: W

Question

Write an ARM assembly function that takes an array of integers and insures that all entries are positive. Remember the initial integer in the array is at index zero.

The C language program is:

/* absdriver.c - driver for absarray function */

#include <stdio.h>

#include <stdlib.h>

extern int absarray( int array[], int size ) ;

int main( int argc, char * argv[] )

{

            int array[] = {1, 3, -5, -252, 280, -332 } ;

            int asize = sizeof(array) / sizeof(int) ;

            int result ;

            int i ;

           

            result = absarray( array, asize ) ;

            for( i = 0 ; i < asize ; i++ )

                        printf( " %d ", array[i] ) ;

            printf( " " ) ;

            exit( 0 ) ;

           

}

The input to the ARM assembly language function is a pointer to the first element of the array in register a1. The size of the array is in a2 and is the number of integers. Remember an integer is 4 bytes long.

Recall that the ldr instruction does not affect the condition codes. Also consider the reverse subtract instruction, rsb.




For ARM we have


AREA Program,CODE,READONLY

    ENTRY

Main

    EOR R3,R3,R3; Clear R3

    ADD R3,R1,R3; load R3 with R1 ie address

    EOR R0,R0,R0; clear R0

    ADD R0,R1,R0; load R0 with the size

    EOR R4,R4,R4; clear R4

Loop

    LDR R5,[R3] ; load R5 with the value at address

    ADD R4,R4,R5 ; add R4 with the value present at address,signed

    ADD R3,R3,#4; increment address pointer by 4 as int is 4 bytes

    SUB R0,R0,#1; decrease the count

    CMP R0,#0; compare the count with 0 to check for end of array

    BNE Loop ;loop back if count is not 0

    MOV PC,R14 ; load Pc with the return address with the result stored in R4

    END


Is this correct?


Can someone provide me with the output of this since I have not installed correct librarys for this.




Explanation / Answer

AREA Program,CODE,READONLY

ENTRY

Main

EOR R3,R3,R3; Clear R3

ADD R3,R1,R3; load R3 with R1 ie address

EOR R0,R0,R0; clear R0

ADD R0,R1,R0; load R0 with the size

EOR R4,R4,R4; clear R4

Loop

LDR R5,[R3] ; load R5 with the value at address

ADD R4,R4,R5 ; add R4 with the value present at address,signed

ADD R3,R3,#4; increment address pointer by 4 as int is 4 bytes

SUB R0,R0,#1; decrease the count

CMP R0,#0; compare the count with 0 to check for end of array

BNE Loop ;loop back if count is not 0

MOV PC,R14 ; load Pc with the return address with the result stored in R4


END




there is no need to use the RSB instruction.

as the addition in arm assembly is signed. so the sign is taken care by the processor.

Hope it helps...Please feel free to ask if any clarification is needed...:)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote