Académique Documents
Professionnel Documents
Culture Documents
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:
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:
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:
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:
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:
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:
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:
MOV R0,R1
MOV R1,R4
SUBS R3,#0X01
BNE LOOP
END
Output:
ADD R0,R1,R2
STRB R0,[R4],#0X01
MOV R1,R2
MOV R2,R0
SUBS R3,#0X01
BNE LOOP
END
Output:
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:
ADD R0,#4
SUBS R5,#0X01
BNE L1
LDR R0,=0X5000
STR R2,[R0]
END
Output: