InnerProduct.asm
aaronjohnsabu1999_IITB-EE-Sem05/EE337/Labwork/Q2/Reading_Material/Codes/InnerProduct.asm
org  0000h
ljmp main
org  0200h

init:							;Reserves 0x50, 0x51, 0x52, 0x53, 0x60, 0x61, 0x62, 0x63, 0x64, 0x70, 0x71, 0x72, 0x73, 0x74
		mov 0x50, #0x60		;0x50	-	location of x1
		mov 0x51, #0x70		;0x51	-	location of y1
		mov 0x52, #0x06		;0x52	-	length   of vectors
		mov 0x53, #0x7F		;0x53	-	location of storage
		mov 0x60, #0x04
		mov 0x61, #0x07
		mov 0x62, #0x09
		mov 0x63, #0x0B
		mov 0x64, #0x0C
		mov 0x65, #0x02
		mov 0x70, #0x05
		mov 0x71, #0x09
		mov 0x72, #0x0C
		mov 0x73, #0x0D
		mov 0x74, #0x04
		mov 0x75, #0x0F
ret

adder_16bit:					;Reserves 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
		mov  A,    0x31		;0x31	-	LSB1
		add  A,    0x33		;0x33	-	LSB2
		mov  0x36, A		;0x36	-	LSBY
		mov  A,    0x30		;0x30	-	MSB1
		addc A,    0x32		;0x32	-	MSB2
		mov  0x35, A		;0x35	-	MSBY
		mov  A,   #0x00
		addc A,   #0x00
		mov  0x34, A		;0x34	-	C
ret

inner_product:
		mov  R2, #0xFF		;R2 stores the value of i
		mov  R5, #0x00		;R5 stores LSB of cumulative sum
		mov  R4, #0x00		;R5 stores MSB of cumulative sum
CP1:	inc  R2
		mov  R0,  0x50
		mov  A,   R0
		add  A,   R2
		mov  R0,  A
		mov  A,  @R0
		mov  B,   A			;B stores x_i
		
		mov  R0,  0x51
		mov  A,   R0
		add  A,   R2
		mov  R0,  A
		mov  A,  @R0
		mov  R6,  A			;A stores y_i
		
		mul  AB
		mov  0x31, A
		mov  0x30, B
		mov  0x33, R5
		mov  0x32, R4
		lcall adder_16bit
		mov  R5, 0x36
		mov  R4, 0x35
		mov  A,  R2
		cjne A,  0x52, CP1
		
		mov  A,   R4
		add  A,   R5
		mov  R0,  0x53
		mov @R0,  A
		mov  P1,  A
ret

main:	lcall init
		lcall inner_product
here:	sjmp  here
end