I. OBJECTIVES
1. To learn conditional branch instructions
2. To learn iteration in assembly language
II. PARTS LIST
Equipment:
IBM PC or compatible with Windows 2000 or higher
Keil uVision MDK-ARM v4.73
III. INTRODUCTION
Iteration is one of the major advantages of a computer, the ability of repeating the task precisely and at the same time the ability to make decision and change the course based on the inputs. In this lab we will use iterations to copy a block of data from the read-only memory to RAM and to find the smallest number within the block.
IV. PROCEDURE
1. Start a new project in Keil uVision and copy the program template from a previous lab.
2. Declare an array of eleven 8-bit numbers in the constants area with the following values:
0x76, 0x77, 0xE4, 0x4D, 0x02, 0x13, 0x50, 0x49, 0x4E, 0x6F, 0x77.
3. Write an assembly program using iteration to copy TEN bytes from constant area to RAM.
4. Verify with simulation that 10 and only 10 bytes were copied. The data “0x77” serves as a sentinel to be sure that no more than 10 bytes were copied.
5. Append to the program with the code to find the smallest number in an array. The code are required to
a. Start with the beginning address of the array in R0 and the length of the array in R1.
b. Each byte is treated as an 8-bit signed integer.
c. At the end of the program, the address of the smallest number is left in R0.
6. Verify with simulation that the address of the data “0xE4” is in R0 at the end of program execution.
V. Deliverables
1. The assembly source file of the final program.
2. The screenshot of the IDE showing the registers and the RAM memory with data at the end of program execution.
Sample of program 4
Assembly program template
; This program copies four bytes from SRC to DST
;
AREA Reset, CODE, READONLY
EXPORT __Vectors
__Vectors DCD 0x20008000 ; Top of Stack
DCD Reset_Handler ; Reset Handler
; source of data should be in Flash
AREA constants, CODE, READONLY
; destination of the data should be in RAM
AREA data, ALIGN=2
SIZE EQU 10 ; 0x7800/4
seed SPACE 4 ; for random number generator
array SPACE SIZE*4
; code starts here coming out of reset
THUMB
AREA program, CODE, READONLY
EXPORT Reset_Handler
ENTRY
Reset_Handler
; fill the array with random numbers
LDR R1, =array
LDR R2, =SIZE
L1 BL rand
STR R0, [R1], #4
SUBS R2, #1
BNE L1
; sort the array
LDR R0, =array
LDR R1, =SIZE
BL sort
fin B .
; selection sort
; parameters:
; R0 - address of the array
; R1 - number of elements in the array
;
sort
PUSH {R4-R7, LR}
MOV R4, R0
MOV R5, R1
SUB R5, #1
st1 MOV R0, R4 ; address or the array
MOV R1, R5 ; size of array
BL findmin ; returns addr of min in R0
LDR R6, [R0] ; swap first and min
LDR R7, [R4]
STR R6, [R4]
STR R7, [R0]
ADD R4, #4 ; point to next element
SUBS R5, #1 ; decrement count
BNE st1 ; if not end of array, repeat
POP {R4-R7, LR}
BX LR
; this function returns the address of the smallest element in the array
; parameters:
; R0 - address of the array
; R1 - number of elements in the array
; register usage:
; R4 - the smallest element till now
; R5 - address of the smallest element till now
;
findmin
PUSH {R4-R6}
fm2 LDR R4, [R0] ; R4 - min
MOV R5, R0 ; R5 - location of min
fm1 ADD R0, #4 ; point to next word
SUBS R1, #1 ; decrement count
BEQ fm3 ; if end of array
LDR R6, [R0] ; else get next element
CMP R6, R4 ; compare it to the smallest
BHS fm1 ; if not smaller, look for next one
B fm2 ; else update the smallest
fm3 MOV R0, R5 ; return address of smallest
POP {R4-R6}
BX LR
; use linear congruential generator for pseudorandom numbers
; X(n+1) = (a * X(n) + c) % m
; a = 1103515245 (from GCC)
; c = 12345
; m = 2**32
;
rand
PUSH {R1-R2}
LDR R2, =seed
LDR R0, [R2]
LDR R1, =1103515245
MUL R0, R0, R1 ; a * X
LDR R1, =12345
ADD R0, R1 ; a * X + c
STR R0, [R2] ; save seed
POP {R1-R2}
BX LR
ALIGN
END
Reply Forward
Click here to Reply or Forward