You are on page 1of 11

Experiment – 9

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.

Sections of a Start-Up File:


1. Interrupt Vector Table
2. Clock Selection and PLL Configuration
3. Peripheral Configuration
4. Stack Definition

Start-Up file: lpc2300.s


Mode_USR EQU 0x10
Mode_FIQ EQU 0x11
Mode_IRQ EQU 0x12
Mode_SVC EQU 0x13
Mode_ABT EQU 0x17
Mode_UND EQU 0x1B
Mode_SYS EQU 0x1F
I_Bit EQU 0x80 ; when I bit is set, IRQ is disabled
F_Bit EQU 0x40 ; when F bit is set, FIQ is disabled
; Startup Code must be linked first at Address at which it expects to run.
AREA RESET, CODE, READONLY
ARM
; Exception Vectors
; Mapped to Address 0
; Absolute addressing mode must be used
; Dummy Handlers are implemented as infinite loops which can be modified.
; Vectors
LDR PC, Reset_Addr
LDR PC, Undef_Addr
LDR PC, SWI_Addr
LDR PC, PAbt_Addr
LDR PC, DAbt_Addr
NOP
; Reserved Vector ;
LDR PC, IRQ_Addr
LDR PC, [PC, #-0x0120] ; Vector from VicVectAddr
LDR PC, FIQ_Addr
Reset_Addr DCD Reset_Handler
Undef_Addr DCD Undef_Handler
SWI_Addr DCD SWI_Handler
PAbt_Addr DCD PAbt_Handler
DAbt_Addr DCD DAbt_Handler
DCD 0
; Reserved Address
IRQ_Addr DCD IRQ_Handler
FIQ_Addr DCD FIQ_Handler
Undef_Handler B Undef_Handler
SWI_Handler B SWI_Handler
PAbt_Handler B PAbt_Handler
DAbt_Handler B DAbt_Handler
IRQ_Handler B IRQ_Handler
FIQ_Handler B FIQ_Handler
; Reset Handler
EXPORT Reset_Handler
; Reset_Handler
; Enter the C code
IMPORT _main
LDR R0, =__main
BX R0 // branch to main program
IF :DEF:__MICROLIB
EXPORT _heap_base
EXPORT _heap_limit
ELSE ; User Initial Stack & Heap
AREA |.text|, CODE, READONLY
IMPORT _use_two_region_memory
EXPORT _user_initial_stackheap
__user_initial_stackheap
LDR R0, = Heap_Mem
LDR R1, = (Stack_Mem + USR_Stack_Size)
LDR R2, = (Heap_Mem + Heap_Size)
LDR R3, = Stack_Mem
BX LR
ENDIF
END

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.

AREA TEST, CODE, READONLY


EXPORT START
START
MOV R3,#10 ;Counter for number of elements
MOV R0,#0X40000000 ;Starting address of the Source Array
MOV R2,#0X40000030 ;Starting address of the Destination Array
LOOP
LDRB R1,[R0] ;Load the element from source
STRB R1,[R2] ;Store the element at destination
ADD R0,R0,#0X01 ;Point to next element in source array
ADD R2,R2,#0X01 ;Point to next element in destination array
SUB R3,R3,#0X01 ;Reduce the counter by 1
CMP R3,#0 ;Compare counter with 0
BNE LOOP ;If not 0 then jump to "LOOP"
END
OUTPUT:

Fig. 9.1.1 Memory Window before execution of the program

Fig. 9.1.2 Memory Window after execution of the program

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:

Fig. 9.2.1 Memory Window before execution of the program

Fig. 9.2.2 Memory Window after execution of the program


As can be seen from fig. 9.2.2, the count of even numbers in the array is 5.
(b) To perform an array processing in ARM assembly language programming.

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)

Fig. 9.5.2 Memory Window after execution of the program


Figure 9.5.2 shows the count of even numbers (stored at address 0x40000020) and odd
numbers (stored at address 0x40000021).
(2) Find the count of 1’s from an array of size 5 bytes.
AREA TEST, CODE, READONLY
EXPORT START
START
MOV R3,#5 ;Counter for number of elements in array
MOV R4,#00 ;Counter for number of 1's
MOV R0,#0X40000000 ;Starting address of the source arrray
MOV R2,#0X40000010 ;Address to store the count of 1
LOOP
LDRB R1,[R0] ;Load the test element
CMP R1,#0X00000001 ;Compare the test element with 1
ADDEQ R4,R4,#0X01 ;If [R1] == 1 then increase the count R4
ADD R0,R0,#0X01 ;Point to next element
SUB R3,R3,#0X01 ;Reduce the counter for loop
CMP R3,#0 ;if R3 != 0 then jump to label LOOP
BNE LOOP
STRB R4, [R2] ;store the count of 1's at address pointed by R2
END

OUTPUT:

Fig. 9.6.1 Memory Window before execution of the program (Array of 5 elements)

Fig. 9.6.2 Memory Window after execution of the program


From the figure 9.6.2, it can seen that the number of 1’s in the array are 2(stored at
address 0x40000010).

You might also like