please dont rip this site

Atmel AVR Microcontroller Basic Math Code Library

for Multiplication

Multiply 8 bits by 8 bits in software by

; Flow of multiplication
; 1.The binary to be multiplicated with, is shifted bitwise
;   into the carry bit. If it is a one, the binary number
;   is added to the result, if it is not a one that was
;   shifted out, the number is not added
; 2.The binary number is multiplied by 2 by rotating it
;   one position left, shifting a 0 into the void position.
; 3.If the binary to be multiplied with, is not zero, the
;   multiplication loop is repeated. If it is zero, the
;   multiplication is done.
; Used registers
.DEF rm1 = R0 ; Binary number to be multiplicated (8 Bit)
.DEF rmh = R1 ; Interim storage
.DEF rm2 = R2 ; Binary number to be multiplicated with (8 Bit)
.DEF rel = R3 ; Result, LSB (16 Bit)
.DEF reh = R4 ; Result, MSB
.DEF rmp = R16 ; Multi purpose register for loading
.ORG 0000
	rjmp START
	ldi rmp,0xAA ; example binary 1010.1010
	mov rm1,rmp  ; to the first binary register
	ldi rmp,0x55 ; example binary 0101.0101
	mov rm2,rmp  ; to the second binary register
; Here we start with the multiplication of the two binaries
; in rm1 und rm2, the result will go to reh:rel (16 Bit)
; Clear start values
	clr rmh ; clear interim storage
	clr rel ; clear result registers
	clr reh
; Here we start with the multiplication loop
; Step 1: Rotate lowest bit of binary number 2 to the carry
;         flag (divide by 2, rotate a zero into bit 7)
	clc ; clear carry bit
	ror rm2 ; bit 0 to carry, bit 1 to 7 one position to
	        ; the right, carry bit to bit 7
; Step 2: Branch depending if a 0 or 1 has been rotated to
;         the carry bit
	brcc MULT8b ; jump over adding, if carry has a 0
; Step 3: Add 16 bits in rmh:rml to the result, with overflow
;         from LSB to MSB
	add rel,rm1 ; add LSB of rm1 to the result
	adc reh,rmh ; add carry and MSB of rm1
; Step 4: Multiply rmh:rm1 by 2 (16 bits, shift left)
	clc ; clear carry bit
	rol rm1 ; rotate LSB left (multiply by 2)
	rol rmh ; rotate carry into MSB and MSB one left
; Step 5: Check if there are still one's in binary 2, if
;         yes, go on multiplicating
	tst rm2 ; all bits zero?
	brne MULT8a ; if not, go on in the loop
; End of the multiplication, result in reh:rel
; Endless loop
	rjmp loop

©2002 by

file: /Techref/atmel/avr/mul8x8sw-aat.htm, 2KB, , updated: 2007/9/14 09:49, local time: 2024/7/21 00:38,

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> Atmel AVR, atmega, Microcontroller, embedded controller, math</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?


Welcome to!


Welcome to!