all arm programmes
TRANSCRIPT
![Page 1: All ARM Programmes](https://reader033.vdocuments.mx/reader033/viewer/2022042700/5571fac84979599169931991/html5/thumbnails/1.jpg)
All ARM Programmes
Dr. Sanjay Sharma Manu Prakash
Associate Prof. M.E. Student
Thapar University Thapar University
![Page 2: All ARM Programmes](https://reader033.vdocuments.mx/reader033/viewer/2022042700/5571fac84979599169931991/html5/thumbnails/2.jpg)
Aim 1: Add two 32-bit numbers
;Following programmes are tested in Keil µVision V4.14.4.0
AREA ADDITION, CODE, READONLY
ENTRY
LDR R1, =&ABCDEFAB
LDR R2, =&60000001
MOV R3, #0X0
ADDS R3, R2, R1 ;Result
ADC R4, R4, #0X0 ;Carry if any
END
![Page 3: All ARM Programmes](https://reader033.vdocuments.mx/reader033/viewer/2022042700/5571fac84979599169931991/html5/thumbnails/3.jpg)
Aim2: To find greatest number from given table in ARM7-TDMI.
AREA GREATEST, CODE, READONLY
ENTRY
ADD R0, R0, #MYDATA
MOV R1, #10
LDR R2, [R0], #4
LOOP LDR R3, [R0], #4
CMP R3, R2
BLT NEXT
MOV R2, R3 ;R2 contains greatest no.
NEXT SUBS R1, R1, #1
CMP R1, #0
BNE LOOP
MYDATA DCD 5, 3, 4, 5, 2, 1, 6, 12, 0, 20 ;Note decimal data
END
![Page 4: All ARM Programmes](https://reader033.vdocuments.mx/reader033/viewer/2022042700/5571fac84979599169931991/html5/thumbnails/4.jpg)
Aim3 : To find smallest number from given table
AREA SMALLEST, CODE, READONLY
ENTRY
ADD R0, R0, #MYDATA
MOV R1, #10
LDR R3, [R0], #4
LOOP LDR R2, [R0], #4
CMP R3, R2
BLT NEXT
MOV R3, R2 ;R3 contains smallest no.
NEXT SUBS R1, R1, #1
CMP R1, #0
BNE LOOP
MYDATA DCD 5, 3, 4, 5, 2, 1, 6, 12, -1, 20 ;-1 in hex is 0xFFFFFFFF
END
![Page 5: All ARM Programmes](https://reader033.vdocuments.mx/reader033/viewer/2022042700/5571fac84979599169931991/html5/thumbnails/5.jpg)
AIM4: To count negative numbers in given table.
AREA NEGATIVENO, CODE, READONLY
ENTRY
count equ 8
mov r4, #count ;loop count
add r1,r1, #table ;table address
orr r2, r2, r2 ;clear r2
loop sub r4, r4, #1
ldr r0, [r1], #0x04
movs r5, r0 ;N flag set if neg.
bpl next
add r3, r3, #0x01 ;r3 counts neg. no.
next cmp r2, r4
bne loop
table DCD 20,-20,25,12,00,15,-1,3
ALIGN
END
![Page 6: All ARM Programmes](https://reader033.vdocuments.mx/reader033/viewer/2022042700/5571fac84979599169931991/html5/thumbnails/6.jpg)
AIM5: to copy block of memory using LDMIA/STMIA instructions
;Preconfigurations
;Project-> Options for target -> Linker Tab -> remove R/W base address if any
COUNT EQU 4 ;to count loop
AREA MEMCPY, CODE, READONLY
ENTRY
ADD R0, R0, #SOURCE_DATA ;R0<-address of source
ADD R2, R2, #DEST_DATA ;R2<-address of destination
MOV R1, #COUNT
COPY LDMIA R0!, {R3, R4} ;R3<-[R0], R4<-[R0+4}
STMIA R2!, {R3, R4} ;R3->[R2], R4->[R2+4}
SUBS R1, R1, #1
CMP R1, #0
BNE COPY
AREA MEM2, DATA, READWRITE
SOURCE_DATA DCD 5, 3, 4, 5, 2, 1, 6, 12
DEST_DATA DCD 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;as many 0's as req. mem. size
END
;memory in red is read by cpu
;memory in green is write by cpu
![Page 7: All ARM Programmes](https://reader033.vdocuments.mx/reader033/viewer/2022042700/5571fac84979599169931991/html5/thumbnails/7.jpg)
AIM: TO FIND FACTORIAL OF ONE DIGIT NUMBER
AREA Factorial_Program, CODE
ENTRY
MOV R1, #4 ; Number whose fact is to be found
MOV R2, #1 ; Initialize R2=1
LOOP
CMP R1, #0 ; Compare R1 with 0.
MULNE R2, R1, R2 ; R2 = R2 * R1 iff R1 != 0
SUB R1,R1,#1 ; R1=R1-1
CMP R1,#0 ; Compare if R1=0
BNE LOOP ; If R1!=0, branch to LOOP
END ;R2 Contains result
![Page 8: All ARM Programmes](https://reader033.vdocuments.mx/reader033/viewer/2022042700/5571fac84979599169931991/html5/thumbnails/8.jpg)
AIM: To find factorial using lookup table.
AREA FACTORIAL, CODE, READONLY
ENTRY
COUNT EQU 5
ADD R0, R0, #SOURCE_DATA ;source address
ADD R1, R1, #LOOKUP_TABLE ;lookup table address
ADD R2, R2, #ANS ;address of answer
MOV R3, #COUNT ;loop count
LOOP LDR R4, [R0],#4
LSL R4, 2 ;mul. R4 BY 4
LDR R5, [R1, R4] ;r4 used for offset
STR R5, [R2], #4 ;r5 contains answer
SUBS R3, #1
BPL LOOP
SOURCE_DATA DCD 5, 3, 4, 2, 9 ;0x28 start address
LOOKUP_TABLE DCD 1,1,2,6,24,120,720,5040,40320,362880 ;0x39 start address
ALIGN
AREA MEM2, DATA, READWRITE
ANS DCD 0,0,0,0,0 ;0x64 start address
END
;red indicate value read from memory
;blue indicate value written on memory
;memory is showing decimal equivalent
![Page 9: All ARM Programmes](https://reader033.vdocuments.mx/reader033/viewer/2022042700/5571fac84979599169931991/html5/thumbnails/9.jpg)
Aim7: To read/write from/to stack, using STMFA/LDMFA
AREA stacktest, CODE, READONLY
ENTRY
ADD R13, R13, #mystack ;SP
MOV R1, #0xAA ;to be stacked
MOV R2, #0xBB ;to be stacked
MOV R3, #0xCC ;to be stacked
STMFA R13!, {R1-R3} ;Full Ascending
LDMFA R13!, {R4-R6} ;Full Ascending
AREA stack, DATA, READWRITE
mystack DCD 0,0,0,0,0,0,0,0,0,0
END
;green memory indicates read before write
![Page 10: All ARM Programmes](https://reader033.vdocuments.mx/reader033/viewer/2022042700/5571fac84979599169931991/html5/thumbnails/10.jpg)
AIM: BUBBLE SORT 12 NUMBERS GIVEN IN MEMORY
AREA BUBBLESORT, CODE, READONLY
ENTRY
MOV R7, #11 ;outer LOOP2
LOOP2 MOV R1, #11 ;inner LOOP
EOR R0, R0, R0
ADD R0, R0, #DATA1 ;pointer initialization
LOOP LDMIA R0, {R3, R4} ;get 2 values
CMP R3, R4
BLT NEXT
MOV R6, R3 ;exchange
MOV R3, R4 ;exchange
MOV R4, R6 ;exchange
STMIA R0, {R3, R4}
NEXT ADD R0, R0, #4 ;increment pointer
SUB R1, R1, #1
CMP R1, #0
BNE LOOP
SUB R7, R7, #1
CMP R7, #0
BNE LOOP2
AREA MYDATA, DATA, READWRITE
DATA1 DCD 5, 3, 4, 7, 2, 6, 1, 12, 9, 11, 10, 8
END
![Page 11: All ARM Programmes](https://reader033.vdocuments.mx/reader033/viewer/2022042700/5571fac84979599169931991/html5/thumbnails/11.jpg)
;Memory before sorting
;Memory after sorting
![Page 12: All ARM Programmes](https://reader033.vdocuments.mx/reader033/viewer/2022042700/5571fac84979599169931991/html5/thumbnails/12.jpg)
AIM: TO DISASSEMBLE A GIVEN BYTE INTO 2 NIBBLES
AREA BYTE_DISASSEMBLE_PROGRAM, CODE
ENTRY
MOV R1, #&FE ; Number is in reg R1
AND R2, R1, #&0F ; Mask higher 4-bits.
AND R3, R1, #&F0 ; Mask lower 4 bits.
LSR R3, R3, #4 ; Transform the upper nibble of byte to lower one.
END
AIM: TO FIND 2’S COMPLEMENT
AREA TWOS_COMPLEMENT_PROGRAM, CODE
ENTRY
LDR R1, =&0A0A0A0A ; Number in reg R1
MVN R2, R1 ; R2 = negated (complemented) R1
ADD R2, R2, #1 ; R2 = R2 + 1
END
![Page 13: All ARM Programmes](https://reader033.vdocuments.mx/reader033/viewer/2022042700/5571fac84979599169931991/html5/thumbnails/13.jpg)
AIM: COUNT NUMBER OF ONES IN 8-BIT NUMBER
AREA Countones_PROGRAM, CODE
ENTRY
LDR R1, =&AF ; number is in R1.
MOV R2, #1 ; mask in reg R2
MOV R3, #0 ; Result in R3.
MOV R4, #32 ; Intialize Counter R4=32.
LOOP AND R5, R1, R2 ; mask all bits except LSB.
ADD R3, R3, R5 ; Add R3 to R5R3 = R3 + R5.
LSR R1, #1 ; Right Shift R3 by 1 bit.
SUBS R4, R4, #1 ; Decrement the counter
CMP R4,#0 ; Is counter R4=0?
BNE LOOP ; If R4 !=0 => branch to LOOP
END
![Page 14: All ARM Programmes](https://reader033.vdocuments.mx/reader033/viewer/2022042700/5571fac84979599169931991/html5/thumbnails/14.jpg)
AIM: TO DEVIDE A NUMBER USING REPETITIVE SUBTRACTION METHOD
AREA DEVIDE, CODE, READONLY
ENTRY
MOV R1, #32 ; Dividend.
MOV R2, #5 ; Divisor.
MOV R3, #0 ; Quotient.
MOV R4, #0 ; Remainder.
LOOP SUB R1, R1, R2 ; R1 = R1 - R2.
ADD R3, R3, #1
CMP R1, R2
MOVLT R4, R1
BGE LOOP
END
AIM: TO MULTIPLY 2 NUMBERS USING REPITITIVE ADDITION.
AREA MULTIPLY, CODE, READONLY
ENTRY
MOV R1, #32 ; NUMBER 1
MOV R2, #5 ; NUMBER2
MOV R3, R1
MOV R4, #0 ;FIRST 32 BITS
MOV R5, #0 ;SECOND 32 BITS
LOOP ADDS R4, R4, R2
ADC R5, R5, #0
SUB R3, R3,#1
CMP R3, #0
BNE LOOP
END