[ Pobierz całość w formacie PDF ]

AVR-Assembler program
The following source code demonstrates realisation of multiplication in assembler.
; Mult8.asm multiplies two 8-bit-numbers to yield a 16-bit-result
;
.NOLIST
.INCLUDE "C:\avrtools\appnotes\8515def.inc"
.LIST
;
; 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
;
.CSEG
.ORG 0000
;
rjmp START
;
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)
;
MULT8:
;
; Clear start values
clr rmh ; clear interim storage
clr rel ; clear result registers
clr reh
;
; Here we start with the multiplication loop
;
MULT8a:
;
; 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
;
MULT8b:
;
; 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
;
Avr-Asm-Tutorial 32 http://www.avr-asm-tutorial.net
; End of the multiplication, result in reh:rel
;
; Endless loop
;
LOOP:
rjmp loop
Binary rotation
For understanding the multiplication operation,
it is necessary to understand the binary
rotation commands ROL and ROR. These
instructions shift all bits of a register one
position left (ROL) resp. right (ROR). The void
position in the register is filled with the content
of the carry bit of the status register, the bit that
rolls out of the register is shifted to the carry
bit. This operation is demonstrated using 0xAA
as an example for ROL and 0x55 as an
example for ROR.
Multiplication in the studio
The following screenshots show the multiplication program in the simulator.
The object-code has been
opened, the cursor is placed
on the first executable
instruction. F11 does single
steps.
The registers R0 and R2 are
set to 0xAA and 0x55, our test
binaries, to be multiplied.
Avr-Asm-Tutorial 33 http://www.avr-asm-tutorial.net
R2 is rotated to the
right, to roll the least
significant bit into the
carry bit. 0x55
(0101.0101) yielded
0x2A (0010.1010).
Because the carry bit
had a one the
content of the
registers R1:R0 is
added to the (empty)
register pair R4:R3,
resulting in 0x00AA
there.
Now the register pair
R1:R0 is rotated one
position left to
multiply this binary
by 2. From 0x00AA,
multiplication by 2
yields 0x0154.
The whole multipli-
cation loop is repea-
ted as long there is
at least one binary 1
in register R2. These
following loops are
not shown here.
Avr-Asm-Tutorial 34 http://www.avr-asm-tutorial.net
Using key F5 of the
studio we multi-
stepped over these
loops to a break-
point at the end of
the multiplication
routine. The result
register pair R4:R3
has the result of
the multiplication of
0xAA by 0x55:
0x3872.
This wasn't that complicated, just remind yourself on the similiar decimal operations. Binary multiplication
is much easier than decimal.
Division
Decimal division
Again we start with the decimal division, to better understand the binary division. We assume a division of
5678 by 12. This is done like this:
5678 : 12 = ?
--------------------------
- 4 * 1200 = 4800
----
878
- 7 * 120 = 840
---
38
- 3 * 12 = 36
--
2
Result: 5678 : 12 = 473 Remainder 2
===================================
Binary division
In binary the multiplication of the second number (4 * 1200, etc.) is not necessary, due to the fact that we
have only 0 and 1 as digits. Unfortunately binary numbers have much more single digits than their decimal
equivalent, so transferring the decimal division to its binary equivalent is a little bit inconvenient. So the
program works a bit different than that.
The division of a 16-bit binary number by a 8-bit binary in AVR assembler is listed in the following section.
; Div8 divides a 16-bit-number by a 8-bit-number (Test: 16-bit-number: 0xAAAA, 8-bit-number: 0x55)
.NOLIST
.INCLUDE "C:\avrtools\appnotes\8515def.inc"
.LIST
; Registers
.DEF rd1l = R0 ; LSB 16-bit-number to be divided
.DEF rd1h = R1 ; MSB 16-bit-number to be divided
.DEF rd1u = R2 ; interim register
.DEF rd2 = R3 ; 8-bit-number to divide with
.DEF rel = R4 ; LSB result
.DEF reh = R5 ; MSB result
.DEF rmp = R16; multipurpose register for loading
;
.CSEG
.ORG 0
rjmp start
Avr-Asm-Tutorial 35 http://www.avr-asm-tutorial.net
start:
; Load the test numbers to the appropriate registers
ldi rmp,0xAA ; 0xAAAA to be divided
mov rd1h,rmp
mov rd1l,rmp
ldi rmp,0x55 ; 0x55 to be divided with
mov rd2,rmp
; Divide rd1h:rd1l by rd2
div8:
clr rd1u ; clear interim register
clr reh ; clear result (the result registers
clr rel ; are also used to count to 16 for the
inc rel ; division steps, is set to 1 at start)
; Here the division loop starts
div8a:
clc ; clear carry-bit
rol rd1l ; rotate the next-upper bit of the number
rol rd1h ; to the interim register (multiply by 2)
rol rd1u
brcs div8b ; a one has rolled left, so subtract
cp rd1u,rd2 ; Division result 1 or 0?
brcs div8c ; jump over subtraction, if smaller
div8b:
sub rd1u,rd2; subtract number to divide with
sec ; set carry-bit, result is a 1
rjmp div8d ; jump to shift of the result bit
div8c:
clc ; clear carry-bit, resulting bit is a 0
div8d:
rol rel ; rotate carry-bit into result registers
rol reh
brcc div8a ; as long as zero rotate out of the result registers: go on with the division loop
; End of the division reached
stop:
rjmp stop ; endless loop
Program steps during division
During execution of the program the following steps are ran:
" Definition and preset of the registers with the test binaries,
" presetting the interim register and the result register pair (the result registers are presetted to
0x0001! After 16 rotations the rolling out of the one stops further division steps.),
" the 16-bit-binary in rd1h:rd1l is rotated bitwise to the interim register rd1u (multiplication by 2), if a 1
is rotated out of rd1u, the program branches to the subtraction step in step 4 immediately,
" the content of the interim register is compared with the 8-bit binarly in rd2, if rd2 is smaller it is
subtracted from the interim register and the carry-bit is set to one, if rd2 is greater the subtraction is
skipped and a zero is set to the carry flag,
" the content of the carry flag is rotated into the result register reh:rel from the right,
" if a zero rotated out of the result register, we have to repeat the division loop, if it was a one the
division is completed.
If you don't understand rotation yet you'll find this operation discussed in the multiplication section.
Division in the simulator
The following screen shots demonstrate the
program steps in the studio. To do this, you
have to assemble the source code and
open the resulting object file in the studio.
The object code has been started, the
cursor is on the first executable instruction. [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • nadbugiem.xlx.pl
  • img
    \