We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello to everyone.
When I have to calculate the average of two integer numbers, first i have to add them. Sometimes an overflow occurs. One technique maybe to compare the result to one of the numbers, if result is lower, an overflow is recognized.
Is this good style or better use Assembler?
Hello to everyone, thanks for reply. Here's my Assembler solution, but i'am not sure it will deliver a correct result under all circumstances!
//********************************************************************* // Computing the average of signed/unsigned numbers in ARM Assembler //********************************************************************* AREA ?C?bla, CODE, READONLY, ALIGN=2 PUBLIC my_average?A my_average?A PROC CODE32 ADDS R0,R0,R1 ;add and alter condition code MOVVS R0,R0,RRX ;if sign has moved to C rotate through Carry MOVVC R0,R0,ASR #1 ;if sign not changed do arithmetic shift bx lr ; return R0 ENDP END //*********************************************************************
Here's the corrected version, divided into average for signed / unsigned numbers.
//********************************************************************* // Computing the average of Signed numbers in ARM Assembler //********************************************************************* AREA ?C?bla, CODE, READONLY, ALIGN=2 PUBLIC my_signed_average?A my_signed_average?A PROC CODE32 ADDS R0,R0,R1 ;add and alter condition code MOVVS R0,R0,RRX ;if sign has moved to C rotate through Carry MOVVC R0,R0,ASR #1 ;if sign not changed do arithmetic shift bx lr ;return R0 ENDP //********************************************************************* // Computing the average of Unsigned numbers in ARM Assembler //********************************************************************* PUBLIC my_unsigned_average?A my_unsigned_average?A PROC CODE32 ADDS R0,R0,R1 ;add and alter condition code MOV R0,R0,RRX ;rotate through Carry bx lr ;return R0 ENDP END //*********************************************************************