EXERCISE 3-7

advertisement
EMCH 367
Fundamentals of Microcontrollers
Exercises 3-7
EXERCISE 3-7
GENERAL NOTE

Before starting this exercise, read Examples 3, 4, 5, 6, 7 and work through them using the
THRSim11 simulator.

Then, read this exercise. You will notice that the present exercise is practically identical with these
examples, only that is uses different numbers.

'Save As' this Word file in the personalized format indicated in the course pack.

To complete the exercise, you will have to do the following:

Type in text
The exercise has 6 sections. Do them in sequence.
OBJECTIVE
This exercise has the following objectives:

Review decimal numbers and decimal symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Review the decimal carry concept, i.e., when a result is outside the 0 – 9 range

Review 2-digit decimal arithmetic with carry

Prepare the ground for hex arithmetic

Introduce hex numbers and hex symbols: a, b, c, d, e, f

Introduce2-digit hex arithmetic

Introduce 2-digit hex carry as a repeated incrementation

Introduce 2-digit hex arithmetic with carry

Introduce the ‘lost carry’ concept

Introduce MSB carry

Introduce single precision and double precision register concepts
Note: To facilitate understanding, the same numbers are used in both decimal and the hex cases.
Dr. Victor Giurgiutiu
Page 1
2/16/2016
EMCH 367
Fundamentals of Microcontrollers
Exercises 3-7
PROGRAM
You have to write .asm code to perform the arithmetic operations listed below:
i) 8 + 7 = 15 (decimal) using the opcode mnemonics LDAA, LDAB, ABA
ii) $08 + $07 = $0f (hex) using the opcode mnemonics LDAA, LDAB, ABA
iii) 9 + 9 = 18 (decimal) using the opcode mnemonics LDAA, LDAB, ABA
iv) $09 + $09 = $12 (hex half carry) using the opcode mnemonics LDAA, ADDA
v) $09 + $09 = $12 (hex) using the opcode mnemonics LDAA, LDAB, ABA
vi) 12 + 29 = 41 (decimal) using the opcode mnemonics LDAA, LDAB, ABA
vii) $0c + $1d = $29 (hex) using the opcode mnemonics LDAA, LDAB, ABA
viii) $ff + $01 = $00 (MSB carry) using the opcode mnemonics LDAA, ADDA
ix) 255 + 1 = 0 (decimal equivalent of a MSB carry) using the opcode mnemonics LDAA, ADDA
CODING AND EXECUTION
Open THRSim11. Maximize THRSim11 window. Close the Commands window.
Open the template.asm file and save it with the personalized name LASTNAME_Firstname_Ex3-7
(Please respect the upper and lower conventions.).
Type your program in the .asm window after the line saying *Begin typing your code.
Assemble your file, tile the windows, set the breakpoint, and reset the registers.
Paste the screen capture here. 1 point(s)
SECTION: 8 + 7 = 15 (DECIMAL) USING THE OPCODE MNEMONICS LDAA, LDAB, ABA
Reset accA and accB by typing zero into them. Set the display option of accA and accB to Decimal.
Use the Step button to step through this section of your program.
Explain:

Which are the MSD and the LSD in the result 15? What do they represent? 3 point(s)

What is a decimal carry 2 point(s)

Did a decimal carry happen or not? Why? 3 point(s)
SECTION: $08 + $07 = $0F (HEX) USING THE OPCODE MNEMONICS LDAA, LDAB, ABA
Reset accA and accB by typing zero into them. Set the display option of accA and accB to hex.
Use the Step button to step through this section of your program.
Explain:
Dr. Victor Giurgiutiu
Page 2
2/16/2016
EMCH 367
Fundamentals of Microcontrollers
Exercises 3-7

How many digits are used to represent the number eight in hex on the simulator? Why? 3
point(s)

What is the meaning of concatenation? 2 point(s)

Which registers are concatenated and what is the result of the concatenation? 2 point(s)

What was the content of accD after you loaded $08 in accA? Why? 3 point(s)

What was the content of accD after you loaded $08 in accA and $07 in accB? Why?
2 point(s)

What is a hex carry 2 point(s)

Did a hex carry happen or not? Why? 2 point(s)
SECTION: 9 + 9 = 18 (DECIMAL) USING THE OPCODE MNEMONICS LDAA, LDAB, ABA
Reset accA and accB by typing zero into them. Set the display option of accA and accB to Decimal.
Use the Step button to step through this section of your program.
Explain:

What is a decimal carry 2 point(s)

Did a decimal carry happen or not? Why? 2 point(s)
SECTION: $09 + $09 = $12 (HEX) USING THE OPCODE MNEMONICS LDAA, ADDA,
Part I: start with $09 in accA and increment by 1 using the ADDA opcode
Reset accA and accB by typing zero into them. Set the display option of accA and accB to hex.
Use the Step button to step through this section of your program until the first has been executed.
Use the Step button to step further through this section of your program until accA has reached the
value $0f.
Use the Step button to do an additional step through this section of your program. AccA should change
to $10.
Use the Step button to do further step through this section of your program until the operation $09 +
$09 = $12 using is completed.
Explain:

Did a hex carry happen or not? Why? 3 point(s)

After how many executions has the carry happen? 2 point(s)

What was the value of accA before the hex carry? 2 point(s)

What was the value of accA after the hex carry? 2 point(s)

What was the final value of accA? 2 point(s)
Dr. Victor Giurgiutiu
Page 3
2/16/2016
EMCH 367
Fundamentals of Microcontrollers
Exercises 3-7
SECTION: $09 + $09 = $12 (HEX) USING THE OPCODE MNEMONICS LDAA, LDAB, ABA
Part II: start with $09 in accA and $09 in accB. Do the addition using the ABA opcode
Reset accA and accB by typing zero into them. Set the display option of accA and accB to hex.
Use the Step button to step through this section of your program.
Explain:

Did a hex carry happen or not? Why? 3 point(s)

What is the hex result of your operation? 2 point(s)

What is the MSD of the result? What does is signify? 3 point(s)

What is the LSD of the result? What does it signify? 3 point(s)

What is the decimal equivalent of the result? 3 point(s)
SECTION: 12 + 29 = 41 (DECIMAL) USING THE OPCODE MNEMONICS LDAA, LDAB, ABA
Reset accA and accB by typing zero into them. Set the display option of accA and accB to Decimal.
Use the Step button to step through this section of your program.
Explain:

Did a decimal carry happen or not? Why? 3 point(s)

What is the LSD in the result? 2 point(s)

What is the MSD in the result? 2 point(s)
SECTION: $0C + $1D = $29 (HEX) USING THE OPCODE MNEMONICS LDAA, LDAB, ABA
Reset accA and accB by typing zero into them. Set the display option of accA and accB to hex.
Use the Step button to step through this section of your program.
Explain:

Did a hex carry happen or not? Why? 2 point(s)

What is the hex result of your operation? 2 point(s)

What is the LSD of the result? What does it signify? 3 point(s)

What is the MSD of the result? What does is signify? 3 point(s)

What is the decimal equivalent of the result? 3 point(s)
$FF + $01 = $00 (HEX) USING THE OPCODE MNEMONICS LDAA, ADDA
Reset accA and accB by typing zero into them. Set the display option of accA and accB to hex.
Use the Step button to step through this section of your program.
Explain:

What is the result of your operation? 2 point(s)

Did a hex carry happen or not? Why? 2 point(s)
Dr. Victor Giurgiutiu
Page 4
2/16/2016
EMCH 367
Fundamentals of Microcontrollers

What type of carry was this? 2 point(s)

What is the name of the event that happened? 2 point(s)

How do you explain what happened? 3 point(s)
Exercises 3-7
255 + 1 = 0 (DECIMAL) USING THE OPCODE MNEMONICS LDAA, ADDA
Reset accA and accB by typing zero into them. Set the display option of accA and accB to decimal.
Use the Step button to step through this section of your program.
Explain:

What is the result of your operation? 3 point(s)

Did a decimal carry happen or not? Why? 3 point(s)

Did a hex carry happen or not? Why? 3 point(s)

What type of carry was this? 3 point(s)

How do you explain what happened? 3 point(s)
Dr. Victor Giurgiutiu
Page 5
2/16/2016
EMCH 367
Fundamentals of Microcontrollers
Exercises 3-7
WHAT YOU HAVE LEARNED
In this exercise, you have learned:

In decimal arithmetic, the base of the number system is ten. Ten independent symbols exist for
representing the first ten numbers in the decimal system: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. These
symbols are called digits.

The greatest decimal number that can be represented with a single decimal symbol is nine.

Decimal numbers greater than nine are represented with two or more digits. For example,
number fifteen is represented as 15, i.e. with two digits.

When the result of adding two decimal numbers is greater than nine, a carry takes place.

The symbol furthest to the left is called the most significant digit (MSD). In number 15, the most
significant digit is 1. It signifies the number of tens contained in the number. In our case, we
have only one ten, since 1 x ten = ten

The symbol furthest to the right is called the least significant digit (LSD). In number 15, the least
significant digit is 5. It signifies the number of units contained in the number. In our case, we
have five units, since 5 x units = five.

A complete number is assembled by adding the contribution of all digits, from LSD through
MSD. In number 15 there are two digits, LSD = 1, MSD = 5. The complete number is 1 x ten + 5
x units = ten + five = fifteen.

In decimal arithmetic, when the result of an addition is greater than nine, a carry occurs.

In hex arithmetic, the base of the number system is sixteen (hexadecimal). Sixteen independent
symbols exist for representing the first sixteen numbers in the hex system: 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, a, b, c, d, e, f.

The greatest hex number that can be represented with a single hex symbol is fifteen.

In single-precision 8-bit arithmetic, hex numbers are always represented with two digits, even if
the most significant digit (MSD) is zero. For example, during this exercise, the single-precision
accA and accB took values $08 and $07. After addition, accA became $0f.

In double precision 8-bit arithmetic, hex numbers are always represented with four digits, even if
the more significant ones are zero.

In hex arithmetic, single-precision numbers can be built into double precision numbers through
concatenation (i.e., by putting them side by side). For example, when accA was $08 and accB
was $07, accD, which is simply accA:accB, was $0807. This was not so in decimal arithmetic
(check this out by looking into the part of your program performing the decimal addition 8 + 7,
with accA, accB and accD set to decimal display).

In hex arithmetic, the symbol furthest to the left is also called the most significant digit (MSD). In
number $1d, the most significant digit is 1. It signifies the number of sixteens contained in the
number. In our case, we have only one sixteen, since 1 x sixteen = sixteen

In hex arithmetic, the symbol furthest to the right is called the least significant digit (LSD). In
number $1d, the least significant digit is d. It signifies the number of units contained in the
number. In our case, we have thirteen units, since the hex number d represents the number
thirteen, and d x units =thirteen.

The hex number $1d has the value twenty-nine, since sixteen plus thirteen makes twenty-nine.
Dr. Victor Giurgiutiu
Page 6
2/16/2016
EMCH 367

Fundamentals of Microcontrollers
Exercises 3-7
Addition can be viewed as a repeated incrementation. The number $09 was loaded in accA and
subsequently incremented nine times.

After the first six increments, from $09 to $0a, $0b, $0c, $0d, $0d, $0e, and $0f, the hex
symbols in the MSD were exhausted.

The next increment (the seventh) enacted a carry such that the MSD was increased
from 0 to 1, while the first placeholder (LSD) was reset to 0. At this stage, the value in
accA was $10. (This is by far different from the decimal 10).

The next and final increment (the eighth) incremented the LSD and the number in accA
moved from $10 to $11.

You saw the direct application of the hex addition using accA and accB, such that $09 + $08 =
$12.

You should note that the result $12 is the hex equivalent of the decimal result 18 (recall, 9 + 8 =
17).

You have observed an MSB carry. In our case, the machine has 2-digit hex precision, hence it
cannot store numbers with more than two digits. However, if to the number $ff we add $01, the
result should be $100 (Proof: $0ff + $001 = $100). Since we cannot store more than 2 hex
digits, the leading 1 is lost (‘lost carry’). The phenomenon is called MSB carry where MSB refers
to the most significant bit from binary representation.

New words and notations: digits, most significant digit (MSD), least significant digit (LSD), hex
arithmetic, hexadecimal, single precision, double precision, 8-bit arithmetic, concatenation,
addition with carry, hex arithmetic with carry, increment, placeholder, ‘lost carry’, most
significant bit (MSB), MSB carry.
Dr. Victor Giurgiutiu
Page 7
2/16/2016
EMCH 367
Fundamentals of Microcontrollers
Exercises 3-7

Dr. Victor Giurgiutiu
Page 1
2/16/2016
Download