Professional Documents
Culture Documents
Array Processing
Objectives:
(a)To study start-up file for LPC2300 and programming of ARM assembly language.
Theory:
A start-up code performs stack initialization and the microcontroller setup, before an
arm microcontroller can execute the main program. The start-up file LPC2300.s code is
executed after CPU reset.
The file LPC2300.s is an assembler module provided by Keil. As its name implies, the
start-up code is located to run from the reset vector. It provides the exception vector
table as well as initializing the stack pointer for the different operating modes. It also
initializes some of the on-chip system peripherals and the on-chip RAM before it jumps
to the main function in c code. The start-up code will vary depending on which arm7
device you are using and which compiler you are using, so for your own project it is
important to make sure that you are using the correct start-up file.
First of all, the start-up provides the exception Vector table as shown below. The vector
table is located at 0x00000000 and provides a jump to interrupt service routines (ISR)
on each vector to ensure that the full Address range of the processor is available. The
area command is used by the linker to the Place the vector table at the correct start
address. For a single chip use this is always 0x00000000, however if you are using the
external bus and want to boot from external memory, the vector table must be located at
0x80000000.
PROCEDURE:
In built ARM start-up file (lpc2300.s) is written for high level language applications, i.e.,
for C Programming Language. To write the low-level language program (assembly
program), it is necessary to make certain modifications in a start-up file.
Steps are mentioned below to write an ARM assembly language program / application.
1. In a start-up file, replace actual code from “enter the c code module” to end as shown
below.
IMPORT START
LDR R0, =START
BX R0
END
Here, “start” is a label to a block of area where an assembly language program /
applications code exists.
2. An assembly program / application code will start with an AREA directive, which
instructs the assembler to assemble a new code or data section. Sections are
independent, named, indivisible chunks of code or data that are manipulated by the
linker.
“TEST” is a name of a block where program code resides, instead of “TEST”, any
name can be given.
CODE means it is program code.
READONLY means the given code area is read-only.
“START” is the starting point of the program.
“END” is the end point directive of a program.
SAMPLE PROGRAM:
Copy an array of size 10 bytes to another memory location.
MODIFICATION:
Modify the above code to find the count of even numbers from an array of size 10 bytes.
AREA TEST, CODE, READONLY
EXPORT START
START
MOV R3,#10 ;Counter for number of elements in the array
MOV R4,#00 ;Counter for number of even numbers
MOV R0,#0X40000000
MOV R2,#0X40000010 ;Address to store the count of even numbers
LOOP
LDRB R1,[R0] ;Load the test element
AND R1,#0X00000001 ;Extract the LSB of the test element
CMP R1,#0X00000000 ;Compare the result with 0x0 to check for even
number
ADDEQ R4,R4,#0X01 ;If even then increase the counter
ADD R0,R0,#0X01 ;Point to next element
SUB R3,R3,#0X01 ;Decrease the counter
CMP R3,#0 ;Compare with 0
BNE LOOP ;If not 0 then jump to label
STRB R4,[R2] ;Store the total count at the address pointed by R2
END
OUTPUT:
PROCEDURE:
In built ARM start-up file (lpc2300.s) is written for high level language applications, i.e.,
for C Programming Language. To write the low-level language program (assembly
program), it is necessary to make certain modifications in a start-up file.
Steps are mentioned below to write ARM assembly language program / application:
1. In a start-up file, replace actual code from “enter the c code module” to end as shown
below.
IMPORT START
LDR R0, =START
BX R0
END
Here, “start” is a label to a block of area where an assembly language program /
application’s code exists.
2. An assembly program / application code will start with AREA directive, which instructs
the assembler to assemble a new code or data section. Sections are independent,
named, indivisible chunks of code or data that are manipulated by the linker.
“TEST” is a name of block where program code resides, instead of “TEST”, any name
can be given.
CODE means it is program code.
READONLY means given code area is read-only.
“START” is starting point of program.
“END” is end point directive of a program.
SAMPLE PROGRAM:
Sort an array in ascending order
AREA TEST, CODE, READONLY
EXPORT START
START
MOV R0,#0X40000000 ;starting address of array
MOV R1,R0 ;load the address
MOV R4,#4 ;counter
MOV R5,#4 ;counter
LOOP2
LDRB R2,[R0] ;load the first element
LOOP1
ADD R1,R1,#1 ;point to next element (byte data)
LDRB R3,[R1] ;load the next element
CMP R2,R3 ;compare both elements
BLT HERE ;if R2<R3 then branch
MOV R6,R2 ;exchange if R2>R3
MOV R2,R3 ;for ascending order
MOV R3,R6
STRB R2,[R0] ;store the exchanged values
STRB R3,[R1]
HERE
SUB R4,R4,#1 ;decrement the counter
CMP R4,#0 ;loop until value of R4=0
BNE LOOP1 ;branch if R4 != 0
ADD R0,R0,#1 ;Point to next element
MOV R1,R0
SUB R5,R5,#1 ;decrement the counter
MOV R4,R5
CMP R5,#0 ;loop until value of R5=0
BNE LOOP2 ;branch if R5 != 0
END
OUTPUT:
Fig. 9.3.1 Memory Window before execution of the program (Ascending Order)
Fig. 9.3.2 Memory Window after execution of the program (Ascending order)
MODIFICATION:
Sort an array in descending order
AREA TEST, CODE, READONLY
EXPORT START
START
MOV R0,#0X40000000 ;starting address of array
MOV R1,R0 ;load the address
MOV R4,#4 ;counter
MOV R5,#4 ;counter
LOOP2
LDRB R2,[R0] ;load the first element
LOOP1
ADD R1,R1,#1 ;point to next element (byte data)
LDRB R3,[R1] ;load the next element
CMP R2,R3 ;compare both elements
BGT HERE ;if R2>R3 then branch
MOV R6,R2 ;exchange if R2<R3
MOV R2,R3 ;for descending order
MOV R3,R6
STRB R2,[R0] ;store the exchanged values
STRB R3,[R1]
HERE
SUB R4,R4,#1 ;decrement the counter
CMP R4,#0 ;loop until value of R4=0
BNE LOOP1 ;branch if R4 != 0
ADD R0,R0,#1 ;Point to next element
MOV R1,R0
SUB R5,R5,#1 ;decrement the counter
MOV R4,R5
CMP R5,#0 ;loop until value of R5=0
BNE LOOP2 ;branch if R5 != 0
END
OUTPUT:
Fig. 9.4.1 Memory Window before execution of the program (Descending Order)
Fig. 9.4.2 Memory Window after execution of the program (Descending Order)
CONCLUSION:
From this experiment, we learnt how to program LPC 2368 using assembly
language. We also understood the importance of startup file and learnt how to use it for
execution of a simple program. The startup file contains address of various interrupts
and exceptions.
EXERCISE:
(1) Find the count of even and odd numbers of from an array of size 20 bytes.
AREA TEST, CODE, READONLY
EXPORT START
START
MOV R3,#20 ;Counter for number of elements in the array
MOV R4,#00 ;Counter for number of even numbers
MOV R0,#0X40000000
MOV R2,#0X40000020 ;Address to store the count of even and numbers
LOOP
LDRB R1,[R0] ;Load the test element
AND R1,#0X00000001 ;Extract the LSB of the test element
CMP R1,#0X00000000 ;Compare the result with 0x0 to check for even
number
ADDEQ R4,R4,#0X01 ;If even then increase the counter
ADD R0,R0,#0X01 ;Point to next element
SUB R3,R3,#0X01 ;Decrease the counter
CMP R3,#0 ;Compare with 0
BNE LOOP ;If not 0 then jump to label
STRB R4,[R2] ;Store the total count of even numbers at the address
pointed by R2
ADD R2,R2,#0x01 ;Address to store count of odd numbers
RSB R4,R4,#20 ;Odd numbers = number of elements - even number
STRB R4,[R2] ;Store the count of odd numbers
END
OUTPUT:
Fig. 9.5.1 Memory Window before execution of the program (Array of 20 elements)
OUTPUT:
Fig. 9.6.1 Memory Window before execution of the program (Array of 5 elements)