You are on page 1of 17

Registration number: 15MES0060

Date:

EXERCISE 1
1.1 Compiling, making, debugging, and running
Writing a program in Keil. There are separate functions in Keil to compile, make, debug, and run a program.
Experiment with all four and describe what each does.
Program:
AREA PRGM1, CODE, READONLY
ENTRY
START MOV R0,#02
MOV R1,#05
ADD R1,R1,R0
END
Explanation:
Compilation:
Build button. Compile and link program using button Build or the menu Project - Rebuild all target files.
Vision starts translating and linking the source files and creates an absolute object module that can be
loaded into the Vision Debugger for testing. The status of the build process is shown in the window Build
Output.
Debug:
Once the program is compiled and linked, start testing it with the Debugger. Use the toolbar button or the
menu Debug - Start/Stop Debug Session. Vision initializes the debugger and starts testing the program
until the end statement are reached. By default, a breakpoint has been set to stop execution there
Run:
After debugging the program the toolbar button can be used to run the given program. In this step data
transfer, data manipulation is done and we get the result. In this example immediate data is transferred to
r0,r1 in first two steps then the result of addition is stored in r1.
Output:

1.2 Stepping and stepping in


Writing a program in Keil Instead of running the code, step all the way through the code using both the step
method and the step in method. What is the difference between the two methods of stepping through the
assembly code?
Program:
AREA PRGM1, CODE, READONLY
ENTRY
START MOV R0,#02
MOV R1,#-05
ADDS R1,R1,R0
END
Explanation:
There are two methods of stepping through assembly code either we can perform step execution or step
over. In step execution each statement is executed and corresponding output we get on the different
registers. This execution can be used for checking logical errors. Another method is step over execution in
this all steps are executed. We get the final output in this method.
Output:

Registration number: 15MES0060

Date:

EXERCISE 2
2. 1 Signed and unsigned addition
For the following values of a and b, predict the values of the N, Z, V, and C flags produced by performing
the operation a + b. Load these values into two ARM registers to perform an addition of the two registers.
Using the debugger, record the flags after each addition and compare those results with your predictions.
When the data values are signed numbers, what do the flags mean? Does their meaning change when the
data values are unsigned numbers?
Program:
area exp21, code, readonly
entry
start ldr r0, =0xffff0000
ldr r1, =0x00001234
add r0,r0,r1
end
Output:

2.2 Multiplication
Change the ADD instruction in the code from the above program [2.1] to MULS. Also change one of the
operand registers so that the source registers are different from the destination register, as the convention for
multiplication instructions requires. Put 0xFFFFFFFF and 0x80000000 into the source registers. Now rerun
your program and check the result.
1. Does your result make sense? Why or why not?
2. Assuming that these two numbers are signed integers, is it possible to overflow in this case?
3. Why is there a need for two separate long multiply instructions, UMULL and SMULL? Give an example
to support your answer.
Program:
Part a:
area exp22,code, readonly
entry
start ldr r0, =0xffffffff
ldr r1, =0x80000000
muls r2,r1,r0
end

Output:

Part b:
Program:
area exp22,code, readonly
entry
start ldr r0, =0xffffffff
ldr r1, =0x80000000
smull r3,r2,r1,r0
umull r5,r4,r1,r0
end
Output:

Registration number: 15MES0060

Date:

EXERCISE 3
3.1 Register-swap algorithm
The EOR instruction is a fast way to swap the contents of two registers without using an intermediate
storage location such as a memory location or another register. Suppose two values a and b are to be
exchanged. The following algorithm could be used:
Write the ARM code to implement the above algorithm, and test it with the values of a = 0xF631024C and
b = 0x17539ABD. Analyze the result before and after running the program.
Program:
AREA pgm3_1, CODE, READONLY
ENTRY
START LDR R0,=0xF631024C
LDR R1,=0x17539ABD
EOR R0,R0,R1
EOR R1,R0,R1
EOR R0,R0,R1
END
Output:
Before:

After:

3.2 Signed multiplication


Assume that a signed long multiplication instruction is not available. Write a program that performs long
multiplications, producing 64 bits of result. Use only the UMULL instruction and logical operations such as
MVN to invert, EOR, and ORR. Run the program using the two operands 2 and 4 to verify.
Program:
AREA EXP3_2, CODE, READONLY
ENTRY
START
MOV R0,#-2;
MOV R1,#-4;
MOV R4,#-1
MUL R2,R0,R4
MUL R3,R1,R4
UMULL R10,R9,R2,R3
END
Output:

Registration number: 15MES0060

Date:

EXERCISE 4
4.1 Absolute value
Write ARM assembly to perform the function of absolute value. Register r0 contains the initial value, and r1
contains the absolute value. Try to use only two instructions, not counting the SWI to terminate the program.
Program:
AREA pgm4_1, CODE, READONLY
ENTRY
MOVS r1, #-15;
RSBLT r1, r1, #0;
END
Output:

4.2 Division
Write ARM assembly to perform the function of division. Registers r1 and r2 contain the dividend and
divisor, r3 contains the quotient, and r5 contains the remainder. For this operation, you can either use a
single shift-subtract algorithm or another more complicated one.
Program:
AREA pgm4_2, CODE, READONLY
ENTRY
START
LDR R1,=0x20
LDR R2,=0x5
MOV R3,#0
LOOP
SUBS R1,R1,R2
ADDGE R3,#1
BGE LOOP
ADD R1,R1,R2
MOV R5,R1
END

Output:

4.3 Gray codes


A Gray code is an ordering of 2n binary numbers such that only one bit changes from one entry to the next.
One example of a 2-bit Gray code is b10 11 01 00. The spaces in this example are for readability. Write
ARM assembly to turn a 2-bit Gray code held in r1 into a 3-bit Gray code in r2.
Program:
AREA exp2, CODE, READONLY
ENTRY
START
MOV R1,#0x000000b4
MOV R2,#0x00000003 ;this is mask of two bits
MOV R7,#0
LOOP
AND R3,R1,R2
MOV R3,R3,LSL R7
ORR R5,R3,R5
MOV R2,R2,LSL #2
ADD R7,#1
CMP R7,#8
BLT LOOP
END
Output:

Registration number: 15MES0060

Date:

EXERCISE 5
5.1 Assignments with operands in memory
Assume an array of 25 words. A compiler associates variables x and y with registers r0 and r1, respectively.
Assume that the base address for the array is located in r2. Translate this C statement/assignment using the
post-indexed form:
x = array [5] + y
Now try writing it using the pre-indexed form.
Post-indexed form:
Program:
AREA pgm5_1, CODE, READONLY
ENTRY
START MOV R2,#0X40000014
MOV R1,#1
LDR R3,[R2],#0X14
ADD R0,R3,R1
END
Output:

Pre-indexed form:
Program:
AREA pgm5_1, CODE, READONLY
ENTRY
START MOV R2,#0X40000000
MOV R1,#1
LDR R3,[R2,#0X14]
ADD R0,R3,R1
END
Output:

5.2 Loads and stores


Assume an array of 25 words. A compiler associates y with r1. Assume that the base address for the array is
located in r2. Translate this C statement/assignment using the post-indexed form:
array [10] = array[5] + y
Now try it using the pre-indexed form.
Post-indexed form:
Program:
AREA pgm5_2, CODE, READONLY
ENTRY
START MOV R2,#0X40000014
MOV R1,#3
LDR R3,[R2],#0X14
ADD R0,R3,R1
STR R0,[R2]
END
Output:

Pre-indexed form:
Program:
AREA pgm5_2, CODE, READONLY
ENTRY
START MOV R2,#0X40000000
MOV R1,#3
LDR R3,[R2,#0X14]!
ADD R0,R3,R1
STR R0,[R2,#0X14]
END
Output:

5.3 Array assignment


Write ARM assembly to perform the following array assignment in C:
for ( i = 0; i <= 10; i++)
{
a[i] = b[i] + c;
}
Assume that r3 contains i, r4 contains c, the starting address of array a is in r1, and the starting address of
array b is in r2.
Program:
AREA pgm5_3, CODE, READONLY
ENTRY
START MOV R1,#0X40000000 ;array a
MOV R2,#0X50000000 ;array b
MOV R3,#0X0A
MOV R4,#5
REPEAT
LDR R0,[R2],#0X04
ADD R0,R0,R4
STR R0,[R1],#0X04
SUB R3,#1
BNE REPEAT
END
Output:

Registration number: 15MES0060

Date:

EXERCISE 6
6.1 Arrays and pointers
Consider the following two C procedures that initialize an array to zero using a) indices, and b) pointers:
a) init_Indices (int a[], int s) {
int i;
for ( i = 0; i < s; i ++)
a[i] = 0; }
b) init_Pointers (int *a, int s) {
int *p;
Convert these two procedures to ARM assembly. Put the starting address of the array in r1, s in r2, and i and
p in r3. Assume that s > 0
Program:
Part a:
AREA exp2, CODE, READONLY
ENTRY
START MOV R3,#0
MOV R4,#1
MOV R1,#0x00000050
MOV R2,#10
MOV R5,#0
UP
STR R5,[R1]
ADD R1,R1,#4
ADD R3,R3,#1
CMP R3,R2
BLT UP
END
Output:

Part b:
AREA exp2,CODE,READONLY
ENTRY
START MOV R3,#0
MOV R4,#1
MOV R1,#0x50
MOV R6,#0x78
MOV R2,#10
MOV R5,#0
UP
STR R5,[R1]
ADD R1,R1,#4
ADD R3,R3,#1
ADD R1,R6
BLE UP
END
Output:

6.2 The Fibonacci sequence


The Fibonacci sequence is an infinite sequence of numbers that begins with 0 and 1, and each of the
remaining numbers is the sum of the previous two numbers. Write an ARM assembly program that
computes the first 12 numbers of the sequence and stores the sequence in memory locations 0x4000 to
0x400B.
Program:
AREA EX1,CODE,READONLY
ENTRY
START MOV R0,#0X0
MOV R1,#0X1
MOV R2,#0X4000
STRB R0,[R2],#0X01
STRB R1,[R2],#0X01
MOV R3,#0X0A
LOOP ADD R4,R0,R1
STRB R4,[R2],#0X01

MOV R0,R1
MOV R1,R4
SUBS R3,#0X01
BNE LOOP
END
Output:

6.3 The nth Fibonacci number


Use The Fibonacci sequence exercise program and write ARM assembly to compute f(n). Start with r1 = n.
At the end of the program, r0 = f(n)
Program:
AREA EX1,CODE,READONLY
ENTRY
START MOV R3,#0X0D ;n values by user
SUB R3,#0X02
MOV R1,#0X0
MOV R2,#0X1
MOV R4,#0X4000
STRB R1,[R4],#0X01
STRB R2,[R4],#0X01
LOOP

ADD R0,R1,R2
STRB R0,[R4],#0X01
MOV R1,R2
MOV R2,R0
SUBS R3,#0X01
BNE LOOP
END

Output:

Registration number: 15MES0060

Date:

EXERCISE 7
7.1 Factorial calculation
To take advantage of the idea of conditional execution, examine the algorithm for computing n!, where n is
an integer. For a given value of n, the algorithm iteratively multiplies a current product by a number that is
one less than the number it used in the previous multiplication. The code continues to loop until it is no
longer necessary to perform a multiplication, first by subtracting one from the next multiplier value and
stopping when it is equal to zero.
Program:
AREA Factorial, CODE, READONLY
ENTRY
MOV r6,#10 ; load n into r6
MOV r7,#1 ; if n = 0, at least n! = 1
loop
CMP r6, #0
MULGT r7, r6, r7
SUBGT r6, r6, #1 ; decrement n
BGT loop ; do another mul if counter!= 0
stop
B stop ; stop program
END
Output:

7.2 Find maximum value


In this exercise, find the largest integer in a series of 32-bit unsigned integers. The length of the series is
determined by the value in register r5. The maximum value is stored in the memory location 0x5000 at the
end of the routine. The data values begin at memory location 0x5006. Choose 11 or more integers to use.
Use as much conditional execution as possible when writing the code
Program:
AREA pgm7_2, CODE, READONLY
ENTRY
START
LDR R0, =0X5006
MOV R5,#5
LDR R1, [R0],#4
LDR R2, [R0],#4
SUB R5, #0X01
L1
LDR R3, [R0]
CMP R2,R3
BCS L
MOV R2,R3

ADD R0,#4
SUBS R5,#0X01
BNE L1
LDR R0,=0X5000
STR R2,[R0]
END

Output:

7.3 Sequential parity checker


Write ARM assembly to inspect the parity of a value initially held in r0. If r0 has an odd number of ones, the
program ends with 0x0001 in r1. If r0 has an even number of ones, the program ends with 0x0000 in r1.
Program:
AREA pgm7_3, CODE, READONLY
ENTRY
START
MOV R2,#1
LDR R0,=0xFEEE
MOV R3,#32
MOV R1,#0
LOOP
TST R0,R2
ADDGT R1,R1,#1
LSL R2,R2,#1
SUBS R3,R3,#1
BGT LOOP
AND R1,R1,#1
L
BL
END
Output:

You might also like