35
At its very core, a computer is capable of only performing 2 things: ADD COMPARE We have already discussed some of the arithmetic, let’s look at the logical operations

At its very core, a computer is capable of only performing 2 things: ADD COMPARE

  • Upload
    obelia

  • View
    32

  • Download
    0

Embed Size (px)

DESCRIPTION

At its very core, a computer is capable of only performing 2 things: ADD COMPARE We have already discussed some of the arithmetic, let’s look at the logical operations. Compare Packed Decimal. Compare two packed decimal fields: CPM1(L1),M2(L2) Compare first field to second field - PowerPoint PPT Presentation

Citation preview

Page 1: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

At its very core, a computer is capable of only performing 2 things:

• ADD• COMPARE

We have already discussed some of the arithmetic, let’s look at the logical operations

Page 2: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Compare Packed Decimal

• Compare two packed decimal fields:– CP M1(L1),M2(L2)

• Compare first field to second field• Set a condition code based on results

– M1(L1) = M2(L2)– M1(L1) > M2(L2)– M1(L1) < M2(L2)

• Then test the condition code• Maybe you remember that the condition

code is located within the PSW

Page 3: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Condition Code

• 4 bits located within the processor (PSW bits: 18-19)

Cond. Code

PSW bits 18 & 19

M1(L1) = M2(L2) 1000 00M1(L1) < M2(L2) 0100 01M1(L1) > M2(L2) 0010 10

Not used --- 11

Page 4: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

So How Do You Test CC?

• Branch-on-Condition Instruction

07 Mask1 R2

47 B2X2Mask1 D2

0 8 12 15

BCR

0 8 12 16 20 31

BC

Page 5: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

So How Does It Work?

• Compare Mask and CC

• If corresponding bits are both 1, then Branch to address in Operand 2

• If corresponding bits are not 1, then fall through to the next instruction (don’t Branch)

CP TC-HRS,40HRSBC 2,CALC-OT * or BH CALC-OT

In the example, Time-Card Hours Worked is compared to 40 Hours. If Time-Card Hours is greater than 40 Hours, then Branch to Calculate Overtime Pay. If Time-Card Hours is not greater than 40, fall through to the instruction following the BC. Of course, the field names have also been defined …40HRS DC PL2’40’TC-HRS DS CL2

Page 6: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Another Example

CP FIELDA,FIELDBBC 8,SAME (BE)BC 4,LOWER (BL)BC 2,HIGHER (BH)

00 01 23 45 67 8C 00 08 76 54 32 1C

FIELDA FIELDB

Which Branch is taken? ______________LOWER

Page 7: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Now, that more or less takes care of COMPARE …Let’s Take a Look at the ADD side of a processor

• Of course, it seems that a computer can do more than just ADD and COMPARE. There are instructions that let you Subtract, Multiply, and Divide as well.

• However, the processor converts Subtract, Divide, and Multiply into an ADD, then performs the arithmetic. Multiply is simple: just perform addition of the values by the number of times of the multiplier.

• Subtraction and Division are a little more complicated. First, the value being added needs to be converted into its twos-complement form, then the numbers are added (or for Division, added the number of times of the value of the divisor.

• All of this has very little importance to us.

Page 8: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Packed Decimal Numbers

• Manipulating numbers in their decimal format is generally used for those numbers that are read into memory from an external source. These values are always read-in in their zoned-decimal format. They are characters: a single byte per character. The numbers appear as a string of characters, such as ‘F1F2F3F4F5F6F7F8F9F0’.

• These numbers are characters and, therefore, cannot be manipulated.• They must be converted from zoned-decimal to packed-decimal.

They would appear without their zone character ‘1234567890F’ or ‘1234567890C’ in memory. To accomplish this necessary change in the look of the numbers, use the PACK instruction, such as:

PACK PACKNO,ZONENO

Page 9: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

PACK Instruction

In machine format, PACK is an SS-type instruction (memory-to-memory)

‘F2’ is the op-code and is 1 byte in length. L1 and L2 represent the length (in bytes) of each of the operands in memory. They do not need to be the same length. Each pair of B1D1 and B2D2 are the two memory locations. In the PACK instruction example on the prior slide, B1D1 is the PACKNO field and B2D2 is the ZONENO field.

When executed, the format of the B2D2 (ZONENO) operand is changed from zoned to packed, and the result is placed in the B1D1 (PACKNO) operand.

Page 10: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

PACK Instruction

As the zoned value of the B2D2 operand is converted to packed, the numeric bits of each byte are treated as a digit and the zoned bits are ignored, except the zoned bits in the rightmost byte, which are treated as a sign.

The sign and digits are moved unchanged to the first operand and are not checked for validity. If the B1D1 field is larger than needed, zeros will pad to the left. If B1D1 is too small, truncation takes place on the left.

Assume, in the prior slide’s example, that ZONENO is 8 bytes and PACKNO is 6 bytes. (PACK PACKNO,ZONENO)

PACKNO DS CL6ZONENO DC CL8’24681357’

ZONENO Contents: ‘F2F4F6F8F1F3F5F7’

Page 11: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

When the PACK instruction has executed …

PACK PACKNO,ZONENO

Before AfterPACKNO n/a 00002468135C

ZONENO F2F4F6F8F1F3F5F7 F2F4F6F8F1F3F5F7

The result of the instruction’s execution is the 6-bytes (padded with zeros) that you can now use for decimal arithmetic. The right-most character (‘C’) is the positive sign character (it may also be ‘F’). The character ‘D’ identifies a negative value. You need to be careful in determining the proper length for the B1D1 operand.

Note: PACK does not set the cond. Code.

Page 12: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

ADD packed decimal numbers

The second operand (B2D2) is added to the first operand (B1D1), and the resulting sum is place at the first operand location. Operands are in packed-decimal format before and after execution. The addition is algebraic and takes into account the value of the sign bits.

If the first operand is too short to contain the sum, a decimal overflow occurs (cond.code = 3). If B1D1 is longer than the length needed, padding is done with leading zeros.

Example on the next slide

Page 13: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Add Packed Instruction Example

Assume that the sign, packed-decimal number at location ADDER (4 bytes) is to be added to the signed, packed-decimal number at location ADDEND (3 bytes). For this example, also assume that R12 points to ADDEND and R13 points to ADDED.

ADDER ADDENDBefore 01 12 34 5C 38 46 0D

After 73 88 5C 38 46 0D

AP ADDEND(3),ADDER(4)- or –

AP 0(3,12),0(4,13)

Note that the signs are different. In effect, the values are subtracted. There is no overflow since the sum fits in the first operand location. Condition code is set to 2: result is greater than 0.

Page 14: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Zero and Add Pack Instruction (ZAP)

The second operand is placed at the first operand location. This is the equivalent of an add to zero. All operands are in packed decimal format. The condition code is set: 0 = result is zero, 1 = result is negative, 2 = result is positive, 3 = overflow.

Examples follow a little later is this slide presentation.

Page 15: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Subtract Packed Instruction

Subtract is very similar to Add, except that the result is different.

The second operand (B2D2) is subtracted from the first operand (B1D1) and the resulting difference is placed at the first-operand location (B1D1). The operands and results are in packed-decimal format.

Example on the next slide

Page 16: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Subtract Packed Instruction Example

00303C FB21 404A 4048 25 SUBTR1 SP P2,P1 P2= 04 44 4C003042 FB11 4048 4048 26 SUBTR2 SP P1,P1 P1= 00 0C * :003048 123C 28 P1 DC P’123’ P1= 12 3C00304A 04567C 29 P2 DC P’4567’ P2= 04 56 7C

SUBTR2 clears a field to zeros. Subtracting a field from itself, as in SUBTR2, is more efficient but requires valid packed data in operand 1.SP sets the condition code: 0 = result is 0, 1 = result is negative, 2 = result is positive, 3 = result overflow.

Page 17: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Multiply Packed Instruction

The product of the first operand (multiplicand) and the second operand (multiplier) is placed at the first operand location. All operands are in packed decimal format.

The multiplier length cannot exceed 15 digits (L2 cannot be greater than 7) and must be less length than the multiplicand.

The multiplicand must have at least as many bytes of leftmost zeros as the total number of bytes in the multiplier, otherwise the product will not fit when the instruction has executed.

All values are packed decimal and the condition code is not set.

Page 18: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Multiply Packed Example

ZAP PRODUCT,MULTPLD 00 00 12 34 56 0CMP PRODUCT,MULTPLR 01 17 28 32 00 0C

* :MULTPLD DC PL4’1234560’MULTPLR DC PL2’950’PRODUCT DS PL6

Recall that the ZAP instruction is like a Move, moving the second operand to the first operand assuming both fields are formatted as packed decimal. To the right of the two instructions (as comments) you can see the value of PRODUCT after each instruction executes.

PRODUCT is defined as 6 bytes (the sum byte count of MULTPLR and MULTPLD lengths.

Page 19: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Another Multiply Packed Instruction Example

50 * MULTIPLY, GENERATE 3 DECIMALS, ROUND:51 * -------------------------------------52 ZAP WAGE,HRS 00 00 01 20 5C53 MP WAGE,RATE 00 10 30 27 5C MULTIPLY54 SRP WAGE,63.5 00 01 03 02 8C SHIFT/ROUND55 * :56 HRS DC P’120.5’ 01 20 5C57 RATE DC P’8.55’ 85 5C58 WAGE DS PL5

This example multiplies a 3-byte field, HRS, by a 2-byte field, RATE. The product, WAGE, is therefore defined as 5 bytes in length. Since HRS has one decimal and RATE has 2, the generated product has three implied decimal places. Because only two decimal places are required for dollars and cents, the unwanted rightmost digit is rounded. A Shift and Round Packed instruction (SRP) shifts the product one digit to the right, leaving two decimal places. To the right of the instructions, appearing as comments is the value of WAGE after each instruction executes.

Page 20: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Divide Packed Decimal Instruction

Aren’t all these packed-decimal instructions beginning to look alike? Unfortunately the Divide instruction is a little more complicated than the others.

The first operand (B1D1), which is the dividend, is divided by the second operand (B2D2) and divisor. The resulting quotient and remainder are placed at the first operand location. All values must be packed decimal format.

The quotient is placed in the leftmost portion of the first operand. The number of bytes in the quotient is equal to the difference between the dividend and divisor lengths (L1-L2). The remainder is placed in the rightmost portion of the same operand and is the same length as the divisor. The condition code is not changed.

Page 21: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

A Typical Decimal Divide Instruction Example

ZAP RESULT,DIVDNDDP RESULT,DIVSOR

* :DIVDND DS PL4 DividendDIVSOR DS PL1 DivisorRESULT DS PL5 Quotient/Remainder

In the typical situation, you do not know the values that are to be divided, so you plan for all possible situations. Divide by zero is not allowed, so your program will be abnormally terminated. The next worst case is when the divisor is one. This means that the quotient will equal (and cannot exceed) the value of the dividend.

1234567 / 1 = 1234567

Dividend DivisorQuotient = Dividend

Page 22: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

A Typical Decimal Divide Instruction Example

A convention is to provide the length of the quotient equal at least to that of the dividend. In addition, the length of the remainder always equals that of the divisor. Therefore, you should define the operand 1 field to be a length of at least the dividend plus the divisor. The result will be padded to the left with leading zeros. Here is a more realistic example:

ZAP RESULT,DIVDND 00 12 34 56 7CDP RESULT,DIVSOR 04 11 52 2C 1C

* :DIVDND DC PL4’1234567’ DividendDIVSOR DC PL1’3’ DivisorRESULT DS PL5 Quotient/Remainder

Since DIVDND contains 4 bytes and DIVSOR contains 1 byte, RESULT should contain at least 5 bytes, although it may be longer.The ZPA instruction initializes the dividend in RESULT. After the DP, the first 4 bytes of RESULT contain the quotient, with its sign, and the fifth byte contains the remainder, with its sign. The reaminder is 1 byte, the same length as the divisor.

Page 23: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Now that the decimal arithmetic is done, what’s next?

Many times you have to provide for an implied decimal point, so you can use the following rule:

The number of decimal places in a quotient equals the number in the dividend minus the number in the divisor.

For example, if the dividend contains four decimal places, and the divisor contains one, then the quotient has three:

dividend xx.xxxxdivisor x.x

= xx.xxx quotient

Page 24: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

And finally ? … UNPK

Now that all the decimal arithmetic is done in our program, we have a result that is a packed decimal number. This number is not printable since it is not character numbers. So we need to convert the packed-decimal numbers to zoned-decimal numbers. For this, use the UNPK (unpack) instruction.

UNPK is pretty much the opposite of the PACK instruction. The format of the second operand (B2D2) is changed from packed to zoned, and the result is placed at the first-operand location.

Signs are placed in the first-operand location unchanged. Zone bits with ‘1111’ are supplied for all bytes except the rightmost byte which is the sign. Padding takes place to the left, truncation also takes place on the left. The condition code remains unchanged.

Page 25: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

UNPK instruction example

UNPK ZONED,PACKED* :PACKED DC PL3’12345’ 12 34 5C

ZONED DS ZL5 F1 F2 F3 F4 C5

On the right, UNPK reverses the PACKed sign ‘C’ and the digit ‘5’ in the receiving ZONEd field. All other digits are placed in the receiving filed with a zone of ‘F’. Now the receiving field is printable (except for the rightmost byte).

To take care of the rightmost byte, you can use something like the OI (Or Immediate) instruction to change the sign field in the receiving field to an ‘F’. For the ZONED field above, this would appear as:

OI ZONED+4,’F0’

The rightmost byte now is changed to ‘F5’ and will print as the digit 5.

Page 26: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Input/Output Operations

Connecting to external data(see page 90)

Page 27: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Several Requirements

• A file or data set (on a device)• An area of memory describing the data file being used (DCB)• The file must be “connected” to the program using it

(OPEN/CLOSE)• An area in memory to stage the records being read or written

(buffer) – usually defined by Define Storage (DS) and probably subdivided:

BUFFER DS 0CL80

Page 28: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

data 1

data 1

data 2

data 2

DCBfor Input

input file output file

DCBfor

Output

Program

Input/Output Operations

StartSTMOPENOPEN : :

OPEN identifies the DCB that describes the file being OPEN’d. To retrieve a record from the OPEN file, issue GET. Get identifies the DCB (GET from which file…) and identifies the buffer into which to GET the record.

OPEN DCB1,INPUT----

GET DCB1,DATA1

Page 29: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Order of an I/O Operation

• Input file is available• Program OPEN’s the file – completes filling in DCB • Read (GET) the first record• O/S performs the request while program waits • Program is notified that operation has completed – I/O Interrupt• Program processes the record• Program loops through reading the records from the file• CLOSE the file when done – DCB EODAD=eof-rtn entry pt.

Page 30: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

AND In the Program

OPEN dcbname :

NXTREC GET dcbname,inarea : B NXTREC

eofrtn CLOSE dcbname

inarea DS CLnndcbname DCB LRECL=nn,RECFM=F,

MACRF=G,EODAD=eofrtn,DDNAME=filename

Page 31: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Output Operation Example

OPEN dcbname :

NXTREC PUT dcbname,outarea : B NXTRECCLOSE dcbname

outarea DS CLnndcbname DCB LRECL=nn,RECFM=F,

MACRF=G,DDNAME=filename

There is no EODAD= parameter for an output DCB since an end-of-file condition doesn’t occur on output files.

Page 32: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

NOTE: When using PC/370 Emulator

• PC sees data as ASCII• Mainframe sees data as EBCDIC• Data needs to appear on your PC as though it is mainframe

data

• So …– Before every OPEN, modify the DCB by coding:

OI dcbname+10,X’08’

so PC sees data as EBCDIC

Page 33: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

BEGIN STARTREGS:

*** OPEN FILESOI INDCB+10,X’08’OPEN INDCB,INPUTOI OUTDCB+10,X’08’OPEN OUTDCB,OUTPUT:

LOOP GET INDCB,INBUFFADDand more …PUT OUTDCB,OUTBUFFB LOOP

*** DEFINE FILESINDCB DCB LREC=80,RECFM=F,EODAD=EOFRTN …OUTDCB DCB LREC=80,RECFM=F,MACRF=P …*** DEFINE BUFFERSINBUFF DS 0CL80EMP# DS CL6

DS CL1EMPNAME DS CL20

:OUTBUFF DC CL80’ ‘ * 80 blanks

:

Page 34: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

*** READ INPUT & WRITE OUTPUT TO FILESTART 0

* REGSBEGIN BEGIN**** OPEN FILES

OI INDCB+10,X'08'OPEN INDCB,INPUTOI OUTDCB+10,X'08'OPEN OUTDCB,OUTPUT

* :LOOP GET INDCB,INBUFF MVC OUTBUFF,INBUFF* and more …

PUT OUTDCB,OUTBUFF WTO OUTBUFF

B LOOPEOFRTN CLOSE INDCB

CLOSE OUTDCBRETURN

*** DEFINE FILES*********-*********-*********-*********-*********-*********-*********-**INDCB DCB RECFM=F,EODAD=EOFRTN,DDNAME=F:\TEST\NAMES.TXT, X DSORG=S,MACRF=G,LRECL=29OUTDCB DCB RECFM=FB,MACRF=P,DSORG=S,LRECL=29, X DDNAME=F:\TEST\NAMES1.TXT*** DEFINE BUFFERSINBUFF DS 0CL27EMP# DS CL6

DS CL1EMPNAME DS CL20OUTBUFF DC CL27' ' * 27 blanksCARRCTRL DC XL2'0D25'

END BEGIN

SAMPLE WORKING PROGRAM WITH I/O

Page 35: At its very core, a computer is capable of only performing 2 things: ADD COMPARE

Console Output of

Sample Working Program

Execute the program step followed by the console output from the WTO in the program.

Linkage Editor step execution

Assemble the program