• No results found

Converting Binary Coded Decimal Numbers and Integers

Conversion Instructions

12.1 Converting Binary Coded Decimal Numbers and Integers

You can use the following instructions to convert binary coded decimal numbers and integers to other types of numbers:

Mne-monic

Instruction Function

BTI BCD to Integer This instruction converts a binary coded decimal value in the low word of accumulator 1 to a 16-bit integer.

BTD BCD to Double Integer

This instruction converts a binary coded decimal value in accumulator 1 to a 32-bit integer.

ITB Integer to BCD This instruction converts a 16-bit integer in the low word of accumulator 1 to a binary coded decimal value.

ITD Integer to Double Integer

This instruction converts a 16-bit integer in the low word of accumulator 1 to a 32-bit integer.

DTB Double Integer to BCD

This instruction converts a 32-bit integer in accumulator 1 to a binary coded decimal value.

DTR Double Integer to Real

This instruction converts a 32-bit integer in accumulator 1 to a 32-bit IEEE floating-point number (real number).

Description

Conversion Instructions

The BCD to Integer (BTI) instruction converts a three-place binary coded decimal number (BCD number, see Figure 12-1) in the low word of accumulator 1 to a 16-bit integer. The BCD number can be in the range of -999 to +999. The result of the conversion is stored in the low word of accumulator 1.

Sign: 0000 stands for positive, 1111 stands for negative.

Value of the BCD number 15

102 101 100

14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

S S S S

Hundreds Tens Ones

These bits are not used during the transfer.

Figure 12-1 Structure of a Binary Coded Decimal Number to Be Converted to Integer

If a place of a BCD number is in the invalid range of 10 to 15, a BCDF error occurs during an attempted conversion. In this case, one of the following occurrences takes place:

 The CPU goes into the STOP mode. “BCD Conversion Error” is entered in the diagnostic buffer with event ID number 2521.

 If OB121 is programmed, it is called.

The following sample program includes a BTI instruction. Figure 12-2 shows how this instruction works.

STL Explanation

L MW10 BTI T MW20

Load the BCD value in memory word MW10 into accumulator 1.

Convert the BCD value to a 16-bit integer and store the result in accumulator 1.

Transfer the result to memory word MW20.

15... ...8 7... ...0

“ + ” “ 9 ” “ 1 ” “ 5 ”

BCD to Integer:

BTI

Conversion Instructions

The BCD to Double Integer (BTD) instruction converts a seven-place binary coded decimal number (BCD number, see Figure 12-3) in accumulator 1 to a 32-bit integer. The BCD number can be in the range of -9,999,999 to +9,999,999. The result of the conversion is stored in accumulator 1.

If a place of a BCD number is in the invalid range of 10 to 15, a BCDF error occurs during an attempted conversion. In this case, either one of the following occurrences takes place:

 The CPU goes into the STOP mode. “BCD Conversion Error” is entered in the diagnostic buffer with event ID number 2521.

 If OB121 is programmed, it is called.

31... ...16 15... ...0

S S S S

106 105 104 103 102 101 100

Sign: 0000 stands for positive, 1111 stands for negative.

Value of the BCD number

These bits are not used during the transfer.

Figure 12-3 Structure of a 32-Bit BCD Number to Be Converted to Double Integer The following sample program includes a BTD instruction. Figure 12-4 shows how this instruction works.

STL Explanation

L MD10 BTD T MD20

Load the BCD value in memory double word MD10 into accumulator 1.

Convert the BCD value to a 32-bit integer and store the result in accumulator 1.

Transfer the result to memory double word MD20.

31... ...16 15... ...0

0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 1 1 1 0 0 0 0 0 1 0 0 0 0 1

0 1 1 1

0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0 1 L MD10

BTD

T MD20

BCD to integer

“ + ” “ 0 ” “ 1 ” “ 5 ”

MD10

MD20

“157821”

“+157821”

“ 7 ” “ 8 ” “ 2 ” “ 1 ”

Figure 12-4 Using the BTD Instruction to Convert a BCD Number to a 32-Bit Integer BCD to Double

Integer: BTD

Conversion Instructions

The Integer to BCD (ITB) instruction converts a 16-bit integer in the low word of accumulator 1 to a three-place binary coded decimal number. The BCD number can be in the range of -999 to +999. The result of the conversion is stored in the low word of accumulator 1.

If the integer is too large to be represented in BCD format, no conversion takes place and the overflow (OV) and stored overflow (OS) bits of the status word (see Section 3.4) are set to a signal state of 1.

The following sample program includes an ITB instruction. Figure 12-5 shows how this instruction works.

STL Explanation

L MW10 ITB T MW20

Load the 16-bit integer value in memory word MW10 into accumulator 1.

Convert the 16-bit integer to a BCD value and store the result in accumulator 1.

Transfer the result to memory word MW20.

15... ...8 7... ...0

0 1 0 0

0 1 1 0 0 0 1 1 1 1 1 1

0 0 0 1 1 1 1 1

L MW10

ITB

T MW20 0 0 1 1

1 1 1 0

Integer to BCD

“ – ” “ 4 ” “ 1 ” “ 3 ”

MW10

MW20 “-413” BCD

“-413” integer

Figure 12-5 Using the ITB Instruction to Convert a 16-Bit Integer to a BCD Number Integer to BCD:

ITB

Conversion Instructions

The Integer to Double Integer (ITD) instruction converts a 16-bit integer in the low word of accumulator 1 to a 32-bit integer. The result of the conversion is stored in accumulator 1. The following sample program includes an ITD instruction. Figure 12-6 shows how this instruction works.

STL Explanation

L MW10 ITD T MD20

Load the 16-bit integer value in memory word MW10 into accumulator 1.

Convert the 16-bit integer value to a 32-bit integer and store the result in accumulator 1.

Transfer the result to memory double word MD20.

15... ...0

Integer (16-bit) to integer (32-bit) MW10

Figure 12-6 Using the ITD Instruction to Convert a 16-Bit Integer to a 32-Bit Integer

The Double Integer to BCD (DTB) instruction converts a 32-bit integer in accumulator 1 to a seven-place binary coded decimal value. The BCD number can be in the range of -9,999,999 to +9,999,999. The result of the conversion is stored in accumulator 1.

If the double integer is too large to be represented in BCD format, no conversion takes place and the overflow (OV) and stored overflow (OS) bits of the status word (see Section 3.4) are set to a signal state of 1.

The following sample program includes a DTB instruction. Figure 12-7 shows how this instruction works.

STL Explanation

L MD10 DTB T MD20

Load the 32-bit integer in memory double word MD10 into accumulator 1.

Convert the 32-bit integer to a BCD value and store the result in accumulator 1.

Transfer the result to memory double word MD20.

31... ...16 15... ...0 Figure 12-7 Using the DTB Instruction to Convert a 32-Bit Integer to a BCD Number

Integer to Double Integer: ITD

Double Integer to BCD: DTB

Conversion Instructions

The Double Integer to Real (DTR) instruction converts a 32-bit integer in accumulator 1 to a 32-bit IEEE floating-point number (real number). If necessary, the instruction rounds the result. The result of the conversion is stored in accumulator 1. The following sample program includes an DTR instruction. Figure 12-8 shows how this instruction works.

STL Explanation

L MD10 DTR

T MD20

Load the 32-bit integer in memory double word MD10 into accumulator 1.

Convert the 32-bit integer to a 32-bit IEEE floating-point value and store the result in accumulator 1.

Transfer the result to memory double word MD20.

31 ...0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 1 0 0

0 L MD10

DTR

T MD20

Integer (32-bit) to IEEE floating-point

1 bit

Sign of the mantissa 8-bit exponent

23-bit mantissa MD10

MD20

“+500”

integer

“+500”

IEEE

30... 22...

0 0 1 1 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

Figure 12-8 Using the DTR Instruction to Convert a 32-Bit Integer to an IEEE 32-Bit Floating-Point Number For a summary of the number conversions, see Figure 12-13 at the end of Section 12.2.

Double Integer to Real: DTR

Conversion Instructions