0% found this document useful (0 votes)
45 views27 pages

CH - En.u4ece21016 - Lab Report 2

The document is a lab report for a course on microcontrollers and interfacing. It provides instructions for assembly language programs to perform operations like addition, subtraction, logical operations, comparison, multiplication, and multiply-accumulate. The programs are written in assembly code areas and utilize instructions like MOV, LDR, ADD, SUB, CMP, MUL, and MLA. Each program section describes the source code, provides output, and explains what was learned from the program.

Uploaded by

NP GK
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)
45 views27 pages

CH - En.u4ece21016 - Lab Report 2

The document is a lab report for a course on microcontrollers and interfacing. It provides instructions for assembly language programs to perform operations like addition, subtraction, logical operations, comparison, multiplication, and multiply-accumulate. The programs are written in assembly code areas and utilize instructions like MOV, LDR, ADD, SUB, CMP, MUL, and MLA. Each program section describes the source code, provides output, and explains what was learned from the program.

Uploaded by

NP GK
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/ 27

Course Code: 19ECE304 Name: N P Ganesh Kumar

Course Title: Microcontrollers and Roll No: CH.EN.U4ECE21016


cascasc Interfacing
Semester: 5th
Course Faculty: Dr. C Ganesh Kumar
Academic Year: 2023-2024

Lab Report – 2
Software Used

General Instruction

1. MOV: Moves 16-bit data to the register.


2. AREA: Assigns and instructs the assembler to assemble the code.
3. END: To denote the completion of the program.
4. CODE: To specify whether it is code or data sec􀆟on.
5. READONLY: To make it viewable but not editable.

List Of Experiment

1. Addition with Flag


2. Addition without Flag
3. Addition and Shift
4. Reverse Subtract
5. Logical OR
6. Compare
7. Multiply
8. Multiply and accumulate
9. 16-bit Adder
10. 32-bit Adder
11. Largest of Two Number
12. Largest from a Series
13. Factorial
14. Decimal to Hex
15. Hex to Decimal

Addition with Flag

Source Code

AREA add_flag, CODE, READONLY


LDR R1, =0XFFFFFFFF
ADDS R1, #1
END

Code Description

Similar to our previous program, we have designed a code area named “add flag” and the
instructions “CODE” indicates that the instructions written under this area are executable which
is also read only. We use “LDR” instruction to load the value into a register, here it loads the
value 0xffffffff (all bits set to 1) into register R1.Then we use a new instruction called “ADDS”
which is used to add two operands and store the result in a register. Here, it adds the value 1 to
the value already stored in register R1 and updates the result in R1. The “S” suffix indicates that
the instruction updates the flags based on the result. Here, the cpsr gets affected. The last
instruction “END” marks the end of the code.
Output

Inference

From this program I have learnt a new similar instruction to “MOV” that is “LDR” which loads
the value into a register. Then I learnt a new instruction called “ADDS” which adds the values of
two registers and affects the flags that is cpsr based on the result. I have implemented this both in
the program and have verified it.

Addition without Flag

Source Code

AREA add_without_flag, CODE, READONLY


LDR R1, =0XFFFFFFFF
ADD R1, #0X1
END

Code Description

Similar to our previous program, we have designed a code area named “add without flag” and
the instructions “CODE” indicates that the instructions written under this area are executable
which is also read only. We use “LDR” instruction to load the value into a register, here it loads
the value 0xffffffff (all bits set to 1) into register R1. Then we use a new instruction called
“ADDS” which is used to add two operands and store the result in a register. Here, it adds the
value 1 to the value already stored in register R1 and updates the result in R1. The “S” suffix
indicates that the instruction updates the flags based on the result. Here, the cpsr gets affected.
The last instruction “END” marks the end of the code.

Output

Inference

Overall, the code initializes two registers, R1 and R2, with the values 8 and 4, respectively. Then
it performs an addition with carry operation, adding the values in R1 and R2 along with the carry
flag (if set) and stores the result in R3. Finally, the program ends.
Reverse Subtract

Source Code

AREA reversesubtract,CODE,READONLY
MOV R1,#0x00000000
MOV R2,#0x00000000
MOV R3,#0x00000077
RSB R1,R3,R2 ;reverse subtract
END

Code Description

Similar to our previous program, we have designed a code area named “add without flag” and
the instructions “CODE” indicates that the instructions written under this area are executable
which is also read only. We use “LDR” instruction to load the value into a register, here it loads
the value 0xffffffff (all bits set to 1) into register R1. Then we use a new instruction called
“ADDS” which is used to add two operands and store the result in a register. Here, it adds the
value 1 to the value already stored in register R1 and updates the result in R1. The “S” suffix
indicates that the instruction updates the flags based on the result. Here, the cpsr gets affected.
The last instruction “END” marks the end of the code.

Output
Inference

Overall, the code initializes two registers, R1 and R2, with the values 8 and 4, respectively. Then
it performs an addition with carry operation, adding the values in R1 and R2 along with the carry
flag (if set) and stores the result in R3. Finally, the program ends.

Addition and Shift

Source Code

AREA add_shift, CODE, READONLY


MOV R1, #0X0
MOV R2, #0XA
ADD R1, R2, R2, LSL #1
END

Code Description

We have created a work space or code area called “addshift” which contains executable
instructions and which is read only. We have used “MOV” instruction to move the immediate
value #0x00000000 into register R1, and the immediate value #0x0000000A into register R2.
We have used “ADD” instruction to add the value of register R2 to itself and then use instruction
“LSL” to shift the value in R2 left by one bit position. The result is then stored in register R1.
The last line “END” marks the end of the code.

Output
Inference

Overall, the code initializes two registers, R1 and R2, with the values 8 and 4, respectively. Then
it performs subtraction with the carry operation, subtracting the values in R1 and R2 along with
the carry flag (if set) and stores the result in R3. After execution, the code stops

Logical OR

Source Code

AREA orring_bro, CODE, READONLY


LDR R1, =0X10305070
LDR R2, =0X02040608
LDR R3, =0
ORR R3, R1, R2
END

Code Description

We have created a new code area called “logicalor” which contains set of executable set of
instruction and which is read only. We load the value 0x00000000 into register R1, the value
0x10305070 into the register R2, and the value 0x02040608 into the register R3. Then we use a
new instruction called “ORR” which performs a logical OR operation on two registers and stores
the result in a register, here it performs a logical operation between the values of registers R2 and
R3, and store the result in register R1. The “END” instruction marks the end of the code.
Output

Inference

From this program I have learnt a new instruction called “ORR” which performs the logical
operation between two operands or registers. I have also implemented this in the program and
verified it.

Compare
Source Code

AREA not_a_comparer, CODE, READONLY


MOV R1, #0X5
MOV R2, #0X5
CMP R1, R2
END

Code Description

We have created a new code area called “compare” which contains a set of executable
instructions which is read only. We have moved the immediate value #0x00000004 into register
R1, the immediate value #0x00000004 into register R2. Then we make use of a new instruction
called “CMP” which is used to compare two registers, here it compares the values in registers R1
and R2 and updates the status flags based on the result of the comparison. 2’s complement
subtraction is happening which makes C=1. The “END” line marks the end of the code.

Output

Inference
From this program I have learnt new instruction called “CMP” which compares the two registers
as mentioned above and updates the status flags based on the results of the comparison. I have
observed the cpsr output and noticed the change. I have also observed the output of Carry which
is 1. I have also implemented this in the program and have verified it.

Multiply

Source Code

AREA multiply, CODE, READONLY


LDR R0, =0X0
LDR R1, =0X20000002
LDR R2, =0X40000004
MUL R0, R1, R2
END

Code Description

We have created a code area named “multiply” which contains executable set of instructions
which is read only. We load the value 0x00000000 into register R0, the value 0x20000002 into
the register R1, the value 0x40000004 into the register R2. We make use of the previously
studied instruction “MUL” which performs multiplication between two registers. Here, it
multiplies the values of register R1 and R2 and store the result in register R0. The last instruction
“END” marks the end of the code.

Output
Inference

From this program I have brushed up the basics of instructions “MUL” and “LDR” and have
observed the output and also observed the variant of “MUL” we are giving here indicates only
the lower 32bits of the result are stored in the destination register that is R0. I have implemented
this in the program and verified it.

Multiply and Accumulate

Source Code

AREA multiply_and_accmulate, CODE, READONLY


LDR R0, =0X0
LDR R1, =0X1
LDR R2, =0X2
LDR R3, =0X4

MLA R0, R1, R2, R3


END
Code Description

We have created a new code area “multiplyacc” which contains executable instructions and
which is read only. We have load the value 0x00000000 into register R0, the value 0x00000001
into register R1, the value 0x0000002 into register R2, and similarly load the value 0x00000004
into register R3. We have used the new instruction “MLA”, which performs the multiplication
and addition operations and store the result in a register. Here, it multiplies the values in registers
R1 and R2, adds the value in register R3 to the result, and stores the final result in register R0.
The “END” instruction marks the end of the code.

Output

Inference

From this program I have learnt a new instruction called “MLA” which multiplies and
accumulates that is addition after multiplication. Here it multiplies R2 and R1 and adds with R3
and stores it in R0. I have implemented this in the program and verified it at the output.
16-bit Adder

Source Code

AREA A_16_bit_adder, CODE, READONLY

MAIN
LDR R1, VALUE1
LDR R2, VALUE2

ADD R3, R1, R2


STR R3, RESULT ;STORE
SWI &11

VALUE1
DCW &C123
ALIGN

VALUE2
DCW &02AA
ALIGN

RESULT
DCW 0
END

Code Description

We have created a new code area named “Program” which contains set of executable instructions
which is read only. Firstly, we define three areas of memory: the MAIN area, the VALUE1 area,
and the VALUE2 area. The “MAIN” label or area is where the main program code is stored. The
“VALUE1” area and “VALUE2” area are where the values 123 and 2AA will be stored
respectively. Then we use the “LDR” instruction to load the value from “VALUE1” area into
register R1, to load the value from “VALUE2” area into register R2. Then the next line of codes
uses the “ADD” instruction to add the values in registers R1 and R2, and the result is stored in
R3. Lastly we use “STR” instruction which stores the value into a register, here it stores the
value in register R3 in the “RESULT” area. The last line “END” marks the end of the program.
Output

Inference

From this program I have understood the branching that occurs between main code and sub
instructions that happens in lengthy assembly level language programs. I have also learnt how to
store the value separately and then calling it to the register and performing add instruction. I have
also learnt new instructions such as “STR” which stores the value into a register, “SWI” which
terminates the program. I have learned how to do 16-bit addition operation and verified it results.
32-bit Adder

Source Code

AREA adder_32_bit, CODE, READONLY

MAIN
LDR R1, VALUE1
LDR R2, VALUE2
ADD R3, R1, R2
STR R3, RESULT
SWI &11

VALUE1
DCD &ABCDC123

VALUE2
DCD &111102AA

RESULT
DCD 0
END

Code Description

We have created a code area named “Program” which contains set of executable set of
instructions which is read only. Similar to our previous program where we performed 16 bit
addition, the code defines three areas of memory. The “MAIN” area, the “VALUE1” area, and
the “VALUE2” area. Similar to our previous program the “MAIN” area is where the main
program will be stored, the” VALUE1” area stores ABCD123 and “VALUE2” area stores
111102AA will be stored. Then we use “LDR” instruction to load the value from “VALUE1” to
register R1, and similarly from “VALUE2” to register R2. The next line of the code use the
“ADD” instruction to add the values to add the values in registers R1 and R2. The result of the
addition is stored in register R3. Then we use the “STR” instruction to store the value in register
R3 in the “RESULT” area. The last line of the program “SWI” marks the end of the code.
Output

Inference

From this program I have understood the branching that occurs between main code and sub
instructions that happens in lengthy assembly level language programs. I have also learnt how to
store the value separately and then calling it to the register and performing add instruction. I have
also learnt new instructions such as “STR” which stores the value into a register, “SWI” which
terminates the program. I have learned how to do 32-bit addition operation and verified it results.
Largest of Two Series

Source Code

AREA Larger_number, CODE, READONLY

MAIN
LDR R1, VALUE1
LDR R2, VALUE2
CMP R1, R2
BHI DONE
MOV R3, R2
B STORE

DONE
MOV R3, R1

STORE
STR R3, RESULT
SWI &11

VALUE1
DCD &12345678

VALUE2
DCD &87654321

RESULT
DCD 0
END

Code Description

We have created a code area named “Program” that contains executable set of instructions which
is read only. There are also sections called Value1 and Result where we store values for certain
variables. In the code, we take the values from the Value1 and Value2 that is (&12345678 and
&87654321) labels and put them into registers called R1 and R2. We then compare the values in
R1 and R2 using a special instruction called CMP. If R1 is bigger than R2, we go to a place
called Done, otherwise we continue to the next part of the code. If R1 is indeed bigger, we move
the value from R2 to R3. But if R1 is not bigger than R2, we move the value from R1 to R3.
Finally, we store the value from R3 into the Result section. The program ends when it reaches an
instruction called SWI &11.
Output

Inference

From this program I have learnt new instructions such as “BHI” which does branch if higher,
that is used to perform a conditional branch based on the condition that the unsigned comparison
result of the two registers is “higher” or “greater”. I have also brushed up topics of “CMP”,
“STR”, “SWI”, “DCD”. I have implemented this in the program and verified the results and
learnt to do find the larger of two numbers.
Largest from a Series

Source Code

AREA Scan_numbers_finding_largest, CODE, READONLY


MAIN
LDR R0, =TABLE ; Here this = points to the address of first one but not the
value.
EOR R1, R1, R1 ; Clearing and making R1 empty (avoiding garbage value)
LDR R2, LENGTH
CMP R2, #0
BEQ Done ; BRANCH EQUALITY

LOOP
LDR R3, [R0] ; Using [] to make it capable of holding address value, like
pointers in C
CMP R3, R1 ; Compares by subraction, and if same, zero flag goes to 1, if
greater no carry or zero, if less then carry flag goes 0 from 1
BCC LOOPTEST ; Branch if carry is clear (Default carry flag is 1)
MOV R1, R3

; Note, During subraction at no borrow case, carry is always 1

LOOPTEST
ADD R0, R0, #4 ; Adding 4 because of 32 bits (4 bytes)
SUBS R2, R2, #0X1
BNE LOOP ; Branch not equal to 0

Done
STR R1, RESULT
SWI &11

TABLE
DCW &A152
ALIGN

DCW &7F61
ALIGN

DCW &F123
ALIGN

DCW &8000
ALIGN

TABLEEND

LENGTH
DCW (TABLEEND-TABLE)/4
ALIGN

RESULT
DCD 0
END
Code Description

We have created a code area “Program2” that contains a set of programmable executions that is
read only. It loads the memory address of the "Table" section into register R0. It initializes
register R1 to zero. It loads the value from the "Length" label into register R2. It compares the
value in register R2 to zero. If the value in register R2 is equal to zero, the code branches to the
"Done" label. If the comparison result is not zero, the code enters a loop. Within the loop, it
loads the value from the memory address specified by register R0 into register R3. It compares
the value in register R3 to the value in register R1. If the value in register R3 is less than the
value in register R1, the code branches to the "Looptest" label. If the comparison result is not
less, the code copies the value in register R3 to register R1. It then checks if the value in register
R2 is not equal to zero. If the value in register R2 is not zero, the code branches to the "Loop"
label. The loop continues until the condition in step 12 is no longer satisfied. Once the loop is
exited, the code stores the value in register R1 at the memory address specified by the "Result"
label. Finally, the program terminates.

Output
Inference

From this program I’ve learnt new instructions such as “EQR” which does Exclusive OR. It
is a bitwise logical operation that performs the exclusive OR operation between two
operands. The “BEQ” instruction which does Branch if Equal, that performs conditional
branch based on the condition that two values are equal. I’ve also learnt about “SUBS”,
“MCC” loop test, and better understanding of other instructions we used in previous
experiments. I have learnt to scan a series of numbers to find the largest. The results have
been verified.

Factorial

Source Code

AREA Factorial, CODE, READONLY

MAIN
LDR R0, =DATATABLE
LDR R1, VALUE
MOV R1, R1, LSL#0X2
ADD R0, R0, R1
LDR R2, [R0]
ADR R3, RESULT1
STR R2, [R3]
SWI &11

DATATABLE
DCD 1
DCD 1
DCD 2
DCD 6
DCD 24
DCD 120
DCD 720
DCD 5040

VALUE
DCB 5
ALIGN

RESULT1
DCD 0
END
Code Description

We have created a code area named “Program1”, that contains a set of executable instructions
that is read only. The code begins by defining two memory areas: DATATABLE and RESULT1.
The DATATABLE area consists of an array of 8 constants, while the RESULT1 area is a single
memory location. Next, the code loads the memory address of the DATATABLE area into
register R0. This address will be used to access the values stored in the DATATABLE array.
Then, the value of the VALUE label is loaded into register R1. To determine the specific index
of the value to be read from the DATATABLE array, the value in register R1 is shifted left by 2
bits using the LSL (Logical Shift Left) instruction. This is necessary because each constant in the
DATATABLE array corresponds to a specific index. The code adds the value in register R1 to
the value in register R0. This yields the address of the desired value within the DATATABLE
array. Next, the value at the address specified by register R0 is loaded into register R2. This
value will be stored in the RESULT1 area. Subsequently, the value in register R2 is stored in the
RESULT1 area, completing the assignment of the desired value to the RESULT1 memory
location. Finally, the program concludes when the SWI (Software Interrupt) instruction with the
interrupt number &11 is encountered.

Output
Inference

From this program how to manipulate an array of 8 constants. I have also brushed up
concepts of creating data of separate areas and loading and retrieving data from them. I have
learnt how to do the factorial of a number and verified the results.

Decimal to Hex Conversion

Source Code

AREA dec2hex, CODE, READONLY


ENTRY

NUM EQU 0X9876 ;Decimal


LDR R1, =NUM
AND R2, R1, #0X000F
AND R3, R1, #0X00F0
LSR R3, #4
MOV R4, #10
MLA R5, R3, R4, R2

AND R3, R1, #0X0F00


LSR R3, #8
MOV R4, #100
MLA R5, R3, R4, R5

AND R3, R1, #0XF000


LSR R3, #12
MOV R4, #1000
MLA R5, R3, R4, R5

LDR R6, =RESULT


STR R5, [R6]

HERE B HERE
AREA DATA2, DATA, READWRITE
RESULT DCD 0X0
END

Code Description

It defines a code area named "eodd" that contains read-only executable instructions. The
"ENTRY" directive indicates that this code is the entry point of the program. It defines a constant
value "NUM" with the hexadecimal value 0X9876 using the EQU directive. The code loads the
value of "NUM" into register R1 using the LDR (Load Register) instruction. It performs bitwise
AND operations to extract specific bits from R1 and store them in registers R2, R3, and R4.It
performs a multiply and accumulate operation (MLA) using registers R3, R4, and R2, and stores
the result in register R5.The code continues to perform similar bitwise AND, logical shift right
(LSR), multiply and accumulate (MLA), and register value assignments to manipulate and
combine different sets of bits from the "NUM" value. It loads the memory address of the
"RESULT" label into register R6 using the LDR instruction. The final result, stored in register
R5, is stored at the memory location specified by the "RESULT" label using the STR (Store
Register) instruction. The code includes a label "HERE" followed by a branch instruction "B
HERE," creating an infinite loop that keeps the program execution at that point. It defines a new
data area named "DATA2" that contains read-write data.It defines a memory location labelled
"RESULT" with the initial value of 0X0 using the DCD (Define Constant Doubleword)
directive. The code ends with the END directive, marking the end of the code.

Output
Inference

From this program I have how to mesh all the instructions that I have learnt till now, that is
“MOV”, “LSR”, “MLA”, “AND”, “LDR”, “STR” into a single program that converts a decimal
number to hexadecimal number. I have also observed that the result gets stored in R5 and the
address of the result gets stored in R6. I have also learnt that the code here includes infinite loop
created by branch instruction (B) to create a continuous execution flow. The results have been
verified.

Hex to Decimal Conversion

Source Code

AREA hex_2_dec, CODE, READONLY

ENTRY

NUM EQU 0XFFF


MOV R2, #0
MOV R3, #0
MOV R4, #0
LDR R1, =NUM

LOOP
CMP R1, #1000
BLT NEXT
SUB R1, R1, #1000
ADD R2, R2, #1
B LOOP

NEXT
CMP R1, #100
BLT NEXT1
SUB R1, R1, #100
ADD R3, R3, #1
B NEXT

NEXT1
CMP R1, #10
BLT NEXT2
SUB R1, R1, #10
ADD R4, R4, #1
B NEXT1

NEXT2
LDR R5, =RESULT
ADD R1, R4, LSL #4
ADD R3, R2, LSL #4
STRB R1, [R5], #1
AREA DATA2, DATA, READWRITE
RESULT DCB 0X0, 0X0
END

Code Description

This code converts a hexadecimal number to decimal. It uses registers R2, R3, and R4 to keep
track of the decimal digits. It compares the value in register R1 to 1000, 100, and 10. If the value
is greater, it subtracts the corresponding amount and increments the respective register. The
converted decimal digits are stored in memory at the "RESULT" label. The program includes an
infinite loop to continuously convert numbers. The "RESULT" memory location is initialized
with the values 0x0 and 0x0.
Output

Inference

Overall, the code initializes two registers, R1 and R2, with the values 8 and 4, respectively. Then
it performs a reverse subtraction with the carry operation, subtracting the value in R2 from the
value in R1 along with the carry flag (if set), and stores the result in R3. After execution, the
code stops.

Cessation Of this Lab

We can conclude that we have seen different aspects of programming, including arithmetic
operations, bitwise operations, memory access, control flow, and data manipulation. The codes
demonstrate familiarity with assembly language concepts such as registers, memory areas,
constants, labels, and directives. The instructions used in the codes are consistent with standard
assembly language syntax and conventions. The codes showcase an understanding of how to
perform calculations, comparisons, branching, looping, and data manipulation in assembly
language.

You might also like