• No results found

Integer Math Instructions

9.1 Four-Function Math

Table 9-1 lists the statement list instructions that you can use to add, subtract, multiply, and divide integers (16 bits) and double integers (32 bits).

Table 9-1 Four-Function Math Instructions for Integers and Double Integers

Instruction Size in Bits

Function

+I 16 Adds the contents of the low words of accumulators 1 and 2 and stores the result in the low word of accumulator 1.

–I 16

Subtracts the contents of the low word of accumulator 1 from the contents of the low word of accumulator 2 and stores the result in the low word of accumulator 1.

I 16 Multiplies the contents of the low words of accumulators 1 and 2 and stores the result (32 bits) in accumulator 1.

/I 16

Divides the contents of the low word of accumulator 2 by the contents of the low word of accumulator 1. The result is stored in the low word of accumulator 1. Any whole remainder is stored in the high word of accumulator 1.

+D 32 Adds the contents of accumulators 1 and 2 and stores the result in accumulator 1.

–D 32 Subtracts the contents of accumulator 1 from the contents of accumulator 2 and stores the result in accumulator 1.

D 32 Multiplies accumulator 1 by the contents of accumulator 2 and stores the result in accumulator 1.

/D 32 Divides the contents of accumulator 2 by the contents of accumulator 1. The result is stored in accumulator 1.

MOD 32

Divides the contents of accumulator 2 by the contents of accumulator 1 and stores the remainder as a result in accumulator 1.

The function descriptions in Table 9-1 point out that the math operations combine the contents of accumulators 1 and 2. The result is stored in

accumulator 1. The old contents of accumulator 1 is shifted to accumulator 2.

The contents of accumulator 2 remains unchanged.

In the case of CPUs with four accumulators, the contents of accumulator 3 is then copied into accumulator 2 and the contents of accumulator 4 into accumulator 3. The old contents of accumulator 4 remains unchanged.

Description

Relationship of Math Operators to Accumulators

Integer Math Instructions

The Add Accumulators 1 and 2 As Integer instruction (+I) tells the CPU to add the contents of the low word of accumulator 1 and the low word of accumulator 2 and store the result in the low word of accumulator 1. This operation overwrites the old contents of the low word of accumulator 1. The old contents of accumulator 2 and the high word of accumulator 1 remain unchanged (see Figure 9-1). A sample program follows the figure.

Accumulator 2

Figure 9-1 Adding Two Integers

The Add Accumulators 1 and 2 As Integer instruction (+I) tells the CPU to add the contents of the low word of accumulator 1 and the low word of accumulator 2 and store the result in the low word of accumulator 1. This operation overwrites the old contents of the low word of accumulator 1. It then copies the contents of accumulator 3 to accumulator 2 and the contents of accumulator 4 to accumulator 3. Accumulator 4 and the high word of accumulator 1 remain unchanged (see Figure 9-2).

Accumulator 2 Accumulator 2

31 16 15 0

Accumulator 3 Accumulator 3

31 16 15 0

VI V VIII VII

Accumulator 4 Accumulator 4

31 16 15 0

VIII VII VIII VII

Accumulator contents in CPUs with two Accumulators

Combining two Integers (16 bits) in CPUs with four Accumulators

Integer Math Instructions

STL Explanation L MW10

L DBW12

+I

T DBW14

Load the value of memory word MW10 into accumulator 1.

Load the value of data word DBW12 into accumulator 1. The old contents of accumulator 1 are shifted to

accumulator 2.

The CPU interprets the contents of the low words of accumulators 1 and 2 as 16-bit integers, adds them, and stores the result in the low word of accumulator 1.

Transfer the contents of the low word of accumulator 1 (the result) to data word DBW14.

The math instructions affect the following bits of the status word:

 CC 1 and CC 0

 OV

 OS

A hyphen (–) entered in a bit column of the table means that the bit in question is not affected by the result of the integer math operation. You can use the instructions in Table 9-5 to evaluate these bits of the status word

Table 9-2 Signal State of Bits in the Status Word for Integer Math Result in Valid Range

Valid Range for an Integer (16 Bits) and Doubleg g Bits of Status Word

Integer (32 Bits) Result CC 1 CC 0 OV OS

0 (zero) 0 0 0

I: -32,768 v Result t 0 (negative number)

D: -2,147,483,648 v Result t 0 (negative number) 0 1 0 I: 32,767 w Result u0 (positive number)

D 2,147,483,647 w Result u0 (positive number) 1 0 0

Table 9-3 Signal State of Bits in the Status Word for Integer Math Result That Is Not in Valid Range

Range Not Valid for a Double Integer Resultg g Bits of Status Word

(32 Bits) CC 1 CC 0 OV OS

I: Result u 32,767 (positive number)

D: Result u 2,147,483,647 (positive number) 1 0 1 1 I: t –32,768 (negative number)

D: Result t -2,147,483,648 (negative number) 0 1 1 1 Evaluating the Bits

in the Status Word

Valid Result

Invalid Result

Integer Math Instructions

Table 9-4 Signal State of Bits in the Status Word for Double Integer Math Instructions +D, /D, and MOD

Instruction

Bits of Status Word Instruct on

CC 1 CC 0 OV OS

+D: Result = -4,294,967,296 0 0 1 1

/D or MOD: Division by 0 has occurred. 1 1 1 1

Table 9-5 Instructions That Evaluate the CC 1, CC 0, OV, and OS Bits Instruction Reference to Bit of

the Status Word or Jump Label

Bits in Status Word That Are Evaluated (indicated by an X)

Section in This Manual

A,O,X,AN,ON,XN >0, <0, <>0, >=0, <=0,

==0, UO, OV, OS

CC 1, CC 0, OV, OS 5.3

JO <jump label> OV 16.4

JOS <jump label> OS 16.4

JUO <jump label> CC 1 and CC 0 16.5

JZ <jump label> CC 1 and CC 0 16.5

JN <jump label> CC 1 and CC 0 16.5

JP <jump label> CC 1 and CC 0 16.5

JM <jump label> CC 1 and CC 0 16.5

JMZ <jump label> CC 1 and CC 0 16.5

JPZ <jump label> CC 1 and CC 0 16.5

Integer Math Instructions