0 Replies - 1585 Views - Last Post: 17 July 2008 - 08:50 AM

#1 Louisda16th   User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

8051 Assembly - Multiplying a 16 - bit number with an 8 - bit number

Posted 17 July 2008 - 08:50 AM

Description: Run the program in an 8051 simulator.The program shows how you can handle numbers larger than the microcontroller's wordsize i.e. 8 bits.
;Multiplying a 16 bit number by an 8 bit number.
;Lets look at a normal multiplication first:
;Consider 19 x 12
;First multiply 12 by the least significant digit.
;		12 x 9 = 108 	Take 10 as a carry since it exceeds the number of digits in "9" i.e. 1
;						and keep 8 as the least significant digit of the answer
;Now multiply 12 by the most significant digit.
;		12 x 1 = 12.
;And add the carry to this
;		12 + 10 = 22
; Therefore the answer becomes 228.
;Now lets look at this in hex. Consider AABB x CC
;-----------------------STEP ONE---------------------------------------------------
;		BB x CC = 9504  	Keep 95 as carry and 04 as the lower byte (not bit or nibble) 
;							of the answer.
;----------------------STEP TWO----------------------------------------------------
;		AA x CC = 8778
;----------------------STEP THREE--------------------------------------------------
;Now add the carry
;		8778 + 95 = 880D	Remember that you are adding a 16bit number and an 8bit number.
;							So addition will take two steps.
;Therefore the answer is 880D04 
;Input is in 9000h to 9002h
;Output is in 9100h to 9102h
;----------------------------Get the Input--------------------
MOV DPTR, #9000h	;Data is stored from location 9000h 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, #9100h	;Start storing the answers from here
					;Least siginificant byte goes first so the bytes
					;of the answer will be in reverse order. 

;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
;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.

;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.
Here: SJMP Here

Is This A Good Question/Topic? 0
  • +

Page 1 of 1