;Multiplying a 16 bit number by an 8 bit number.
;consider AABB x CC
;step1: BB x CC = XXYY where as XX is carry, YY is lower byte
;step2: AA x CC = UUVV where as UU is carry, VV is lower byte
;step3: UUVV + XX
ORG 0H
IN_ADDR EQU 4000H
OUT_ADDR EQU 4010H
MAIN:
MOV DPTR, #IN_ADDR
MOV A, #02H
MOVX @DPTR, A
INC DPTR
MOV A, #03H
MOVX @DPTR, A
INC DPTR
MOV A, #04H
MOVX @DPTR, A
LCALL MULT
SJMP MAIN
MULT:
MOV DPTR, #IN_ADDR ;Data is stored from location IN_ADDR onwards
MOVX A, @DPTR ;Get the higher byte of the 16 - bit number
MOV R0, A ;and store it in R0
INC DPTR ;Move to the next memory location
MOVX A, @DPTR ;Get the lower byte of the 16 - bit number
MOV R1, A ;and store it in R1
INC DPTR ;Move to the next memory location.
MOVX A, @DPTR ;Get the 8 - bit number
MOV R2, A ;and store it in R2
;---Do the Multiplication and Store the Output at Each Step---
MOV DPTR, #OUT_ADDR ;Start storing the answers from here
;Least siginificant byte goes first so the bytes
;of the answer will be in reverse order.
step1: ;STEP ONE of the above explanation:
MOV A, R1 ;Get the lower byte and store it in the accumulator
MOV B, R2 ;Get the 8-bit number.
MUL AB ;Multiply the two
MOVX @DPTR, A ;Store the lower byte in the external memory. The higher byte
;which is in the B register is the carry. (see step one again if unclear)
MOV R3, B ;Store the carry in register B
step2: ;STEP TWO of the above explanation
MOV A, R0 ;Get the lower byte and store it in the accumultor.
MOV B, R2 ;Get the 8-bit number
MUL AB ;Multiply them.
step3: ;STEP THREE of the above explanation:
ADD A, R3 ;Add the carry to the lower byte of the answer in step two.
INC DPTR ;Mov to next memory location
MOVX @DPTR, A ;Store the second byte of the final answer in the external memory.
MOV A, B ;Put the higher byte of the answer of step two in accumulator.
ADDC A, #00h ;This step adds a carry to the higher byte. We just need to add a carry so
;we add 0 to the higher byte with carry if a carry was generated from the
;previous addition.
INC DPTR ;Move to the next memory location.
MOVX @DPTR, A ;Store the final result.
RET
END
Related topics:
8051 Program - arithmetic operation 8bit | 8051 Program - addition 8bit | 8051 Program - subtraction 8bit | 8051 Program - multiplication 8bit | 8051 Program - division 8bit | 8051 Program - addition 16bit | 8051 Program - subtraction 16bit | 8051 Program - multiplication 16bit | 8051 Program - division 16bit | 8051 Program - addition multibyte | 8051 Program - addition 8bit 2digit bcd | 8051 Program - memory subroutines | 8051 Program - math subroutines | 8051 Program - conversion subroutines
List of topics: 8051
No comments:
Post a Comment