BCS402_Codes_Quick Note_FINAL
BCS402_Codes_Quick Note_FINAL
EXPERIMENTS: 1 to 11
Conduct the following experiments by writing program using ARM7TDMI/LPC2148 usingan
evaluation board/simulator and the required software tool.
Experiment-1 Using Keil software, observe the various Registers, Dump, CPSR, with a simple
MODULE–1 Assembly Language Programs (ALP).
ARM - REGISTERS
Observing the Various Registers in ARM using Keil µVision 4
GPR’s: General Purpose Registers (R0 to R10)
GPR’s /Multipurpose Registers (R11/FP: Frame Pointer &
R12/IP: Intra Procedural Call
# Alias Purpose
R0 - GPR: General Purpose Registers
R1 - GPR: General Purpose Registers
R2 - GPR: General Purpose Registers
R3 - GPR: General Purpose Registers
R4 - GPR: General Purpose Registers
R5 - GPR: General Purpose Registers
R6 - GPR: General Purpose Registers
R7 - Holds Syscall Number
R8 - GPR: General Purpose Registers
R9 - GPR: General Purpose Registers
R10 - GPR: General Purpose Registers
R11 FP Frame Pointer
R12 IP Intra Procedural Call
R13 SP Stack Pointer
R14 LR Link Register
R15 PC Program Counter / Instruction Pointer
CPSR - Current Program Status Register
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES
RV Institute of Technology & Management®
Department of Computer Science & Engineering
R0-R12: can be used during common operations to store temporary values, pointers (locations to
memory), etc. R0, for example, can be referred as accumulator during the arithmetic operations or for
storing the result of a previously called function. R7 becomes useful while working with syscalls as it
stores the syscall number and R11 helps us to keep track of boundaries on the stack serving as the frame
pointer (will be covered later). Moreover, the function calling convention on ARM specifies that the
first four arguments of a function are stored in the registers r0-r3.
R13: SP (Stack Pointer). The Stack Pointer points to the top of the stack. The stack is an area of
memory used for function-specific storage, which is reclaimed when the function returns. The stack
pointer is therefore used for allocating space on the stack, by subtracting the value (in bytes) we want to
allocate from the stack pointer. In other words, if we want to allocate a 32 bit value, we subtract 4 from
the stack pointer.
R14: LR (Link Register). When a function call is made, the Link Register gets updated with a memory
address referencing the next instruction where the function was initiated from. Doing this allows the
program return to the “parent” function that initiated the “child” function call after the “child” function
is finished.
R15: PC (Program Counter). The Program Counter is automatically incremented by the size of the
instruction executed. This size is always 4 bytes in ARM state and 2 bytes in THUMB mode. When a
branch instruction is being executed, the PC holds the destination address. During execution, PC stores
the address of the current instruction plus 8 (two ARM instructions) in ARM state, and the current
instruction plus 4 (two Thumb instructions) in Thumb(v1) state. This is different from x86 where PC
always points to the next instruction to be executed.
N Z C V Q J GE E A I F T M
Greater
Overflow Flag
Negative Flag
Abort disable
Processor
IRQ disable
FIQ disable
Endianness
Carry Flag
Underflow
Zero Flag
than Thumb
Jazelle
Mode
Equal
(Privilige
for
Mode)
SIMD
N - Negative Flag Set when the result of the operation was Negative.
Z - Zero Flag Set when the result of the operation was Zero.
C - Carry Flag Set when the operation resulted in a Carry.
V - Overflow Flag Set when the operation caused oVerflow.
ARM can operate either in little endian, or big endian. This bit is set to 0
E - Endian-bit
for little endian, or 1 for big endian mode.
I IRQ – Disable
F FRQ – Disable
This bit is set if you are in Thumb state and is disabled when you are in
T - Thumb-bit
ARM state.
M - Mode-bits These bits specify the current privilege mode (USR, SVC, etc.).
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES
RV Institute of Technology & Management®
Department of Computer Science & Engineering
Before proceeding to the Execution of Sample Program: Lets know about below details
DATA TYPES
This is part of the ARM Assembly Language Programming Basics, covering data types
Similar to high level languages, ARM supports operations on different data types.
ENDIANNESS
There are two basic ways of viewing bytes in memory: Little-Endian (LE) or Big-Endian (BE). The difference
is the byte-order in which each byte of an object is stored in memory. On little-endian machines like Intel x86,
the Least-significant-byte / LSB is stored at the lowest address (the address closest to zero). On big-endian
machines the Most-significant-byte / MSB is stored at the lowest address. The ARM architecture was little-
endian before version 3, since then it is bi-endian, which means that it features a setting which allows for
switchable endianness. On ARMv6 for example, instructions are fixed little-endian and data accesses can be
either little-endian or big-endian as controlled by bit 9, the E bit, of the Program Status Register (CPSR).
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES
RV Institute of Technology & Management®
Department of Computer Science & Engineering
Here are some examples of how these data types can be used with the instructions Load & Store:
LOAD INSTRUCTION DATA TYPES STORE INSTRUCTION DATA TYPES
ldr = Load Word str = Store Word
ldrh = Load unsigned Half Word strh = Store unsigned Half Word
ldrsh = Load signed Half Word strsh = Store signed Half Word
ldrb = Load unsigned Byte strb = Store unsigned Byte
ldrsb = Load signed Bytes strsb = Store signed Byte
Experiment-1 Using Keil software, observe the various Registers, Dump, CPSR, with a simple Assembly
MODULE–1 Language Programs (ALP).
Sample & simple Assembly Language Programs (ALP) to observe the movement of date in various Registers
GPR’s & CPSR
here B here
END
CPSR / Current Program Status SPSR / Saved Program Status Register
Register CPSR consists of 8-Flags
Default filling of Flags
RESET=0
N,Z,C & V
N = Negative Flag
Z = Zero Flag
C = Carry Flag
V = Overflow Flag
SET=1
F&I
F = First Interrupt Flag
I = Interrupt Flag
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES
RV Institute of Technology & Management®
Department of Computer Science & Engineering
Experiment-1 Using Keil software, observe the various Registers, Dump, CPSR, with a simple Assembly
MODULE–1 Language Programs (ALP).
Sample & simple Assembly Language Programs (ALP) to perform Dump operation or just a Dumping process.
; SIMPLE ASSESMBLY LANGUAGE PROGRAM/ALP TO MULTIPLY GPR’s / General Purpose Registers
;(Z = X*Y) USING MEMORY Locations
AREA Program, CODE, READONLY
ENTRY
LDR R0, MEMORY
;load MEMORY LOCATION ADDRESS/MLA 0x40000000 to R0
LDRH R1, [R0]
;X=0004 load 16-Bits First number, MLA 0x40000000 & 01 to R1
LDRH R2, [R0,#2]
;Y=0003 load 16-Bits Second number, MLA 0x40000002 & 03 to R2
MUL R3, R2, R1
;Perform MULTIPLICATION Z=X*Y store the result in R3
STR R3, [R0, #4]
;move and store the 32-Bits result Z to MLA 0x40000004 on wards
here B here
;Unconditional Branching LABEL name is here
;all done conclude here
END
CPSR / Current Program Status Register SPSR / Saved Program Status Register
CPSR consists of 8-Flags
Default filling of Flags
RESET=0
N,Z,C & V
N = Negative Flag
Z = Zero Flag
C = Carry Flag
V = Overflow Flag
SET=1
F&I
F = First Interrupt Flag
I = Interrupt Flag
Dump: The process of storing/pushing inside the Memory & retrieving/popping up the value from the
Memory is called Dump or just a Dumping process.
32-Bits output Z=0000000C
Dumping the 16-Bits input X=0004 Dumping the 16-Bits input Y=0003 (Z = X*Y) 12 = 4 * 3
Inside the MEMORY LOCATION Inside the MEMORY LOCATION C = 12 in Decimal
Inside the MEMORY LOCATION
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES
RV Institute of Technology & Management®
Department of Computer Science & Engineering
Experiment-2 Develop and simulate ARM ALP for Data Transfer, Arithmetic and Logical operations
MODULE–2 (Demonstrate with the help of a suitable program).
AREA MULTIPLY, CODE, READONLY OBSERVATOION OF
ENTRY ; Mark first instruction to execute OUTPUT INSIDE
GPR’s / General Purpose Registers
START FOR ARTHEMATIC OPERATONS
MOV R1, #6400
ADDITION Y=A+B inside R3
; 1900 in HEX STORE FIRST NUMBER/A IN R1
MOV R2, #3200
; 0C80 in HEX STORE SECOND NUMBER/B IN R2
HERE B HERE;
END ; Mark end of file
OBSERVATOION OF LOGICAL OPERATION OUTPUT’S
AND OPERATION OR OPERATION EX-OR OPERATION
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES
RV Institute of Technology & Management®
Department of Computer Science & Engineering
Experiment-3
Develop an ALP to multiply two 16-bit binary numbers.
MODULE–2
ENTRY
; Mark first instruction to execute
START
here b here
;Unconditional Branching LABEL name is here
;all done conclude here
END
; Mark end of file
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES
RV Institute of Technology & Management®
Department of Computer Science & Engineering
Experiment-4
Develop an ALP to find the sum of first 10 integer numbers.
MODULE–2
OBSERVATOION OF
AREA SUM, CODE, READONLY OUTPUT INSIDE
GPR’s / General Purpose Registers
ENTRY
loop
ADD R2, R2, R1
; add the content of R1 with result at R2
SUBS R1, #0x01
; Decrement R1 by 1
BNE loop
; repeat till r1 becomes 0
back B back
; Unconditional Branching LABEL name is back
END
55 in
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 =
DECIMAL
37 in
HEXA-DECIMAL
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES
RV Institute of Technology & Management®
Department of Computer Science & Engineering
CMP R2, R4
; COMPARE NUMBERS
R5 GPR is used as a counter
BHI LOOP1 Execute the code (Single Step)
; IF THE FIRST NUMBER IS > THEN GO TO LOOP1
Until R5 becomes zero
MOV R2, R4
; IF THE FIRST NUMBER IS < THEN MOV CONTENT R4 TO R2 Element AAAAAAAA is
LARGEST in the ARRAY
LOOP1 Should load to R2 Finally
SUBS R5, R5, #1
; DECREMENTCOUNTER
CMP R5, #0
; COMPARE COUNTER TO 0
BNE LOOP
; LOOP BACK TILL ARRAY ENDS
LDR R4, =RESULT
; LOADS THE ADDRESS OF RESULT
STR R2, [R4]
; STORES THE RESULT IN R2
NOP
back B back
; ARRAY OF 32-BIT NUMBERS (N=7)
VALUE1
DCD 0X44444444
DCD 0X22222222
DCD 0X11111111
DCD 0X33333333
DCD 0XAAAAAAAA
DCD 0X88888888
DCD 0X99999999
AREA DATA2, DATA, READWRITE
;TO STORE RESULT IN GIVEN ADDRESS
RESULT DCD 0X0
END ;Mark end of file
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES
RV Institute of Technology & Management®
Department of Computer Science & Engineering
CMP R2, R4
; COMPARE NUMBERS
R5 GPR is used as a counter
BLS LOOP1 Execute the code (Single Step)
; IF THE FIRST NUMBER IS > THEN GO TO LOOP1
Until R5 becomes zero
MOV R2, R4
; IF THE FIRST NUMBER IS < THEN MOV CONTENT R4 TO R2 Element 11111111 is
SMALLEST in the ARRAY
LOOP1 Should load to R2 Finally
SUBS R5, R5, #1
; DECREMENTCOUNTER
CMP R5, #0
; COMPARE COUNTER TO 0
BNE LOOP
; LOOP BACK TILL ARRAY ENDS
LDR R4, =RESULT
; LOADS THE ADDRESS OF RESULT
STR R2, [R4]
; STORES THE RESULT IN R2
NOP
back B back
; ARRAY OF 32-BIT NUMBERS (N=7)
VALUE1
DCD 0X44444444
DCD 0X22222222
DCD 0X11111111
DCD 0X33333333
DCD 0XAAAAAAAA
DCD 0X88888888
DCD 0X99999999
AREA DATA2, DATA, READWRITE
;TO STORE RESULT IN GIVEN ADDRESS
RESULT DCD 0X0
END ;Mark end of file
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES
RV Institute of Technology & Management®
Department of Computer Science & Engineering
Experiment-6 Develop an ALP to count the number of ones and zeros in two consecutive
MODULE–2 Memory locations.
AREA ONEZERO, CODE, READONLY Input the 32-bits / 8-digit
ENTRY DECIMAL VALUE inside the
LDR R0, MEMORY MEMORY LOCATION
; Load Address Memory ADDRESS
LDR R1,[R0] 40000000 TO 40000007
; Load 32bit Number INPUT VALUE:
MOV R4, #32 01010000
; Load rotation Count
OBSERVATOION OF
ROTATE
FINAL OUTPUT
RORS R1, #1 inside R2 GPR hold the
; Rotate right by 1 bit, update number of 1’s count will be in
CPSR hex value, total number of
BCS ONES ONE’S
; is carry =1
ADD R3,R3,#1 in the INPUT VULUE is 2
; increment 0 count
B NEXT inside R3 GPR hold the
; Branch to next Rotation number of 0’s count will be in
hex value, total number of
ONES
ZERO’S
ADD R2,R2,#1 in the INPUT VULUE is 30
; increment 1 count (1E in HEX value)
NEXT
ADD R4,R4,#-1
; Decrement Rotation Count
CMP R4, #0
; if Rotation EQ to 0
BNE ROTATE
; if No, goto Rotate
ADD R0,R0,#04
; Add Address of no. of 1's
STRB R2,[R0]
; Store No. of 1's
ADD R0, R0, #1
; Load address of no. of 0's
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES
RV Institute of Technology & Management®
Department of Computer Science & Engineering
Experiment-7 Simulate a program in C for ARM microcontroller using KEIL to sort the
MODULE–3 numbers in ascending / descending order using bubble sort.
Experiment 7a: To sort ASCENDING ORDER using bubble sort TECHNIQUE.
#include <LPC214x.h>
#include <stdio.h> INPUT UNSORTED ARRAY
using CALL STACK
// Function to swap two numbers
void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
int main() {
int arr[] = {2,6,8,7,9,4,1,3,5};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSortAscending(arr, n);
printf("Sorted array in ascending order: \n");
printArray(arr, n);
printf("Original array: \n");
printArray(arr, n);
return 0;
}
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES
RV Institute of Technology & Management®
Department of Computer Science & Engineering
Experiment-7 Simulate a program in C for ARM microcontroller using KEIL to sort the
MODULE–3 numbers in ascending / descending order using bubble sort.
Experiment 7b: To sort DESCENDING ORDER using bubble sort TECHNIQUE.
#include <LPC214x.h>
#include <stdio.h> INPUT UNSORTED ARRAY
using CALL STACK
// Function to swap two numbers
void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
int main() {
int arr[] = {2,6,8,7,9,4,1,3,5};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSortDescending(arr, n);
printf("Sorted array in descending order: \n");
printArray(arr, n);
printArray(arr, n);
printf("Original array: \n");
printArray(arr, n);
return 0;
}
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES
RV Institute of Technology & Management®
Department of Computer Science & Engineering
int main() {
int num;
unsigned long long int result;
return 0;
}
INPUT OF FACTORIAL
Number Is 5
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES
RV Institute of Technology & Management®
Department of Computer Science & Engineering
int main() {
char str[] = "hello rvitm"; // Example string
int i = 0;
return 0;
}
Prepared By: Santhosh Kumar K.P, Programmer, Dept of CSE, RVITM. IPCC: BCS402/MC Lab: Quick Ref NOTES