Arithmetic Instructions
Arithmetic Instructions
Arithmetic Instructions
LDR R0,=0X80000000
MOV R2,#0X00000000
stop B stop
END
R4:0x80000000
R5:0x7FFFFFFF
AREA Rotate, CODE, READONLY
ENTRY
LDR R0,=0X80000000
MOV R2,#0X0000000F
SUBS R4,R0,R2
SBC R5,R4,#01
stop B stop
END
• R0 0x80000000
• R2 0x0000000F
• R4 0x7FFFFFF1
• R5 0x7FFFFFF0
• CPSR:V=1 C=1
EG
AREA Rotate, CODE, READONLY
ENTRY
LDR R0,=0X80000000
MOV R2,#0X0000000F
SUBS R4,R0,R2
;SBC R5,R4,#01
MOV R5,R0,RRX
stop B stop
END
R0 0x80000000
R5 = 0xC0000000
ELSE
R5=0x40000000
AREA Rotate, CODE, READONLY
ENTRY
LDR R0,=0X80000000
MOV R2,#0X0000000F
MOVS R5,R0,LSL #01
stop B stop
END
CPSR : Z=1 C=1 V=0//DOESNT AFFECT V FLAG
Eg:subtract instruction subtracts a value stored in
register r2 from a value stored
in register r1. The result is stored in register r0.
PRE r0 = 0x00000000
r1 = 0x00000002
r2 = 0x00000001
POST r0 = 0x00000001
This reverse subtract instruction (RSB) subtracts
r1 from the constant value #0, writing the
result to r0. You can use this instruction to
negate numbers.
PRE r0 = 0x00000000
r1 = 0x00000077
POST r0 = 0x0000000f
r1 = 0x00000005
64 bit addition:
r3 r2 (1st operand)
r1 r0 (2nd operand)
------------
r5 r4 (result)
________
SUBS R4,R0,R2
SBC R5,R1,R3
Find absolute
CMP R1,#0
RSBLT R0,R1,#0
done B done
Logical Instructions
Syntax: <instruction>{<cond>}{S} Rd, Rn, N
AND logical bitwise AND of two 32-bit values
Rd = Rn & N
ORR logical bitwise OR of two 32-bit values
Rd = Rn | N
EOR logical exclusive OR of two 32-bit values
Rd = Rn ∧N
BIC logical bit clear (AND NOT)
Rd = Rn & ∼N
This example shows a logical OR operation
between registers r1 and r2. r0 holds the
result.
PRE r0 = 0x00000000
r1 = 0x02040608
r2 = 0x10305070
ORR r0, r1, r2
POSTr0 = 0x12345678
This example shows a more complicated logical instruction
called BIC, which carries out
a logical bit clear.
PRE r1 = 0b1111
r2 = 0b0101
POST r0 = 0b1010
This is equivalent to
Rd = Rn AND NOT(N)
Comparison Instructions
Syntax: <instruction>{<cond>} Rn, N
• CMN compare negated
flags set as a result of Rn + N
• CMP compare
flags set as a result of Rn − N
• TEQ test for equality of two 32-bit values
flags set as a result of Rn ∧ N
• TST test bits of a 32-bit value
flags set as a result of Rn & N
This example shows a CMP comparison instruction. You
can see that both registers, r0 and
r9, are equal before executing the instruction. The value
of the z flag prior to execution is 0
and is represented by a lowercase z. After execution the z
flag changes to 1 or an uppercase
Z. This change indicates equality.
POSTr0 = 0x00000004
r1 = 0x00000002
r2 = 0x00000002
PRE r0 = 0x00000000
r1 = 0x00000000
r2 = 0xf0000002
r3 = 0x00000002
r1 = 0x00000001 ; = RdHi
MUL r4,r2,r1 ;r4=r2*r1
MULS r4,r2,r1 ;r4=r2*r1,then
set the flags
MLA r7,r8,r9,r3 ;r7=r8*r9+r3
DISADV:
Uses multiplier array
Consumes more area and power
Slow
Single cycle or more
Solution :
Multiplication using barrel shifter
ADD r0,r1,r1,LSL #1 ;r0=r1*3
SUB r0,r0,r1,LSL #4 ;r0=r1*3-r1*16
=-r1*13
ADD r0,r0,r1,LSL #7 ;r0=-r1*13+r1*128
=r1*115
Branch Instructions
Syntax:
B{<cond>} label
BL{<cond>} label
BX{<cond>} Rm
BLX{<cond>} label | Rm
B branch
pc = label
BL branch with link
• pc = label
• lr = address of the next instruction after the BL
BX branch exchange
pc = Rm & 0xfffffffe, T = Rm &1
BLX branch exchange with link
• pc = label, T = 1
• pc = Rm & 0xfffffffe, T = Rm &1
• lr = address of the next instruction after the BLX
BL subroutine
CMP r1, #5
MOVEQ r1, #0
--
---
subroutine
<subroutine code>
MOV pc, lr ; return by moving pc = lr
Factorial of a given number
AREA Rotate, CODE, READONLY
ENTRY
MOV R6,#03
MOV R4,R6
LOOP SUBS R4,R4,#1
MULNE R7,R6,R4
MOV R6,R7
BNE LOOP
stop B stop
END
AREA Rotate, CODE, READONLY
ENTRY
LDR R0,=0XF631024C
LDR R1,=0X17539ABD
EOR R0,R0,R1 ;R0^R1
EOR R1,R0,R1 ;R1^R0
EOR R0,R0,R1 ;R0^R1
! indicates that the instruction writes the calculated address back to the
base address register.
PRE r0 = 0x00000000
r1 = 0x00090000
mem32[0x00009000] = 0x01010101
mem32[0x00009004] = 0x02020202
POST(1) r0 = 0x02020202
r1 = 0x00009004
Preindexing:
POST(2) r0 = 0x02020202
r1 = 0x00009000
Postindexing:
POST(3) r0 = 0x01010101
r1 = 0x00009004
Single-register load-store addressing,
word or unsigned byte.
Addressing1mode and index method Addressing1syntax
STRCPY
LDRB R2,[R1],#1
STRB R2,[R0],#1
CMP R2,#0
BNE STRCPY
stop B stop
;stop program
MEM DATA(LSBYTE)
4000000C 33
40000008 11
40000004 22
40000000 AA
R0=0X40000000 R1=AA
R0=0X40000004 R2=22
R0=0X40000008 R3=11
R0=0X4000000C
LDMIB r0!, {r1-r3}
MEM DATA(LSBYTE)
4000000C DD
40000008 CC
40000004 BB
40000000 AA
R0=0X40000000
R0=0X40000004 R1=BB
R0=0X40000008 R2=CC
R0=0X4000000C R3=DD
LDMDA r0!, {r1-r3}
MEM DATA(LSBYTE)
4000000C DD
40000008 CC
40000004 BB
40000000 AA
R0=0X4000000C R3=DD
R0=0X40000008 R2=CC
R0=0X40000004 R3=BB
R0=0X40000000
LDMDB r0!, {r1-r3}
MEM DATA(LSBYTE)
4000000C DD
40000008 CC
40000004 BB
40000000 AA
R0=0X4000000C
R0=0X40000008 R3=CC
R0=0X40000004 R2=BB
R0=0X40000000 R1=AA
STMIA r0!, {r1-r3}
R0=0X40000000
R1=0X00000011
R2=0X00000022
R3=0X40000033
R4=0X40000044
MEM DATA(LSBYTE)
4000000C
40000008 33
40000004 22
40000000 11
R0=0X4000000C
STMIB r0!, {r1-r3}
R0=0X40000000
R1=0X00000011
R2=0X00000022
R3=0X40000033
R4=0X40000044
MEM DATA(LSBYTE)
4000000C 33
40000008 22
40000004 11
40000000
R0=0X4000000C
STMDA r0!, {r1-r3}
R0=0X4000000C
R1=0X00000011
R2=0X00000022
R3=0X40000033
R4=0X40000044
MEM DATA(LSBYTE)
4000000C 33
40000008 22
40000004 11
40000000
R0=0X40000000
STMDB r0!, {r1-r3}
R0=0X4000000C
R1=0X00000011
R2=0X00000022
R3=0X40000033
R4=0X40000044
MEM DATA(LSBYTE)
4000000C
40000008 33
40000004 22
40000000 11
R0=0X40000000
Addressing Description Start address End address Rn!
mode
STMIB LDMDA
STMDA LDMIB
STMDB LDMIA
BLOCK MOVE
AREA Rotate, CODE, READONLY
ENTRY
MOV R0,#08 ;NO OF WORDS TO MOVED
LDR R9,=0X40000000
LDR R10,=0X400000C0
SUBS R0,#04
BGT LOOP
stop B stop ;stop program
BLOCK EXCHANGE
AREA Rotate, CODE, READONLY
ENTRY
MOV R0,#03
LDR R9,=0X40000000
LDR R10,=0X400000C0
MOV R2,#04
SUBS R0,#01
BGT LOOP1
stop B stop
Stack Operations
• uses the load-store multiple instructions
• The pop operation (removing data from a
stack) uses a load multiple instruction
• the push operation (placing data onto the
stack) uses a store multiple instruction
• the stack will grow up or down in memory.
• A stack is either ascending (A) or descending
(D)
• Ascending stacks grow towards higher
memory addresses
• descending stacks grow towards lower
memory addresses.
• Full stack (F), the stack pointer sp points to an
address that is the last used or full location
(i.e., sp points to the last item on the stack).
• empty stack (E) the sp points to an address
that is the first unused or empty location (i.e.,
it points after the last item on the stack).
Addressing Description Pop = LDM Push = STM
mode
FA full LDMFA LDMDA STMFA STMIB
ascending
FD full LDMFD LDMIA STMFD STMDB
descending