Arm Example
Arm Example
Arm Example
Examples-
### Addressing modes- examples
Given- Register r11 contains 4C , r3 contains 08 , r5 =01, r6=40h, r4=00000010h, carry flag=0
memory address [data] : 54 [AA], 50[BB], 4C[CC], 48[DD], 44[EE], 40[FF], 3C[01], 38[02], 34[45], 30[67]
( (registers update after given operations);registers use modified address wherever applicable)
LDR r2, [r11,# 8] ; EA= 54 ; r2← AA; r11 unchanged(4C) ; immediate
LDR r2, [r11,# -4] ; EA= 48 ; r2← DD , r11 unchanged(4C)
LDR r2, [r11,# -8]! ; EA= 44 ; r2← EE , r11← 44(changed) ;pre index
LDR r2, [r11,# 4] ! ; EA= 48 ; r2← DD , r11← 48(changed)
LDR r2, [r11],# -8 ; EA= 48 ;r 2← DD , r11← 40(changed) ; post index
LDR r2, [r11],# 4 ; EA= 40 ; r2← FF , r11← 44(changed)
LDR r2, [r11, -r5 LSL#3]! ; EA= 54 ; r2<-AA , r11←54(changed) ; scaled register r5 (00 00 00 08)
LDR r2, [r11, -r6 LSR#3]! ; EA= 4C ; r2<-CC , r11←4C(changed) ; r6 is right shift 3 bit, (00 00 00 08)
LDR r2, [r11], r4 RRX ; EA= 4C ; r2<-CC , r11←54(changed) ; r4 is right shift 1 bit, (00 00 00 08)
### to implement g = h+ A[2] ; data in 2nd element array A ,(3rd element from base of array A)
LDR r5 , [r3, #8] ; read indexed element in r5 from memory A which is represented by r3
ADD r1, r2, r5 ;
//if 8 (9th element) is the index number (starting from 0) of array A then
LDR r5 , [r3, #32] ; read indexed element , r3 is base register of array A
### IF-ELSE statement if (a < b) { a=10;} else a=100; (assume R0=a R2=b)
CMP R0, R1 ;set flags from operation R0-T1
BGE else ; a >=b
MOV R0, #10
BAL end
else: MOV R0, #100
end:
### FOR statement for ( i = 0 ; i < 10 ; i++) { sum+= i;} //(assume R0=i; R2=sum)
version I version II
SUB R0 , R0, R0 ; set i=0 SUB R0 , R0, R0
for: CMP R0, #10 ; test condition of for loop for: CMP R0, #10
ADDLT R2, R2,R0 ; for loop body BGE end
ADDLT R0,R0,#1 ; ADD R2, R2,R0
BLT for ADD R0, R0,#1
BLT for
end:
### to implement loop : while( save[i] == k) i+ = 1;
LOOP: ADD r12, r6, r3, LSL #2 ; r6 is base address of save[ ], r12 points to save[i] , r3 represents i
LDR r0, [r12, #0] ; read memory save[i]
CMP r0, r5 ;compare memory with k represented by r5
BNE EXIT ; jump if save[i] != k
ADD r3, r3, #1 ; i++
B LOOP
EXIT :
(run above code manually assuming SP=0x0100 and r0=2 before 2000 BL fact; )
### Find larger of two numbers
LDR R1, num1
LDR R2, num2
CMP R1, R2
BHI rest ; branch if R1 > R2
MOV R1, R2
rest: STR R1 , ans
SWI _exit ; OS call – return to OS
num1 DCD ; declare data
numb2 DCD
ans DCD
;
### Find length of a string
(string is terminated by carriage return (CR) )
LDR R1 , strng_addr ;load address of string
EOR R2, R2, R2 ; clear R2 to hold length
rep: LDRB R1,[R1], #1 ; read first byte in R1
CMP R1, #0D ; compare CR(0x0d in hex)
BEQ out1 ; CR found
ADD R1 , R1 , #1 ;increment count
B rep ; read next char
out1: STR R1,len ;store length in variable len
SWI _exit ; OS call
### stack ( multiload/store LDM/STM)
Given- Register r11 contains 44 ,
memory format -address [data] : 58 [99], 54 [AA], 50[BB], 4C[CC], 48[DD], 44[EE], 40[FF], 3C[01], 38[02], 34[45], 30[67]
(source register updates after given operations)
LDMIA r11, [r3,r5,r0] ; r0← EE, r3← DD,r5← CC; r11 unchanged(44) ;
LDMIA r11!, [r3,r5,r0] ; r0← EE, r3← DD,r5← CC; r11 points to address 50 ;
LDMDA r11!, [r3,r5,r0] ; r0← DD, r3← CC,r5← BB; r11 points to address 44 ;
LDMDB r11!, [r3,r5,r0] ; r0← 02, r3← 01,r5← FF ; r11 points to address 38 ;
LDMIB r11!, [r3,r5,r0] ; r0← 01, r3← FF,r5← EE ; r11 points to address 44 ;
LDMFD r13! , [r0-r2] ;r0 <-AA , r1 <-BB , r2 <-CC ; r13 =50 // POP , stack full, descending
(same as LDMIA)
STMFA r13! , [r0-r3] ;r0->48[AA] , r1->4C[BB] , r2->50[CC] , r3->54[DD] ; r13 =54 // PUSH , full stack , ascending
(same as STMIB)
LDMEA r13! , [r0-r2] ;r0 <-AA , r1 <-BB , r2 <-CC ; r13 =48 // POP , empty stack , ascending
(same as LDMDB)
PUSH/multistore POP/multiload
STMFD/ STMDB LDMFD/LDMIA
STMFA/ STMIB LDMFA/LDMDA
STMED/ STMDA LDMED/LDMIB
STMEA/ STMIA LDMEA/LDMDB