0% found this document useful (0 votes)
16 views9 pages

Lab 2 - Program - in - Assembly

The document describes an assembly language laboratory assignment. Students are tasked with writing assembly code to copy an array, swap elements in an array if a condition is met, find minimum and maximum values in arrays, and sort an array. The tasks cover basic instructions, flow control, stack operations, and subroutines. Sample code is provided for students to complete the tasks.

Uploaded by

Sami Keyrouz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views9 pages

Lab 2 - Program - in - Assembly

The document describes an assembly language laboratory assignment. Students are tasked with writing assembly code to copy an array, swap elements in an array if a condition is met, find minimum and maximum values in arrays, and sort an array. The tasks cover basic instructions, flow control, stack operations, and subroutines. Sample code is provided for students to complete the tasks.

Uploaded by

Sami Keyrouz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

EEET2096 Laboratory 2: Assembly Programming

Objectives:

The objectives of this laboratory are:


• Learn to program in assembly.
• Familiarize and program with basic instructions including data transfer, arithmetic
and logic operations, flow control, stack operations, subroutines.
• Understand the basic ARM architecture through programming

Introduction

To study the basic instructions in ARM is a good way to understand the ARM architecture.
Some basic instruction categories are covered in the lectures. However, the rich and detailed
information on ARM instructions can be found in MicroVision Help Manual and the reference
manual, Cortex-M4 Programming Manual (PM0214).

Figure 1: Coretex-M4 Programming Manual


Figure 2. Screen snapshot of MicroVision Help Manual on ARM and Thumb Instructions

Figure 3. The ARM and Thumb Instruction summary

The ARM and Thumb Instruction summary is especially useful. It lists all the ARM Thumb
instructions. You don’t have to understand all of them, but for the basic categories, you are
expected to know them. A detailed explanation can be found for each instruction in the
manuals. You can also use search to find the explanation of any particular instruction you
want to know. With aid of the sample program in Lab 1 and the resources mentioned above,
you shall be able to do simple assembly programming. In this lab practice, you are required
to write some codes to perform some simple tasks.
Preliminary
Each laboratory contains some preliminary questions. These will involve reading part of the
references and answering questions. The objective is to have students prepare some material
that will assist in performing the laboratory procedure. These questions do not involve copying
quantities of information from references but rather a summary to demonstrate your
understanding.

a) Give a sample instruction for each of the following actions. Provide explanation for the
instruction.

1. transfer one byte from memory to a general purpose register


2. transfer a 32 bit data from memory to a general purpose register
3. store a 8-bit data from register to a memory unit
4. store a 32-bit data from register to 4 memory units
5. load a 16 bit immediate data to a general purpose register
6. a comparison of two general purpose registers
7. add an immediate 8-bit number to a register
8. decrease the value of a general register by 1
9. an unconditional branch
10. conditional branch if greater or equal for signed numbers
11. call a subroutine
12. return from a subroutine
13. push two registers to the user stack
14. pop data to two registers from the user stack

b) Here is an 8-bit numbers array of length of 8.

0x81,0x72,0x83,0x74,0x85,0x76,0x87,0x78

1. What is the largest number if they are considered as unsigned numbers


2. What is the least number if they are considered as unsigned numbers
3. What is the largest number if they are considered as signed numbers
4. What is the least number if they are considered as signed numbers

Your answers will be useful for you to check your program later

c) Write a section of codes to form a loop. Give your explanation

References
1. Lecture Notes week1-3
2. Tutorial 1-3
3. Instructions set under Microvision Help manual
4. Cortex-M4 Programming Manual
5. Lab1 sample program
Tasks

In this laboratory exercise, you are required to write assembly codes to perform a series of
small tasks. A skeleton of the program is provided and it can be found in the appendix of this
document.

The tasks you are required are

Task1: copy the data array from Array 1 in ROM to Array 2 in RAM .

Task 2: Write a subroutine to swap two 8 bit unsigned numbers in memory only if the first
number is larger than the second one

Task 3 : testing the subroutine in Task 2

Task 4: Find the largest number in an Unsigned 8-bits number array.

Task 5: Find the least number in an Unsigned 8-bits number array.

Task 6 : Find the largest number in a Signed 8-bits number array.

Task 7: Find the least number in a Signed 8-bits number array.

Task 8: Sort an unsigned number array in ascending order

The detailed requirements of these tasks can be found in the skeleton program

You better do the task one by one. Don’t try to finish all the codes and pray all will work.

Procedure

1. Start a new project in the Keil development environment. The Keil start up code is not
required. Save the project as Lab02. Create the sample program Lab02.s given in the
appendix.
2. Start to code the tasks one by one. Go though the code – debug cycle till it works.
Then move on to next task. Task 2 and 3 can be combined for debugging.
3. Record the memory locations for Array1, Array2 and Stack ( Initial top and size)
4. With multiple memory windows, you can easily check if your tasks are correctly
completed. Take screen snapshot should be included in your reports to demonstrate
the results.
Questions

1. ARM has instructions for multiple data transfer between registers and memory block.
Give an example of such instructions.

2. What is the limit for the number of layers of nested subroutines? [ Note: For two
layers, it means inside subroutine 1 there is call to subroutine 2. For three layers, it
means inside subroutine 1, there is at least one call to subroutine2. Inside subroutine
2, there is at least one call to subroutine 3. So on so forth. ]

3. In your subroutine, r3 and r4 are kept in the stack. If there is a nested subroutine call
in your subroutine, how your stack operations shall be modified? Justify your
modification.

4. Task 8 is the hardest one among all of them. If you have completed it, elaborate your
experience . If you have no time to finish it, describe the challenges and your partial
work if you have tried it.

Report

As well as summarising what you did and answering the questions your report should highlight
any challenges the laboratory presented and how you overcame these. Consider you are writing
a laboratory guide for other students. Attempt to document something to assist them with any
problems you perceive they might have..

Looking ahead

For first and second laboratories, assembly language is used. Assembly language is helpful on
understanding the architecture of the microcontroller. However, Assembly is not efficient
language for development. In laboratory 3, Programming ARM microcontroller by C will be
introduced.
Appendix: Lab2 Assembly Program Skeleton
=====================================
; "Lab2.s" by J Wang 2020

; In Lab2 students are required to

; 1. write a simple Assembly program

; 2. farmiliarize with

; arithmetic, logic and data transfer instructions

; flow control ( unconditional and conditional branches)

; Stack operations

; Subroutines

; Task1

Number1 EQU 0x87654321

AREA RESET, DATA, READONLY

EXPORT __Vectors

__Vectors

DCD 0

DCD Reset_Handler

AREA |.text|, CODE, READONLY

EXPORT Reset_Handler

Reset_Handler PROC

ldr sp, = Init_S_Top ; set up stack pointer

; *** Task1: copy the data array from Array 1 in ROM to Array 2 in RAM ***

; The length of the array is 8 bytes

; Array1 starts from Tbe1


; Array2 starts from Tbe2

; Try to use the most efficient way to copy the data.

; task 3 : testing your subroutine

; Let r1 points to the very first byte in Array 2

; Let r2 points to the second byte of Array 2

; call your subroutine to check if it does the job

;now check if your subroutine does the job

; Task 4: If Array 2 is treated as Unsigned 8-bits numbers

; Find the largest number and store it at LargestU

; Task 5: If Array 2 is treated as Unsigned 8-bits numbers

; Find the least number and store it at LeastU

; Task 6: If Array 2 is treated as Signed 8-bits numbers

; Find the largest number and store it at LargestS


; Task 7: If Array 2 is treated as Signed 8-bits numbers

; Find the least number and store it at LeastS

; Task 8: If Array2 is considered as an unsigne 8-bits data array

; write a code to sort the array in ascending order

; This is for advanced students. you can get full marks for this lab only if you can do this one)

Loop3 b Loop3 ; dead loop

ENDP

; Task 2 Write a subroutine to swap two 8 bit unsigned numbers in memory only if the first number is
larger than the second one

; the first number is pointed by r1 and the second number is pointed by r2

; at the beginning of the subroutine, push r3 and r4 to stack

; at the end of the subroutine pop r3 and r4 out

; therefore you can use r3 and r4 freely in the subroutine

; when it returns r3 and r4 can still keep their original values

Swap2U PROC

; implenent your subroutine here

ENDP
AREA Table1, DATA, READONLY

Tbe1 DCB 0x81,0x72,0x83,0x74,0x85,0x76,0x87,0x78 ; Array 1

AREA Tables, DATA, READWRITE

Tbe2 SPACE 0x8 ; Array 2

LargestU DCB 0x00

LeastU DCB 0x00

LargestS DCB 0x00

LeastS DCB 0x00

myStack SPACE 0x100 ; this is a space for a test stack

Init_S_Top DCB 0x88 ; this serve as the initial top of the stack

END

You might also like