题目描述:
多字节数据求和,求无符号数Ai的累加和。Ai为单字节数,并按i(i=1,2,3,...,,n)顺序存放在RAM从60H开始的单元中,
n放在R2中,求他们的和(双字节)高位放在R3,低位放在R4中。
算法思想:
向让低四位相加求和(ADD),如果有进位(CY=1),则对高四位用ADDC,并将结果放入R3
源代码:
ORG 0000H LJMP ADD2 ORG 0100H ADD2: MOV R2,#8H MOV R0,#60H MOV A,#0H MOV R4,#0DFH MOV R5,#09H TT: ADD A,R5 MOV @R0,A INC R0 INC R5 MOV A,R4 DJNZ R2,TT ADD1: CLR A MOV R3,#00H MOV R4,#00H MOV R2,#8H MOV R0,#60H MAIN: MOV A,R4 ADD A,@R0 MOV R4,A CLR A ADDC A,R3 MOV R3,A INC R0 CLR C DJNZ R2,MAIN SJMP $ END
调试结果:
ORG 0000HLJMP ADD2ORG 0100HADD2:MOV R2,#8HMOV R0,#60HMOV A,#0HMOV R4,#0DFHMOV R5,#09HTT:ADD A,R5MOV @R0,AINC R0INC R5MOV A,R4DJNZ R2,TTADD1:CLR AMOV R3,#00HMOV R4,#00HMOV R2,#8HMOV R0,#60HMAIN:MOV A,R4ADD A,@R0MOV R4,ACLR AADDC A,R3MOV R3,AINC R0CLR CDJNZ R2,MAINSJMP $END