Sei sulla pagina 1di 4

ARM assembly examples-

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,r3] ; EA= 4C ; r2← CC , r11 unchanged(44) ; register


LDR r2, [r11,-r3] ; EA= 3C ; r2← 01 , r11 unchanged(44)
LDR r2, [r11,-r3]! ; EA= 3C ; r2← 01, r11← 3C(changed)
LDR r2, [r11,r3] ! ; EA= 44 ; r2← EE , r11← 44(changed)
LDR r2, [r11],r3,LSL #1 ; EA= 44 ; r2← EE , r11← 54(changed) ; r3 is left shift 1 bit, (00 00 00 10)
LDR r2, [r11],r3 ; EA= 54 ; r2←AA; r11← 5C(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)

### some data manipulation examples


ADD r0 , r1, r2 ; r0= r1+r2
SUB r0,r3,r0 ; r0=r3 – r0;
MOV R1, R1, LSL #02 ;r1=4r1 //shift R1 two bits left,(multiply R1 by four)
MOV R1, #40,26 ; load R1=0x1000 , (rotate right 00 00 00 40 by 26 bits)
ADDS R1, R2, R3 ; r1=r2+r3 //add and set flags
ADDC R1, R4,R5 ; r1=r4+r5+c //add with carry
MOV R1, R2, LSR #16 ; shift r2 right 16 times, then move the result to r1
BIC R1, R2 , R3 ; R1= R2 AND (NOT R3),example R2=0xFC,R3=0x04 ; result in R1=F8
MVN R0, #0 ;Set R0 to -1 (FF FF FF FF) 1's complement of right operand (sum of R0 +NOT R0 = -1 or FFFFFFFF)
ADD R0,R0,R0,LSL#3 ;Multiple R0 by 9 (9r0=r0+8r0)

### to implement f = (g+h) – (i+j); (using assembly variables)


ADD r0, r2,r3 ; ro = g + h
ADD r1, r4 , r5 ; r1 = i + j
SUB r6 , r0, r1 ; f = r0 – r1

### 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

### to implement A[12] = h+ A[8] ;


LDR r5 , [r3, #32] ; read indexed element(4*8) , r3 base register representing array A[0]
ADD r5, r2, r5 ; r2 represents h
STR r5, [r3,#48] ;store at 12th position(4*12) in array A

### loop example


LDR R1, count ;R1 initialize with count>0
CMP R1, #0 ; if count value is 0, set flags from operation R1-0
BEQ end ; branch if complete
rep: SUBS R1 , R1 #01 ; decrement count by one and set flag
BNE rep ; repeat ;if Z=0 , repeat
end:
### to implement if (i==j) f= g+h ; else f = g – h;
(a) without conditional statements
CMP r3 , r4 ; i in r3 , j in r4, set flags from operation R3-T4
BNE els
ADD r0 , r1, r2
B end
els: SUB r0, r1,r2
end:

(b) using conditional statements


CMP r3, r4 ;set flags from operation R3-T4
ADDEQ r0 , r1, r2
SUBNE r0, r1, r2;

### IF statement if(a==b){a=b+10;}


; r1 <- a; r2<- b
CMP R1,R2 ;set flags
ADDEQ R1,R2,#10

### 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 :

### to implement procedure


int leaf (int g, int g, int i, int j)
{int f;
f = (g+h) – (i + h)
return f;
}
### assembly code of
....assembly code…..
2000 BL leaf ; save return address in lr(r14)
....
leaf:
SUB sp , sp, #12 ; create three words space in stack(r13)
STR r6, [sp, #8] ; save content of registers in stack
STR r5 ,[sp,#4]
STR r4, [sp, #0]
ADD r5, r0, r1 ; g +h
ADD r6 , r2, r3 ;i +j
SUB r4, r5, r6 ;f
MOV r0, r4 ; return f in r0
LDR r4 , [sp , #0] ; restore r4 from stack, sp remains unchanged
LDR r5 , [sp , #4] ; restore r5
LDR r6 , [sp , #8] ; restore r6
ADD sp , sp, #12 ; adjust sp
MOV pc, lr ; return to calling programme restore sp

(may use LDM/STM for multiple register write/read in above example)


### to implement nested procedure call- example of factorial calculation
int fact( int n)
{if (n<1 ) return 1;
else return (n * fact (n-1) );}
....assembly code…..
2000 BL fact
… ; factorial value return by fact() in r0
fact:
SUB sp, sp, #8 ;
STR lr , [sp, #4] ; save return address // being nested call save r14 in stack
STR r0, [sp, #0] ; save argument n
CMP r0 , #1 ; compare n to 1
BGE L1 ; if n> = 1 go to L1
MOV r0 , #1 ;n<1
ADD sp, sp, #8 ; adjust stack
MOV pc , lr ; return to calling
L1 : SUB r0 , r0, #1 ; since n>1 , get n-1
BL fact ; call fact recursively with n-1
MOV r12, r0 ; save return value
LDR r0 , [sp, #0] ;restore previous n
LDR lr, [sp, #4] ; restore return address
ADD sp , sp, #8 ; adjust stack
MUL r0, r0, r12 ; return n * fact(n-1)
MOV pc, lr ; return to calling programme

(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 ;

Given- Register r13(sp) contains 54 , r0= AA , r1=BB , r2=CC, r3=DD


(full stack – TOS points to last data in stack, empty stack – TOS points to empty space in stack)
STMFD r13! , [r0-r3] ;r3->50[DD] , r2->4C[CC] , r1->48[BB] , r0->44[AA] ; r13 =44 // PUSH , full stack , descending
(same as STMDB)

LDMFD r13! , [r0-r2] ;r0 <-AA , r1 <-BB , r2 <-CC ; r13 =50 // POP , stack full, descending
(same as LDMIA)

Given- Register r13 contains 34 , r0= AA , r1=BB , r2=CC, r3=DD


STMEA r13! , [r0-r3] ;r0->34[AA] , r1->38[BB] , r2->3C[CC] , r3->40[DD] ; r13 =44 // PUSH , empty stack, ascending
(same as STMIA)

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

Potrebbero piacerti anche