/* Given 8-bit variables A and B, each holding * an 8-bit signed 2's complement number. Write * a program to find the average of A and B. * Place the result into variable C. */ .INCLUDE .DSEG A: .BYTE 1 B: .BYTE 1 C: .BYTE 2 .CSEG ; inputs: 8-bit variables A and B ; output: 16-bit register C Avg8s: ; load registers A and B lds r24,A lds r26,B ; find average C = A+B/2 rcall Adder816s ; C=A+B ; divide by 2 asr r25 ; least significant bit moved to carry bit C ror r24 ; carry moves into most significant bit of r24 ; store the 8 bit result sts C,r24 clr r25 sts C+1,r25 rjmp Avg8s ; Add two 8-bit signed 2's complement numbers, ; where sum of A and B may be 9 bits ; input: r24 and r26 are two 8-bit numbers ; output: register pair r25:r24 equals sum of r24 and r25 Adder816s: ; make variables 16-bit clr r25 ; guess r25 is positive 0x00:A sbrc r24,7 ; if number is positive guess is correct so skip next instruction ser r25 ; guess incorrect, number is negative 0xFF:A clr r27 sbrc r26,7 ser r27 ;add add r24,r26 adc r25,r27 ;store sts C,r24 ; store the least significant byte sts C+1,r25 ; store most significant bytes ret