microcontrollers lab

alialmuhanpb.97
lab_3.pdf

1/3

Fall 2018

EGEE-280 Lab 3

Lab Objectives

This lab makes use of:

• Branch instructions to implement flow control in assembly programs

• Arithmetic instructions

• Instructions to process signed integers

Notes on Branching Instructions

The next set of commands to be discussed will be conditional branch commands. These are the logical flow operations available to you in assembly. All conditional branch commands use the CCR to determine to branch or not to branch. You should remember that after nearly every command, the CCR is updated to reflect the result of the previous command.

There are two classes of conditional branch commands: signed and unsigned.

The signed conditional branches expect two-complement signed numbers, while the unsigned ones do not. For example, if you wish to compare two numbers and branch if the first is larger than the second, you need to know if you mean positive definite numbers, or positive and negative numbers.

Suppose accumulator A has $10 and accumulator B has $FF. If both numbers are positive, A contains the decimal value of 16, and B contains 255. If you were to compare these two values, then do a BHI (Branch Higher than, unsigned), the branch would not go because A is smaller than B. However, if you did a BGT (Branch Greater Than, signed), now A still has the value of 10, but B contains -1 so the branch would execute.

Some conditional branch commands are invariant to signed and unsigned like BNE and BEQ. BNE is Branch Not Equal, and BEQ is Branch if Equal. Both these commands look at the Z bit, which is set (has ‘1’ value) if the result is a zero. These two branch commands will be very common for your use.

You should also notice that there are two commands for each branch logic, Bxx (e.g., BNE) and LBxx (e.g., LBNE). The L stands for “Long”. If you use the short branch commands (i.e., no “L”), you can only jump from -128 to 127 addresses from your current location. This is because the assembler uses an 8-bit offset for the addressing. If you use long branch commands (i.e., with a “L” in the beginning), the assembler uses a 16-bit offset and thus you can jump anywhere in memory.

2/3

Tasks

Write an assembly program as specified below:

1. Declare a variable in RAM called NUMNEG. This variable is 1-byte long and is used to store the number of the negative numbers scanned from the source array.

2. Declare a variable in RAM called TOTAL. This variable is 1-byte long and is used to store the total of all elements in the source array.

3. In the RAM section create an array of 8-bit signed integers that ends with zero (the last element of the array is a zero). Name that array SOURCE.

4. Immediately after the source array is the destination array called DEST. 5. Program operations:

a. Copy each element from the source array to the destination array. i. Count the number of the negative elements in the array.

ii. Calculate the running sum of all elements in the source array. b. The copy operation iterates and advance through the source array.

i. It stops whenever it encounters a value zero in the source array after copying the zero value to the destination array.

c. When finishes the results are posted in the two variables: i. NUMNEG: contains the count of negative elements in the source array

ii. TOTAL: contains the sum of all elements in the source array.

d. Constraints: i. Your program must use the names, data, and addresses exactly as

shown below (for grading purposes). ii. You cannot use explicit memory addressing (for example do not use

STAA $100D) iii. You must use variables or registers. iv. Branch instructions must be used. v. How the memory should look after your program finishes:

Source Block: Destination Block

SOURCE → $2006 7 DEST → $200D 7

$2007 -63 $200E -63

$2008 8 $200F 8

$2009 115 $2010 115

$200A -44 $2011 -44

$200B -15 $2012 -15

$200C 0 $2013 0

NUMNEG → $2000 3

TOTAL → $2001 8

Copy from Source Block to Destination Block

3/3

Report

Your report shall include the followings: 1) Introduction: Describe in your own words the objectives of this lab and the required

information and concepts to get this lab done.

2) Complete assembly source code in text (not a screenshot) copied from Code Warrior source code window. There should be a text comment in your source code showing your full name and indicate “Lab 3 – Branching”

3) The flowchart diagram of your program

4) Screenshots:

a. One screenshot showing the initial memory contents of the complete source and destination arrays

b. Screenshots showing the first full iteration of the loop. This means one screenshot for each step in the loop to show all affected:

• Registers

• Memory locations: NUMNEG, TOTAL, destination array

c. Screenshots at the end of the program showing:

• Destination array at the end of the program

• Variables NUMNEG and TOTAL

5) Summary and Conclusions

a. What were the key learning items you have achieved from this lab exercise?

b. What do you think regarding building the IF then ELSE, DO WHILE, WHILE-DO loops in assembly codes? Comparing to programming in high-level language such as C++?