Topic 5 Arithmetic & Logic Instruction (ISMAIL - FKEUTM 2018)
Topic 5 Arithmetic & Logic Instruction (ISMAIL - FKEUTM 2018)
• Arithmetic operations:
– ADD, ADC, ADIW, SUB, SBC, MUL, MULS, EXT, NEG.
• Logical
– AND, OR, EOR, NOT
• Shift
– ASL, ASR, LSL, LSR, ROL, ROR, ROXL, ROXR.
• Bit operations:
– BCLR, BSET, BCHG, BTST
Data Processing: Arithmetic
Operations
AVR Arithmetic Instruction
Arithmetic Result and Indicator
Register = result
SREG = indicator of arithmetic condition
SREG: I T H S V N Z C
Carry
Interrupt oVerflow Zero
Temporary Negative
Sign
Half carry N+V
...
LDI LDILDI 0x38
LDI
R16, R20, 0x9C
R20,
R20, 0x9C
0xA5
0x52;R16 = 0x38 Registers
R0 $001F IO Address
ALU LDI LDI 0x2F
LDI
LDI
R17, R21, 0x64
R21,
R1
R21, 0x9C
0x23
0x73
;R17 = $0020
0x2F TWBR $00
TWSR $01
R2
SUB R17R20,
R20, R21
R21;add R17;addStandard IO R21 from R20
;subtract
ADD SUB
ADD
R16, R20, R21 ;subtract
R21 toR21
to Registers
R16 R20from R20
...
...
…
SPH $3E
SREG: I T H S V N Z C
Solution:
Solution:
Solution: R15 11
$005F SREG $3F
CPU
Solution:
$52
$9C
$A5
$38
$9C
- $23
$73
0101
R16 0010
1001
1010
0011
1001
0111
$0060
1100
0101
1000
1100
R17 0011
General
purpose
...
+-- +$64
$9C
$2F 10010100
0010
0110 1100
0011
1111 RAM
PC $DF 1101 1111 R20 = $DF
…
$00
$82
$67
$100 1 0000
1000
0110
0000 0000
0010
0111
0000 R20
R20
R16
R20 ==
= $00
$82
0x67
00
(SRAM)
C = 1 because R21 is bigger than R20 and there is a borrow from D8 bit.
CC===100because
C becausethere
because R21 is
R21 is not
isnot
R30 bigger
bigger
a carry than R20
than
beyond R20 andbit.
and
the D7 there is
there is no
no borrow
borrow from
from D8
D8 bit.
bit.
Z
C == 00 decoder
because
because the
thereR20
is has
no a value
carry otherthe
beyond than
D7zero after the subtraction.
Instruction
ZZ =
H == 1
01 because
because there
because the R20
the R20 iscarry
is ahaszero after
a value
from the D3
other
the than 0 bit.
subtraction.
to theafter the subtraction.
D4 bit.
H = 1 because there isR31 a borrow
carry from
from D4D3
the toto
D3.
the D4 bit.
ZH
H == 00 because
because there
there is
is no
no borrow
borrow from
from D4
D4 to
to D3.
D3.
Z = 0 because the R16 (the result) has a value 0other
= 1 because
Instruction Register the R20 (the
registers
result) has a value in it after
than 0 the addition.
after the addition.
$FFFF
ADD: Add Without Carry
o ADD instruction is used to adds two registers without the C Flag
and place the result in the destination register, Rd.
o Example:
o Example:
Flag affected : H, S, V, N, Z, C
CPU cycle: 1
ADC: Example
ADIW: Add Immediate to Word
o ADIW instruction is used to adds and immediate value (0-63) to a
register pair and place the result in the register pair.
o Example:
Flag affected : S, V, N, Z, C
CPU cycle: 2
ADIW: Example
ADIW R30, 26 • If ZH is > 255, the carry bit in the SREG is set
Solution
• ZL = 13 = 0b 0000 1101
• ZH = 53 = 0b 0011 0101
After adding 26 = 0b 0001 1010
• ZL = 13 + 26 = 39 = 0b 0010 0111 is < 255, the carry bit will not set
• ZH = 53 = 0b 0011 0101 unchanged and the carry bit in SREG is not
set
SUB: Subtract without Carry
o SUB instruction is used to subtracts two registers and places the
result in the destination register, Rd
o Example:
o Example:
o Example:
Flag affected : S, V, N, Z, C
CPU cycle: 2
SBIW: Example
SBC: Subtract with Carry
o SBC instruction is used to subtracts two registers and subtracts
with the C Flag and places the results in the destination registers,
Rd.
o Example:
o Example:
Flag affected : H, S, V, N, Z, C
CPU cycle: 1
MUL: Multiply Unsigned
o MUL instruction perform 8-bit x 8-bit 16-bit unsigned
multiplication.
o Example:
Flag affected : Z, C
CPU cycle: 2
MULS: Multiply Signed
o MULS instruction perform 8-bit x 8-bit 16-bit signed
multiplication.
o Example:
Flag affected : Z, C
CPU cycle: 2
What is Signed and Unsigned numbers?
o In signed 8-bit operands, D7 (MSSB) is the sign, and D0 to D6 are
magnitude.
o If D7 = 0, operand is positive.
o The range of positive numbers are 0 to +127.
o If positive number larger than +127, a 16-bit operand must be used.
o If D7 = 1, operand is negative.
o The magnitude is represented in 2’s compliment.
What is Signed and Unsigned numbers?
What is Signed and Unsigned numbers?
MULSU: Multiply Signed with Unsigned
o MULSU instruction perform 8-bit x 8-bit 16-bit multiplication of
a signed and unsigned number.
Flag affected : Z, C
CPU cycle: 2
INC: Increment
o INC instruction used to adds one (1) to the contents of register Rd
and place the result in the destination register Rd.
o Example:
o Example:
o Example:
o Example:
o Example:
o Example:
o Example:
o Example:
o Example:
o Example:
o Example:
o Example:
Flag affected : S, V, N, Z
CPU cycle: 1
SWAP: Swap Nibbles
o SWAP instruction swaps high and low nibbles in a register.
o Example:
o Example:
o Solution:
LDI R16, 0x62 ;low byte R16 = 62H
LDI R17, 0x27 ;high byte R17 = 27H
LDI R26, 0x96 ;low byte R26 = 96H
LDI R27, 0x12 ;high byte R27 = 12H
ADD R26, R16 ;R26 = R26 + R16 = 96H + 62H = F8H, C=0, H=O,
Z=0
ADC R27, R17 ;R27 = R27 + R17 + C = 12H + 27H + 0 = 39H
Exercise Problem
o Write a program to add 18H and 2917H. Place the results in R25
and R24; R24 should have the lower byte.
o Solution:
LDI R24, 0x17 ;low byte R24 = 17H
LDI R25, 0x29 ;high byte R29 = 29H
ADIW R24, 0x18 ;R25:R24 = R25:R24 + 18H = 2917H + 18H = 292FH
Exercise Problem
o Write a program to multiplies 25H by 65H, and give the answer.
o Solution:
LDI R23, 0x25 ;load R23 = 25H
LDI R24, 0x65 ;load R24 = 65H
MUL R23, R24 ; R1:R0 = R23 x R24 = 25H x 65H = E99H
Exercise Problem
o Write a program to multiply 25H by 65H, and give the answer.
o Solution:
LDI R23, 0x25 ;load R23 = 25H
LDI R24, 0x65 ;load R24 = 65H
MUL R23, R24 ; R1:R0 = R23 x R24 = 25H x 65H = E99H
Exercise Problem
o Write a program to multiply two numbers as below and give the
answer.
a) F7H and 02H (both unsigned number)
b) F7H and 02H (both signed number)
c) F7H (signed) and 02H (unsigned)
o Solution (a):
LDI R23, 0xF7 ;load R23 = F7H (unsigned number)
LDI R24, 0x02 ;load R24 = 02H (unsigned number)
MUL R23, R24 ; R1:R0 = R23 x R24 = F7H x 02H = IEEH
Exercise Problem
o Solution (b):
LDI R23, 0xF7 ;load R23 = F7H (signed number)
LDI R24, 0x02 ;load R24 = 02H (signed number)
MULS R23, R24 ; R1:R0 = R23 x R24 = F7H x 02H = -9x2 = -18
= FFEEH
o Solution (c):
LDI R22, 0xF7 ;load R22 = F7H (signed number)
LDI R23, 0x02 ;load R23 = 02H (unsigned number)
MULSU R22, R23 ; R1:R0 = R22 x R23 = F7H x 02H = -9x2 = -18
;=FFEEH
Exercise Problem
o Show the result of following instructions:
LDI R20, 0x04 ;load R20 = 04H
ORI R20, 0x30 ;R20 = R20 OR 30H
o Solution:
Exercise Problem
o Show the result of following instructions:
LDI R20, 0x35 ;load R20 = 35H
ANDI R20, 0x0F ;R20 = R20 AND 0FH
o Solution:
Exercise Problem
o Show the result of following instructions:
LDI R20, 0x54 ;load R20 = 54H
LDI R21, 0x78 ;load R21 = 78H
EOR R20, R21 ;R20 = R20 EOR R21
o Solution: