0% found this document useful (0 votes)
37 views7 pages

Lab Proposal

The document describes a lab assignment on memory instructions and branches in assembly language for microprocessors. It includes 5 tasks: 1) reading input from PinB and outputting to PortA, 2) grading a test score input from PinB and outputting the letter grade to PortA, 3) incrementing/decrementing a PortA value based on positive/negative PinB inputs, 4) computing the factorial of 5 using multiplication, and 5) evaluating a mathematical expression using memory locations and two registers. The document also covers branch instructions, compare instructions, conditional and unconditional branches, modeling control structures, and LDS and STS memory instructions.

Uploaded by

Muhammad Ishaq
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)
37 views7 pages

Lab Proposal

The document describes a lab assignment on memory instructions and branches in assembly language for microprocessors. It includes 5 tasks: 1) reading input from PinB and outputting to PortA, 2) grading a test score input from PinB and outputting the letter grade to PortA, 3) incrementing/decrementing a PortA value based on positive/negative PinB inputs, 4) computing the factorial of 5 using multiplication, and 5) evaluating a mathematical expression using memory locations and two registers. The document also covers branch instructions, compare instructions, conditional and unconditional branches, modeling control structures, and LDS and STS memory instructions.

Uploaded by

Muhammad Ishaq
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/ 7

Lab 4: Memory Instructions and Branches

EE222: Microprocessor Systems

Table of Contents
1 Administrivia ............................................................................................................... 2

1.1 Objectives ................................................................................................................... 2


1.2 Deliverable .................................................................................................................. 2

2 Hardware Resources.................................................................................................... 2
3. Branch Instructions 2 ................................................................................................... 3

3.1. Conditional Branches .................................................................................................. 3


3.2. Compare Instructions ................................................................................................. 3

3.3. Unconditional Branch Instructions .............................................................................. 4


3.4. Modelling Control Structures ....................................................................................... 4
4. Memory Instructions .................................................................................................. 5

4.1. LDS instruction (LoaD direct from data Space) ............................................................. 5


4.2. STS instruction (STore direct to data Space) ................................................................. 5

5. Lab Tasks ..................................................................................................................... 6


1 Administrivia
1.1 Objectives
By the end of this lab you will be able to;
• Implement branches in assembly language

• Understand data memory of ATmega16

• Use data memory to store and load data

1.2 Deliverable
You are required to submit a report including;
1. Source codes with “proper comments” in the beginning

of next lab.

2. Output screenshots of all the tasks.

2 Hardware Resources
No hardware implementation or resources are required for this lab.
3. Branch Instructions:
Branch instructions can be divided into two groups.
1. Conditional Branches
2. Unconditional Branches (Jumps)

3.1. Conditional Branches:


Conditional branch instructions execute when a specific condition is met. Otherwise the
branch instruction does not execute, simply the program counter increments to the next
instruction.

Table 1: AVR Conditional Branch (Jump) Instructions


BRLO Label Branch if C = 1
BRSH Label BRSH Branch if C = 0
BREQ Label Branch if Z = 1
BRNE Label Branch if Z = 0
BRMI Label Branch if N = 1
BRPL Label Branch if N = 0
BRVS Label Branch if V = 1
BRVC Label Branch if V = 0
BRGE Label Branch if S = 0
BRLT Label Branch if S = 1

3.2. Compare Instructions:


CP (Compare):
Compares the destination operand to the source operand. None of the registers are changed.
SUB instruction rules are followed.

Syntax
CP Rd, Rs Operands: 0 ≤ d ≤ 31, 0 ≤ r ≤ 31
(Operation: Rd-Rs)

This instruction effects the following flags.


HSVNZC
Forms if-else when used with conditional branch instruction.

CPI (Compare with Immediate):


This instruction performs a compare between register Rd and a constant. The register is
not changed. All conditional branches can be used after this instruction.

Syntax
CP Rd, K Operands: 16 ≤ d ≤ 31, 0 ≤ K ≤ 255
(Operation: Rd-K)
CP Example CPI Example
cp r4,r19 ; Compare r4 with r19 cpi r19,3 ; Compare r19 with 3
brne noteq ; Branch if r4 <> r19 brne error ; Branch if r19<>3
... ...
noteq: nop ; Branch destination (do error: nop ; Branch destination (do nothing)
nothing)

3.3. Unconditional Branch Instructions:


The unconditional branch is a jump in which control is transferred unconditionally to the
target location. In the AVR there are three unconditional branches:
Table 2: AVR Unconditional Branch (Jump) Instructions
JMP Label Jump

RJMP Label Relative jump

IJMP Label Indirect jump

3.4. Modelling Control Structures


3.4.1. If-Else

a → R14, b → R15, c → R16

i f (a == b){ LDI R16, 2


CP R14, R15
c = 1; BRNE done
LDI R16, 1
}
done :
else { ; rest of the program
c = 2;
}
3.4.2. While-Loop

sum → R16
while (sum < 10) Loop :
{
sum = sum + 1; SUBI R16, −1
} CPI R16, 10
BRLT Loop
; rest of the program
3.4.3. If-Else if-Else

number → R20, a → R16


i f (number == 1) LDI R16, 10
{ CPI R20, 1
a = 10; BREQ Done
LDI R16, 20
}
CPI R20, 2
else i f (number == 2) BREQ Done
{ LDI R16, 30
a = 20;
Done: ;
} rest of the program

else
{
a = 30;
}

4. Memory Instructions:
The AVR Instructions allows direct access to other locations in the data memory.
4.1. LDS instruction (LoaD direct from data Space)
LDS Rd, K

 load Rd with the contents of location K (0 <d< 31)


 K is an address between $0000 to $FFFF
The LDS instruction tells the CPU to load (copy) one byte from an address in the data
memory to the GPR. After this instruction is executed, the GPR will have the same value as
the location in the data memory. The location in the data memory can be in any part of the
data space; it can be one of the I/O registers, a location in the internal SRAM, or a GPR.
4.2. STS instruction (STore direct to data Space):
STS K, Rr
store register into location K
K is an address between $0000 to $FFFF
The STS instruction tells the CPU to store (copy) the contents of the GPR to an address
location in the data memory space. After this instruction is executed, the location in the
data space will have the same value as the GPR. The location can be in any part of the data
memory space; it can be one of the I/O registers, a location in the SRAM, or a GPR.
5. Lab Tasks

Task 1:
Write a simple program that takes user input on PinB and output the user data
on PortA.

Task 2:
Using the following table as a guide, write an assembly program that asks the user to enter
an integer test score between 0 and 100. The program should display the appropriate letter
grade:

Score Range Letter Grade


90 to 100 A
80 to 89 B
70 to 79 C
60 to 69 D
0 to 59 F

Add the following features to the above program;


1. The program must take the input data on PinB and output the letter grade on PortA.
2. Perform range checking on the user's input: display 0xFF if the test score is greater
than 100.
3. The program must continuously take the input from the user.
You will display result such that you will give inputs to show all grades (A to F), give out of
range input to print 0xFF.

Task 3:
Write a program that takes input from user on PinB and display the output on PortA
depending on the data input by the user.
1. The default value on PortA is 0.
2. If user inputs a positive value then the value must be incremented on PortA. The value
displayed on PortA will be 1, 2, 3, 4, …
3. If user inputs a negative value then must be decremented on PortA. The value
displayed on PortA will be 255, 254, 253, …
4. Move to Step 2.

Task 4:
Write a program to compute the factorial of number 5 using multiply instruction.
Task 5:
Implement the following arithmetic instructions in assembly:
|32*(5-8)+4-9|
1. Store all the numbers on the following memory locations.
Number Memory Location
32 0x250
5 0x251
8 0x252
4 0x253
9 0x254

2. Evaluate the expression using only two registers R16 and R17. Display the results in
register R16.
3. Check if the result is negative then take the absolute value of result. (Using 2’s
complement method).
(Hint: Use COM Rd instruction to take 1’s complement of the number)

4. Finally store the result in 0x255 memory location.

You might also like