Coal Lab1 Stmu
Coal Lab1 Stmu
Coal Lab1 Stmu
ASSEMBLY LANGUAGE
LAB MANUAL
1st Edition, 2023
DEPARTMENT OF COMPUTING
© Copy Rights, Department of Computer Science, STMU Islamabad
DEPARTMENT OF COMPUTING
iii
List of Experiments
14. Final
iv
Lab Number: 1
Lab Title:
Overview of ARM processor’s
assembly programming and
Addressing modes
DEPARTMENT OF COMPUTING
v
Experiment No 1
Overview of ARM processor’s assembly programming and
Addressing modes
1.1 Objectives
This lab is intended for students who are not familiar with assembler, or have a very distant
idea about it. If you have some idea of high-level languages such as Java, Basic C/C++ or
Pascal then you will find assembly coding quite easy.
It is assumed that you have some knowledge about number representation in Hexa-decimal and
binary. If you are not familiar with numbering systems, then you must go through before
starting this lab.
• R13 (Stack Pointer or SP): Holds address of the top of the stack
• R14 (Link Register or LR): Holds return address from subroutine
• R15 (Program Counter or PC): Hold address of the next instruction to be executed
1
Despite the name of a register, it’s the programmer who determines the usage for each general-
purpose register. The main purpose of a register is to keep a number (variable). The size of the
above registers is 32 bits.
Since most compilers convert source code directly to machine code, software developers often
create programs without using assembly language. However, in some cases, assembly code can
be used to fine-tune a program. For example, a programmer may write a specific process in
assembly language to make sure it functions as efficiently as possible.
2
Figure 2. Compiler and Assembler
1.5.1 Label
Label is an optional first field of an assembly statement. Labels are alphanumeric names used
to define the starting location of a block of statements. Labels can be subsequently used in our
program as an operand of some other instruction. When creating the executable file, the
assembler will replace the label with the assigned value. Labels must be unique in the
executable file because an identical label encountered by the Assembler will generate an error.
ARM assembler has reserved first character of a line for the label field and it should be left
blank for the instructions with no labels. In some compilers, labels can be optionally ended
with colon (:) but it is not accepted by the ARM assembler as an end of the label field.
Defining appropriate labels makes your program look more legible. Program location can be
easily found and remembered using labels. It is easier to use certain functionality of your
program in an entirely different code. i.e., your code can be used as a library program. You do
not have to figure out memory addresses because it is a tedious task especially when the
instruction size in your microcontroller is not fixed.
3
1.5.2 Opcode (Mnemonics)
Opcode is the second field in assembly language instruction. Assembly language consists of
mnemonics, each corresponding to a machine instruction. Using a mnemonic, you can decide
what operation you want to perform on the operands. Assembler must translate each mnemonic
opcode into their binary equivalent.
1.5.3 Operands
Next to the opcode is the operand field which might contain different number of operands.
Some of the instructions in ARM Architecture will have no operand while other might have as
many as four operands. The number of operands in an instruction depends on the type of
instruction, and the syntax format of the operand can also be different. Normally, the first
operand is the destination of the operation.
1.5.4 Comments
Comments are messages intended only for human consumption. They have no effect on the
translation process and indeed are not acted on by the ARM Assembler. The comment field of
an assembly language instruction is also optional. A semicolon (;) signifies that the rest of the
line is a comment and is to be ignored by the assembler.
4
After adding an Assembly file, go to “Options for Target” and then in the “Debug” option select
the Simulator.
5
1.8 Area Directives
AREA directive allows the programmer to specify the memory location to store code and data.
Depending on the memory configuration of your device, code and data can reside in different
areas of memory. A name must be specified for an area directive. There are several optional
comma delimited attributes that can be used with AREA directive. Some of them are discussed
below.
6
1.12 Addressing Modes
1) Immediate Addressing mode
The addressing mode in which the data operand is a part of the instruction itself is known as
immediate addressing mode.
7
2) STR R2, [R1]
This instruction will take the address in r1, and then store a 4 byte value from the register r2 to
the memory pointed to by r1. Note: r1 is called the base register.
8
1.14 Post-indexed addressing Mode
The effective address of the operand is the contents of Rn. The offset is then added to this
address and the result is written back into Rn.
Example:
STR R0, [R1], #12
9
Problems and Exercises
Activity 01:
Below is an Assembly Program which defines two 32-bit Arrays having five elements
each. The program will add 5 into each element of Array 1 and stores it in Array 2.
Activity 02:
Write an assembly program which defines two Arrays of 16 bits. Program will copy the
contents of first Array into second Array.
• Contents of both Arrays should be of 16 Bits.
• Array-1 contents (9, 9, 9, 0, 1, 4, 3, 1, 2, 6)
• Array-2 contents (0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
• Load the Arrays into Registers of your choice
• By using a third Register, load the contents of Array-1 and store it in Array-2
• Post-indexed Addressing mode will be used
10
Activity 03:
Write an assembly program that will define two 32-bit Arrays of twenty values in each
array. Program will copy the first Array into second Array using Multiple Load Store
Instruction.
• Copy the data by transferring 5 registers at a time.
• Copy the data by transferring 10 values at a time.
Activity 04:
What functions do the following commands perform?
1) LDR R1 , [ R2 , #16 ]
2) MOV R2 , [ #123 ]
3) STR R6 , [ R8 , #-4 ]
11
Lab Exercise and Summary
12
Student’s Signature: Date:
13
Evaluation Criteria
Method of Evaluation: Viva, File submitted on LMS.
Assignment All tasks Most tasks Some tasks Most tasks All tasks were
completed were were were incomplete or
correctly in completed completed incomplete or incorrect.
given time correctly. correctly and incorrect and
and have a Tasks could have an have no Didn’t
complete be improved incomplete understanding perform tasks
understanding further and understanding and have no
have a understanding
complete
understanding
Total
14