Download as pdf or txt
Download as pdf or txt
You are on page 1of 77

12/12/2009

Assembler Programming Part 2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

Introduction

Assembler Language Part 2 provides instruction and practice in the use of the standard
Assembler Language instructions. The course covers executable instructions for data manipulation, including
loading and storing data, moving in main storage, bit manipulation, binary and decimal arithmetic, data
conversions and data editing.

The first unit, Data Manipulation Instructions, explains the syntax and function of Load, Store and Move
instructions.

The second unit, Basic MVS I/O Facilities, explains the characteristics of Data Set Definitions, syntax and
purpose of Data Control Blocks (DCBs) and the use of OPEN and CLOSE macros.

The third unit, Decimal Arithmetic, explains the use of Decimal Arithmetic, conversion and operation
decimal Arithmetic functions, Editing decimal data.

The fourth unit, Comparing and Branching explains the use of Condition Codes, numerical and logical
comparisons, the function of Branching Instructions and the framework of Structure Constructs.

The fifth unit, Fixed-Point Binary Arithmetic, explains the conversion between decimal and binary, and
functions of Fixed Point Binary Arithmetic Instructions.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

1
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Data Manipulation Instructions

Topics:
 Load and Store Instructions

 Move Instructions

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions

Unit Introduction

Using the General Purpose Registers (GPRs) is an important part of Assembler programming. You will use
GPRs as base registers in addressing, and as index registers. You will also use them as counters, or in
performing fixed-point binary arithmetic operations. This unit will describe machine instructions that are used
to load, store and move data between Main Storage and GPRs.

Specifically, you will learn about instructions that do the following:

• Load values into the general registers from main storage

• Store values from general registers into main storage

• Manipulate values stored in general registers

• Direct the movement of data from one main storage location to another or to a register

© Copyright IBM Corp., 2000, 2004. All rights reserved.

2
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Data Manipulation Instructions

Topics:
 Load and Store Instructions

 Move Instructions

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Load Operation

A load operation moves data from main storage Load (L) Instruction:
into a GPR. You will find this useful when RX Format – Register and Indexed Storage

performing fixed-point binary arithmetic, or using


data for addressing. The first two instructions you L R3,FWD1

will learn are Load (L) and Load Halfword (LH).


1st Operand 2nd Operand

FWD1
R3

00 00 00 00 07 3C EA 00

Target
Full word Source
Full word
07 3C EA 00

Copies FULWORD from main


Storage address beginning at
location FWD1 to GPR R3

CC setting: Condition Code is unchanged

© Copyright IBM Corp., 2000, 2004. All rights reserved.

3
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Load Operation

Load L
Example :
• The four bytes at the storage address specified by
operand-2 is loaded into the register specified by
operand-1 LEX1 L R1,REG1

REG1 DC F'80'
R1 EQU 1

* R1 before '00000000'
* R1 after '00000080'
58 RX BDDD

Format :

L R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Load Operation

Load Register LR
Example :
• The value of register operand-2 loaded into the register
specified by operand-1
LEX1 LR R4,R6

R4 EQU 4
R6 EQU 6

18 RR

Format :

LR R1,R2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

4
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Load Operation

Load Halfword LH
Example :
• The two bytes (halfword) located at the address specified
by operand-2 is loaded into the rightmost two bytes (bits
16-31) of the register specified by operand-1
• The left half of the register is loaded with zeros or LH R3,STDATA
ones according to the sign (leftmost bit) of the halfword
STDATA DS H
R3 EQU 3

48 RX BDDD

Format :

LH R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Load Operation

Load Positive Register LPR


Example :
• The absolute value of operand-2 is loaded into operand-1

LPR R3,R4

R3 EQU 3
R4 EQU 4

10 RR

Format :

LPR R1,R2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

5
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Load Operation

Load Negative Register LNR


Example :
• The two’s complement of absolute value of operand-2 is
loaded into operand-1

LNR R3,R4

R3 EQU 3
R4 EQU 4

11 RR

Format :

LNR R1,R2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Load Operation

Load Multiple LM
Example :
• The set of registers starting with operand-1 and ending
with operand-3 are loaded from storage location specified
by operand-2 EXMP1 LM R4,R6,REG1
• Register numbers are referred in cyclic way REG1 DS F
• R14,R5 means R14,R15,R0,R1,R2,R3,R4,R5 REG2 DS F
REG3 DS F
R4 EQU 4
R6 EQU 6

* Copy contents of REG1,REG2,REG3


* into R4,R5,R6
98 RR BDDD

Format :

LM R1,R3,D2(B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

6
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Load Operation

Load Address LA
Example :
• The actual value of operand-2 (effective address) is
loaded into the register specified by operand-1 .
LA R8,REG1+6

LA R8,0 Load ZERO to R8


LA R8,1(,R8) Increment R8 by 1
LA R8,2(,R8) Increment R8 by 2

REG1 DS F
R8 EQU 8

41 RX BDDD

Format :

LA R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Load and Test Instruction


The Load and Test (LTR) instruction is identical to
Load Register (LR), except that it sets the
condition code.
Load and Test (LTR):
RR Format - Register to Register
The instruction copies the contents of one GPR
(2nd Operand) to another GPR (1st Operand),
setting the CC to indicate if the result is zero, LTR R7,R3
negative or positive.

For example: LTR R7,R3 1st Operand 2nd Operand

R7 R3

00 00 00 00 07 3C EA 00

Target Source
Fullword Fullword
LTR

CC Setting – 0-Result is Zero


1-Result is Negative
2-Result is Positive

© Copyright IBM Corp., 2000, 2004. All rights reserved.

7
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Store Instruction

Store ST
Example :
• The register specified by operand-1 (R1) is stored at the
storage address specified by operand-2 . ST R4,STGAREA
• Operand-2 should be full word storage area

STGAREA DS F
R4 EQU 4

* Contents of R4 copied to STGAREA

50 RX BDDD

Format :

ST R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Store Instruction

Store Character STC


Example :
• Bits 24-31 (one-byte) of the register specified by operand-1
are stored at the storage address specified by operand-2 L R4,=X’C1C2C3C4’
STC R4,REGWORK

R4 EQU 4
REGWORK DC XL4’00000000’

* Store ‘D’ at REGWORK location

42 RX BDDD

Format :

STC R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

8
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Store Instruction

Store Character under Mask STCM


Example :
• Bytes from operand-1 are stored at the storage location
specified by operand-2 under control of the mask (M3) L R3,=X'A0B1C2D3'
MVC STCMDATA(4),XFF
m3=0001, similar to STC STCM R3,MASKF,STCMDATA
m3=0011, similar to STH
m3=1111, similar to ST
R3 EQU 3
m3=0111, store 24-bit address
STCMDATA DC XL4’00000000’
MASKF EQU X’F’
XFF DC XL8'FFFFFFFFFFFFFFFF'

BE RM BDDD * Data moved as if using ST instruction


* due to bit mask of 1111
Format :

STCM R1,M3,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Store Instruction

Store Halfword SH
Example :
• The rightmost two bytes or halfword (bits 16-31) of the
register specified by operand-1 are stored at the storage
address specified by operand-2
SH R3,STHDATA

STHDATA DC XL2’0000’
R3 EQU 3

* Ignore Bytes 1 and 2 and move data


40 RX BDDD * in bytes 3 & 4

Format :

STH R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

9
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Store Instruction

Store Multiple STM


Example :
• The set of registers starting with operand-1 and ending
with operand-3 are stored at the storage location specified
by operand-2 EXMP1 STM R4,R6,REG1
• Register numbers are referred in cyclic way REG1 DS F
• R14,R5 means R14,R15,R0,R1,R2,R3,R4,R5 REG2 DS F
REG3 DS F
R4 EQU 4
R6 EQU 6

* Copy R4 R5,R6 into storage locations


* starting from REG1
90 RR BDDD

Format :

STM R1,R3,D2(B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Store Instruction

What is the function of the Store Multiple


Instruction?
STM R14,R12,12(R13)
The Store Multiple (STM) instruction is used as Save Area
R13 Address
part of the standard housekeeping done at the
R14 01 42 77 FF FF FF FF FF
beginning of a program. This is performed to save
R15 0B 72 1A 80 FF FF FF FF
the register contents of a program's caller, so they
can later be restored before returning control to R0 2B 08 75 FF FF FF FF FF

the caller.

In fact, the first instruction frequently used is a


R11 0C 72 1B 64 FF FF FF FF
STM to perform this operation. An example is
R12 EF FF FF FF FF FF FF FF
shown on the right.
Source Target
For example: STM R14,R12,12(R13) Fullword Fullword
STM

© Copyright IBM Corp., 2000, 2004. All rights reserved.

10
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Inserting Characters

Insert Character IC
Example :
• The byte at address specified by operand-2 is
inserted into bit positions 24-31 of operand-1
• The remaining bits of R1 remain unchanged IC R8,=C’B’

R8 EQU 8

43 RX BDDD

Format :

IC R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Load and Store Instructions

Inserting Characters

Insert Character under Mask ICM


Example :
• Bytes from storage location specified by operand-2
are inserted into operand-1 under control of the
mask (M3) ICM R6,B'1001',FIELDB
• Each bit correspond to one byte in the operand-1
• The character inserted at a byte if the corresponding R6 EQU 6
bit is 1
* FIELDB: 12 34
* R6(before): 00 00 00 00
* R6 (after): 12 00 00 34

BF RM BDDD

Format :

ICM R1,M3,D2(B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

11
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Data Manipulation Instructions

Topics:
 Load and Store Instructions

 Move Instructions

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Move Instructions

Moving data

Move Immediate MVI


Example :
• Operand-2 (the immediate data that is the second byte
of the instruction itself) is moved to the storage address
specified by operand-1 MVI AREA,C'A'
MVI NAME+6,C'B'

92 I BDDD

Format :

MVI D1(B1),I2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

12
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Move Instructions

Moving data

Move Characters MVC


Example :
• The data string located at the storage address specified
by operand-2 is moved to the storage address specified MVC Z00,Z99
by operand-1 . Operand-2 remains unchanged.
• The number of bytes moved is determined by the length Z00 DC CL2'00‘
specified in the 2nd byte of the MVC instruction or Z99 DC CL2'99'
default length of the first operand

D2 L BDDD BDDD

Format :

MVC D1(L,B1),D2(B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Move Instructions

Moving data
Move Characters Long MVCL
• The data string identified by operand-2 is moved to the Example :
storage location identified by operand-1
• Both operands are EVEN-ODD register-pairs. The first R6 EQU 6
register needs to be loaded with the addresses of the data
strings. The second register needs to be loaded with the R7 EQU 7
length of each operand. R8 EQU 8
• The data strings may be different length. The operation ends R9 EQU 9
when the number of bytes specified by bits 40-63 of RECV DC C’SOMEDATA’
general register R1 + 1 have been moved SEND DC C’A LONGER FIELD’
• The high-order byte of the second register of operand-2 is
treated as the padding character LA R6,RECV
• As each character is moved the length registers are LH R7,=H’8’
decremented
LA R8,SEND
LH R9,=H‘14’
0E RR ICM R9,8,=C’ ‘ padding char
MVCL R6,R8
Format :

MVCL R1,R2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

13
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Move Instructions

PRTLINE

A very common programming task is to initialize


a field to some specific value. For example, you
may have a field called PRTLINE, used to hold a
line, intended to be written to the printer.

You might want to initialize this field to blanks,


PRTLINE DS CL133
build the print line and write it out, and then
repeat the series of operations for subsequent
print lines. To initialize the print line, look at the
code. Operation Define Character
STORAGE Length is 133

Suppose PRTLINE is defined as follows:

PRTLINE DS CL133

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Data Manipulation Instructions Topic: Move Instructions

PRTLINE (cont’d)

The most obvious way to initialize PRTLINE


would be to move a constant containing 133
blanks to PRTLINE.

You could eliminate the named constant by using MVC PRTLINE, BLANKS
a literal: .
.
MVC PRTLINE,=CL133‘ ' .
.
.

BLANKS DC CL133

© Copyright IBM Corp., 2000, 2004. All rights reserved.

14
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Basic MVS I/O Facilities

Topics:
 Defining Data Sets

 Data Transfer Macros

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities

Unit Introduction

The main purpose of most assembler programs is to input data, process it, and then print out
the resulting data. MVS provides a vast range of input/output (I/O) facilities and options that
help you accomplish this.

In this unit, a very small subset of these facilities will be presented, I/O macros that sequentially
read 80-byte input records and sequentially write 133-byte output records. These macros
enable you to read data from an input data set you create, and to write data to the printer.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

15
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Basic MVS I/O Facilities

Topics:
 Defining Data Sets

 Data Transfer Macros

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities Topic: Defining Data Sets

Processing Input and Output Data

The main purpose of most assembler programs is


to read in data (input), process it, and then print
out the resulting data (output).

The major steps for processing input and output I/O PROCESSING TASKS MVS MACRO

data in a program are shown. I/O macros are 1. Define the data set DCB
used to accomplish all of these tasks, except for
2. Define I/O areas DS,DC
the definition of I/O areas, which is done with
assembler instructions. 3. Make the data set available OPEN

4. Read input data GET

5. Write output data PUT

6. Free the data set CLOSE

© Copyright IBM Corp., 2000, 2004. All rights reserved.

16
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities Topic: Defining Data Sets

Elements of Defining Data Sets

Every Assembler program that processes I/O


must provide information to the Assembler about
the data sets to be processed. The data is
organized sequentially and processed by the DCB
(Data Control Block) macro in the same manner.
DATA SET
GET
The DCB macro instruction generates a large
block of constants in order to process the data
set. The block of constants is a set of data fields
DCB
(or parameters) that control the the data
RECORD1
set being processed.

EXE DCB PUT

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities Topic: Defining Data Sets

Elements of Defining Data Sets (cont’d)

Name Field
DCB Name
Data Control Block

INFILE DCB DDNAME=IN,RECFM=FB,LRECL=80,DSORG=PS,MACRF=GM, X Keyword=SIGN Value


EODAD=ENDDATA
MACRF=GM
DATA DATA DATA DATA DATA
Data Control FIELD , FIELD , FIELD , FIELD , FIELD , X
Block Macro
DATA
FIELD Key Word Operand

A DCB is a collection of data fields that contain information concerning the data set, including:
• The size and format of the records
• The I/O macros used to process the data
• The name and the current status of the data set

© Copyright IBM Corp., 2000, 2004. All rights reserved.

17
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities Topic: Defining Data Sets

Parameters

What are the parameters used in DCBs?

A variety of keyword parameters are used with DCB


input and output DCBs.
IN DCB DDNAME=INPUT, .....etc
The DDNAME parameter is used as a link to the
external file this DCB represents. The value of the JCL
DDNAME parameter must match the DDNAME
on the JCL DD statement for batch execution, or
//INPUT DD .....etc.
the FILE parameter on the ALLOCATE TSO
statement for interactive execution. TSO

ALLOCATE FILE(INPUT) ....etc.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities Topic: Defining Data Sets

Kinds of Parameters

The DSORG parameter specifies the organization


of the data set represented by this DCB. In the
examples in this course, the parameter is always
PS, which is an acronym for Physical Sequential. DSORG PS

The MACRF parameter specifies the format of


macros used to access the records in the data
GM INPUT
set. You specify GM (Move mode of the GET
MACRF
macro) for input, and PM (Move mode of the
PM OUTPUT
PUT Macro) for output.

The RECFM parameter specifies the record FB INPUT


format of the data set being processed. You RECFM
specify FB (fixed length blocked records) for input, FBA OUTPUT
and FBA (fixed length blocked records with
ANSI printer control characters) for output.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

18
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities Topic: Defining Data Sets

Kinds of Parameters (cont’d)

INPUT DATA SET INPUT 80 Bytes from Data

INFILE DCB DDNAME=IN,RECFM=FB,LRECL=80,DSORG=PS,MACRF=GM, X


EODAD=ENDDATA

SPECIFY WITH INPUT DATA SETS ONLY

OUTFILE DCB DDNAME=OUT,RECFM=FBA, LRECL=133,BLKSIZE=6650, X


DSORG=PS,MACRF=PM

OUTPUT DATA SET OUTPUT 133 Characters to Printer

The LRECL parameter specifies the length of logical records in the data set. You specify 80 for input and 133
for output.

The BLKSIZE parameter specifies the size of physical records, or blocks, in the data set. Omit this parameter
for input data sets and use 6650 for output.

The EODAD parameter (End of Data Address) specifies a label in your program where control is to be
transferred when a GET is attempted and no more data exists. This parameter is only specified for input data
sets.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities Topic: Defining Data Sets

Defining Data Set Elements

Before input and output operations on data sets


can be performed, they must be opened, or made
available for processing. You use the OPEN
macro instruction for this purpose.
OPEN (INFILE,(INPUT)) , (OUTFILE,(OUTPUT))
What is the function of the OPEN macro?

The OPEN macro generates executable


Macro Input DCB Output DCB
instructions that invoke operating system routines
to check for the presence of a data set, confirm
that it matches your specifications, and prepare it
for processing. For output data sets, OPEN will Label of the DCB Indicates the purpose
statement associated for which the data set
create a new data set if the JCL or ALLOCATE is being opened
with the data set
statement so specifies.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

19
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities Topic: Defining Data Sets

Defining Data Sets - Summary

Once the DCB has been opened, code data


Close Macro:
transfer macros, GET for input and PUT for
output.

These statements are discussed in more


CLOSE (INFILE)
detail in the next section.

Having finished the I/O processing, close the CLOSE (OUTFILE)


DCBs, prior to ending the program.The CLOSE
macro is used for file housekeeping.

The CLOSE macro to close the data sets is


coded as follows:

CLOSE (INFILE)
CLOSE (OUTFILE)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Basic MVS I/O Facilities

Topics:
 Defining Data Sets

 Data Transfer Macros

© Copyright IBM Corp., 2000, 2004. All rights reserved.

20
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities Topic: Data Transfer Macros

Move Mode

What is Move Mode?

The method by which data is transferred from or


to an I/O device is called Move Mode.
OPEN (INFILE,(INPUT),OUTFILE,(OUTPUT))
Although data is transferred between the I/O
LOOP EQU *
device and buffer is main storage, one block at a
time, Move Mode I/O moves single logical record GET INFILE,INAREA
to/from a work area. When using this mode, you MVC OUTREC,INAREA
must define a work area for each data set. PUT OUTFILE,OUTAREA

B LOOP
The Move Mode data transfer macro for input,
moves the record from the data set to a buffer in ENDDATA EQU *
main storage and from the buffer to the work area
named INAREA. CLOSE (INFILE,,OUTFILE)

The Move Mode data transfer macro for output,


moves the record from the work area named
OUTAREA to the buffer in the main storage and
from the buffer to the I/O device.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities Topic: Data Transfer Macros

Move Mode (cont’d)

The work area must be the same length as the


logical record.
80-bytes

The logical records in the input data set are 80 GET


bytes in length, so the input work area is defined INAREA DS CL80
as shown.
PUT
DATA CL80
The output records are 133 characters in length, MVC
so the output work area is defined as shown.

These areas could be subdivided by using the OUTAREA DS CL133


zero duplication factor technique.
CL133

© Copyright IBM Corp., 2000, 2004. All rights reserved.

21
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities Topic: Data Transfer Macros

GET Macro

Once a data set is opened, the GET macro reads


the next logical record in the data set into a work
area. The format of the GET macro in the move
mode is shown on the right. Move Mode:

Using the names used up to this point, the macro


would be coded as shown on the right. GET dcbname , workareaname

When the end of the input data is reached, and


another GET macro is issued, control is
transferred to the address specified as the GET INFILE , INAREA
EODAD in the DCB macro.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities Topic: Data Transfer Macros

PUT Macro

What is the function of the PUT Macro?

The PUT macro is used to write the next logical


record from the work area to the output data set. Move Mode:
The format of the PUT macro in move mode is
shown on the right.
PUT dcbname , workareaname
Using the names used up to this point, coding
would appear as shown on the right.

PUT OUTFILE , OUTAREA

© Copyright IBM Corp., 2000, 2004. All rights reserved.

22
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities Topic: Data Transfer Macros

ANSI Control Character

The record format parameter we have used for


our output data set is FBA and is coded as RECFM=FBA ANSI Control
shown: Character

RECFM=FBA ANSI Printer


Meaning
Control Character
The A specifies that the first byte of each Space Single space
record is an ANSI control character, which is
used to specify spacing on the printer. 0 Double space

- Triple space
The most common ANSI printer control
Suppress space
characters and their meanings are shown on +
(overprint last line)
the right. Skip to top
1
of next page

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Basic MVS I/O Facilities Topic: Data Transfer Macros

Consolidating I/O Information

OPEN (INFILE, (INPUT) , OUTFILE, (OUTPUT) ) prepare DCBs for processing


EQU * INPUT
RECORD
LOOP GET INFILE, NAREA get next input record
MVC OUTREC,INAREA move to output record
GET
PUT OUTFILE,OUTAREA write output record
B LOOP continue with next record
ENDDATA EQU * handle end of data situation DATA
CLOSE (INFILE) close dcbs
RECORD
CLOSE(OUTFILE)
RECORD
OUTPUT
PUT

INAREA DS CL80
OUTAREA DS 0CL133 specify double spacing
OUTASA DC C'0' copy of input goes here
OUTREC DS CL80 make the rest spaces
DC CL52 ' ' RECORD
INFILE DCB DDNAME=IN,RECFM=FB,LRECL=80,DSORG=PS,MACRF=GM, X
EODAD=ENDDATA
OUTFILE DCB DDNAME=OUT,RECFM=FBA,LRECL=133,BLKSIZE=6650, X
DSORG=PS,MACRF=P

To put all of this I/O information together, you code a program segment to read data from an input data
set, and then print it out, double spaced, on the printer.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

23
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Decimal Arithmetic

Topics:

 Decimal Type Conversion

 Decimal Arithmetic Operations

 Editing Decimal Data

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic

Unit Introduction

The components of an Assembler Language program are similar to that of a High Level Language program.
They include:

• Input/output
• Arithmetic operations
• Data manipulation
• Instructions to control the program logic and flow

Arithmetic operations are performed in Assembler Language with either fixed-point binary or decimal
instructions. This unit presents packed decimal instructions, which only operate on whole
numbers.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

24
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Decimal Arithmetic

Topics:

 Decimal Type Conversion

 Decimal Arithmetic Operations

 Editing Decimal Data

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Type Conversion

Converting Zoned Decimal Numbers to Packed Decimal Numbers

How do you convert zoned decimal numbers


to packed decimal format?

PACK NUM, DATA


You often need to convert zoned decimal
numbers to packed decimal format. The numbers
1st Operand 2nd Operand
are read in zoned form, but in order to be used in NUM DATA
decimal arithmetic they must be packed.
00 00 12 3C F1 F2 C3

The instruction that performs this conversion is Target Source


called Pack (PACK) and is shown on the right.
PACK

The second operand is converted from zoned to Conversion

packed format, and the result stored in the first Result


00 00 12 3C
operand.
Left padding
Supplied by PACK

CC setting – Condition Code is Unchanged

© Copyright IBM Corp., 2000, 2004. All rights reserved.

25
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Type Conversion

Converting Zoned Decimal Numbers to Packed Decimal Numbers

Pack PACK
Example :
• The data string located at the address specified by
operand-2 is changed from zoned-decimal to packed PACK NUM,DATA
and the result is put into the address specified by
operand-1 .
• Operand-2 remains unchanged. The operands may be
different lengths with a maximum length of 16 bytes
(31 digits) for each operand
• If the length of the operand-2 is greater, left truncation
occurs. If it is less, left padding with zeroes occurs

F2 L1 L2 BDDD BDDD

Format :

PACK D1(L1,B1),D2(L2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Type Conversion

Converting from Packed Decimal to Zoned Decimal Numbers

How do you convert from packed decimal to


zoned decimal numbers?

UNPK OUTFLD, CALCFLD


The reverse conversion, from packed decimal to
zoned decimal is performed by the Unpack
1st Operand 2nd Operand
(UNPK) instruction.

The second operand is converted from packed to


OUTFLD CALCFLD
zoned format and the result stored in the first
operand. 00 00 00 00 00 01 23 4C

Source
Target

UNPK

Conversion
Result

F0 F1 F2 F3 C4

© Copyright IBM Corp., 2000, 2004. All rights reserved.

26
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Type Conversion

Converting from Packed Decimal to Zoned Decimal Numbers

Unpack UNPK
Example :
• The data string located at the address specified by
operand-2 is changed from packed to zoned-decimal UNPK DATA,NUM
and the result is put into the address specified by
operand-1 .
• Operand-2 remains unchanged. The operands may be
different lengths with a maximum length of 16 bytes
(31 digits) for each operand
• If the length of the operand-2 is greater, left truncation
occurs. If it is less, left padding with zeroes occurs

F3 L1 L2 BDDD BDDD

Format :

UNPK D1(L1,B1),D2(L2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Type Conversion

Converting from Packed Decimal to Zoned Decimal Numbers (cont’d)

Notice that when you do arithmetic on packed


numbers, the signs generated are C for + and D
for -. To unpack a number with one of these signs, OR Immediate:
and interpret the contents as characters, the right
most character is not a digit. That is because Prints as 12C
digits have 'F' in the zone position, while an F1 FL C3
DATA OR Immediate
unpacked number has 'C' or 'D'.
changes the sign
nibble

0I DATA + 2, X ‘F0’

Prints as 123
DATA C3
F1 F2 F3
F0

F3
Result

© Copyright IBM Corp., 2000, 2004. All rights reserved.

27
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Type Conversion

Converting from Packed Decimal to Zoned Decimal Numbers (cont’d)

The code shown on the right formats the


UNPK PRTFLD, NUM5
contents of a 4 byte packed field called NUM5 for OI PRTFLD + 6, X ‘F0’
.
printing. .
.
NUM5 DS PL4
PRTFLD DS ZL7

PRTFLD
OR immediate
Changes the
Prints
F1 F2 F3 F4 F5 F6 C7 sign nibble
As
123456C
C7
F0
0I PRTFLD + 6, X ‘F0’
F7
PRTFLD
Prints
F1 F2 F3 F4 F5 F6 F7 as
1234567

Result

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Decimal Arithmetic

Topics:

 Decimal Type Conversion

 Decimal Arithmetic Operations

 Editing Decimal Data

© Copyright IBM Corp., 2000, 2004. All rights reserved.

28
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Arithmetic Operations

Zero and Add Instruction

What is the Zero and Add Instruction?

The Zero and Add (ZAP) instruction is used to


ZAP RESULT,DATA1
move packed data from one main storage location
to another.
1st Operand 2nd Operand
DATA RESULT
ZAP is used primarily to initialize packed decimal
fields and to move packed data from one location 12 34 5F BA C0 F3 49
to another.
Zero 1st Operand

For some operations, like multiplication and ZAP 00 00 00 00


division, it is necessary to place an operand in a
larger field for the operation to work successfully.
ZAP is used in these cases Add 2nd Operand 12 34 5F

. CC setting: 0 – result is zero


1 - result is negative
2 - result is positive
3 - overflow

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Arithmetic Operations

Zero and Add Instruction (cont’d)

Zero and Add packed ZAP


Example :
• Operand-1 is initialized to zero and the data string
located at the storage address specified by operand-2 ZAP D2,D4
is added to the data string located at the address
specified by operand-1.
• Operand-2 remains unchanged. D2 DC XL8'000000000000002C'
• The operands may be different lengths with a D3 DC XL8'000000000000003C'
maximum length of 16 bytes ( 31 digits ) for each
operand.

F8 L1 L2 BDDD BDDD

Format :

ZAP D1(L1,B1),D2(L2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

29
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Arithmetic Operations

Add Decimal

AP
Example :
• The data string located at the address specified by
operand-2 is added to the data string located at the
address specified by operand-1. AP D1,D2
• Operand-2 remains unchanged.
• The operands may be different lengths with a D1 DC XL8'000000000000001C'
maximum length of 16 bytes (31 digits ) D2 DC XL8'000000000000002C'
• The condition code is set as shown below

0 - op1=0, 1 - op1<0, 2 - op1>0, 3 - overflow

D5 L1 L2 BDDD BDDD

Format :

AP D1(L1,B1),D2(L2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Arithmetic Operations

Add Logical

AL
Example :
• The full word operand-2 is added to the full word at
address specified by operand-1, and the sum is
placed in the operand-1 L R4,FW00XX02
• The operands and the sum are treated as 32-bit AL R4,FW00XX01
unsigned binary integers AL R4,FW80XX05
• The condition code is set as shown below
R4 EQU 4
0 – zero nocarry , 1 – not-zero no carry
2 – zero carry , 3 – not-zero carry FW00XX01 DC XL4'00000001'
FW00XX02 DC XL4'00000002‘
FW00XX05 DC XL4'00000005'
5E RX BDDD

Format :

AL R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

30
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Arithmetic Operations

Subtract Decimal

SP
Example :
• The data string located at the address specified by
operand-2 is subtracted from the data string located
at the e address specified by operand-1. SP D1(5),D2
• Operand-2 remains unchanged.
• The operands may be different lengths with a D1 DC XL8'000000000000001C'
maximum length of 16 bytes (31 digits ) D2 DC XL8'000000000000002C'
• The condition code is set as shown below

0 - op1=0, 1 - op1<0, 2 - op1>0, 3 - overflow

FB L1 L2 BDDD BDDD

Format :

SP D1(L1,B1),D2(L2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Arithmetic Operations

Subtract Logical

SL
Example :
• The full word operand-2 is subtracted from the full
word at address specified by operand-1, and the
result is placed in the operand-1 L R4,FW00XX02
• The operands and the result are treated as 32-bit SL R4,FW00XX01
unsigned binary integers SL R4,FW80XX05
• The condition code is set as shown below
R4 EQU 4
0 – zero noborrow , 1 – not-zero borrow
2 – zero borrow , 3 – not-zero borrow FW00XX01 DC XL4'00000001'
FW00XX02 DC XL4'00000002‘
FW00XX05 DC XL4'00000005'
5F RX BDDD

Format :

SL R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

31
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Arithmetic Operations

Multiply Decimal

What are the functions of the multiplier and


MP MPCND, MPLR
multiplicand? .
.
.
Decimal multiplication takes into account the fact MPCND DS PL4
that the length of the product can be as long as MPLR DS PL2
the combined lengths of multiplier and
Specification Exception:
multiplicand. 2 bytes longer
Specification
The length of the multiplier may not exceed 8 Exception
MPCND 00 00 12 3C Does not occur
bytes, and must be less than the length of the
multiplicand, which also holds the product. If MPLR 99 9C
these conditions are not satisfied, a specification 2 bytes of high– order
exception occurs. Zeros not present –
Data Exception: Data Exception

Also, when the instruction is executed the MPCND 00 12 34 5C


multiplicand must have as many bytes of high
order zeros, as there are bytes in multiplier. If MPLR 99 9C
this condition is not met, a data exception occurs.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Arithmetic Operations

Multiply Decimal

MP
Example :
• The data string located at the address specified by
operand-2 is multiplied with the data string located at
the e address specified by operand-1 and the product MP D1,D2
put in operand-1
• Operand-2 remains unchanged. D1 DC XL8'000000000000001C'
• The operands may be different lengths with a D2 DC XL3’00002C'
maximum length of 16 bytes (31 digits )
• The condition code remains unchanged

FC L1 L2 BDDD BDDD

Format :

MP D1(L1,B1),D2(L2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

32
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Arithmetic Operations

Field Lengths

How do you avoid problems with field


lengths?
ZAP PRODUCT,NUM1
A standard technique to avoid problems with field MP PRODUCT,NUM2
.
lengths using Multiply Decimal is to use a field
.
which is the sum of the lengths of multiplier and .
the multiplicand for the product. If NUM1 was a 5 NUM1 DS PL5
NUM2 DS PL3
byte field, and NUM2 a 3 byte field, you would PRODUCT DS PL8
define a field 8 bytes long to hold the product

Length of NUM1 Length of NUM2 Length of Product

5 Byte + 3 Byte = 8 Byte

To ensure that you have enough Low – Order


Zeros, the Product field must be > or =to
the Sum of the lengths of Multiplicand and
Multiplier.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Arithmetic Operations

Divide Decimal

What is the function of the Divide Decimal? Divide Decimal (DP):


SS Format (2 length) – Storage to Storage
• The DP instruction divides a packed decimal
DP NUM3,=P’4’
dividend (first operand) by a packed decimal
divisor (second operand). The quotient and
the remainder replace the dividend in the first 1st Operand
operand. 2nd Operand

NUM3 is divided by 4, with the quotient replacing


DP
all but the rightmost byte of NUM3, and the
Literal 4 NUM3
remainder in the rightmost byte.
%
VALUE VALUE
Divisor Dividend
FD L1 L2 BDDD BDDD Quotient and
QUO REM Remainder
replaces value
Format :
In NUM3
CC Setting – Condition Code is Unchanged
DP D1(L1,B1),D2(L2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

33
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Arithmetic Operations

Divide Decimal (cont’d)

Decimal division is integer division and so it Decimal Division:


produces both an integer quotient and an integer
remainder. After the division, the quotient and L1
remainder share the first operand field that initially Before DIV
DIVIDEND S1
contained the dividend.

Both quotient and remainder are packed decimal


fields, with the quotient preceding the remainder. DIVISOR S2
The length of the quotient field is equal to the L2
length of the dividend field minus the length of
the divisor field. The length of the remainder field QUO REMAINDER
equals the length of the divisor field. L2
L1 –L2

A specification exception results if the dividend L1 = Length of Dividend in Bytes


field is not longer than the divisor field, or if the L2 = Length of Divisor in Bytes
Rules:
divisor length is greater than 8 bytes. If the divisor
L1 – Maximum of 16 Bytes
is zero, or the quotient is too large to fit into the L2 – Maximum of 8 Bytes
quotient field, a decimal divide exception occurs. L1 must be greater than L2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Decimal Arithmetic Operations

Divide Decimal (cont’d)

A useful technique in performing decimal division


ZAP QUOREM,NUM4
is to move the dividend to a field as large as the DP QUOREM,NUM5
sum of the dividend and the divisor field lengths, .
.
prior to the division. You can use the technique of
.
zero duplication factor to define this single field in NUM4 DS PL6
two ways: one for the dividend (before the NUM5 DS PL2
QUOREM DS 0PL8
division), and one for the quotient and remainder QUOTIENT DS PL6
(after the division). REMAINDER DS PL2

To divide NUM4 (6 bytes long) by NUM5 (2 bytes Assume NUM4 = 1000


NUM5 = 7
long), the code shown on the right could be used.
Before Division After ZAP
After the DP instruction, the quotient is in
QUOREM 000000000001000C
QUOTIENT and the remainder in REMAINDER.
After Division PL6 PL2

QUOREM 0 0 0 0 0 0 0 0 1 4 2 C 006C

QUO REM

© Copyright IBM Corp., 2000, 2004. All rights reserved.

34
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Decimal Arithmetic

Topics:

 Decimal Type Conversion

 Decimal Arithmetic Operations

 Editing Decimal Data

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Editing Decimal Data

For many programs, it is important to present


numeric results in a format that is easy to read Editing:
and interpret. If you are dealing with large
numbers, you can use commas to break the
number up for readability . Numbers such as ED MASK, PACKED

123,456,789 are easier to read and interpret than


123456789.
Indicates Number according
formatting to MASK
The Edit instruction is provided for the
purpose of formatting numeric output.
Instruction Structure

ED L B DDD B DDD
DE L1 L2 BDDD BDDD
Length 1st Operand 2nd Operand
Format :

ED D1(L,B1),D2(B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

35
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Edit Instruction

What is an Edit Instruction?

Edit is a very powerful instruction, in that it allows


you greater control and flexibility in presenting Value Formatted Value
numeric output. However, the power comes at a
price. Edit is a very complex instruction. In order 1234567 12,345.67
to use the full power of Edit, you must take the
0123456 1,234.56
time to understand the details of how it works.
0012345 123.45

0001234 12.34

0000123 1.23

0000012 0.12

0000001 0.01

-1234567 12,345.67-

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Edit

The values of all consist of seven digits and a


sign. The positive sign is not shown. Digit prior to period
is also chosen

The formatted values contain digits, and


punctuations such as commas, periods and minus
Value Formatted Value
signs, to make reading the numbers easier. Note
that the values are all 7 digits and a sign.
1234567 12,345.67

However, not all of the digits are significant in all 0123456 1,234.56

of the examples. Zeroes preceding the first non- 0000001 0.01


zero digit in a value are not significant. -1234567 12,345.67-

Punctuation
Sign (-) Significant
Digit
Not a
Significant
Digit

© Copyright IBM Corp., 2000, 2004. All rights reserved.

36
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Edit (cont’d)

If you were printing an amount on a check, you


might want to format the amounts in dollars and
cents to make it difficult to manually change the
check.
One way to do this is to print asterisks rather than Value Formatted Value
spaces prior to the first significant digit.
1234567 12,345.67
The table of formatted values would then look like 0123456 *1,234.56
the table shown on the right.
0012345 ***123.45

0001234 ****12.34

0000123 *****1.23

0000012 *****0.12

0000001 *****0.01

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Significance Indicator

What is the Significance Indicator?

In order to handle this type of editing, Edit must Forcing Significance:


be aware of whether the digit it is handling is
significant or not. Edit maintains a Significance
Indicator to keep track of whether a significant
digit has yet been detected.

To be able to always print a digit before the SIGNIFICANCE Turned on by


decimal point, the programmer must be able to INDICATOR detecting a
specify that Edit will behave as if it has found a non - zero
significant digit once it reaches a certain position
in the output field. This is known as forcing
significance Turned on by
Character in MASK

© Copyright IBM Corp., 2000, 2004. All rights reserved.

37
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Fill Character

What is the fill character?


Significance Indicator:
The programmer must also be able to tell Edit
what to place in the output field if the Significance
Indicator has not been turned on. The character MASK
used to fill the output field, until the Significance
Indicator is turned on, is known as the fill * DS DS
character.

Specifies position for digit (SI ON)


Or Fill Character (SI OFF)

First character replaces


leading zeros or punctuation
before significance Indicator
is Turned On

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Edit Mask

What is an edit mask?

The first operand of Edit is an edit mask that Edit


replaces with the edited number. It is through the
edit mask that the programmer specifies how the
Edited number is to look. The first character of the
edit mask is the fill character. The remaining Character Meaning
characters of the edit mask are a combination of
the characters shown in the table on the right. X ’20’ Digit Select

X ’21’ Significance Start

X ’22’ Field Separator

Anything else Message Character

© Copyright IBM Corp., 2000, 2004. All rights reserved.

38
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Edit Mask (cont’d)

What are the components of an edit mask?

Message Characters
A digit select character specifies a position where
a source digit is to be placed if the Significance MASK
Indicator is set, or the fill character is placed
otherwise. A significance start character has the Fill ds ds , ds ss ds . ds ds -
same effect as a digit select, but also forces the
Significance Indicator on, after the digit or fill
character is placed. The field separator character
is only used if the Edit operation is editing multiple
numbers. It causes the significance indicator to be
turned off.
12,345.67-

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Edit Mask (cont’d)

The Significance Indicator is initially turned off. It


is turned on when a non-zero digit is encountered
Non – Zero Significance
in the second operand, and after processing a
Digit Start Character
byte in the mask containing a significance start
character.

Turned ON
It is turned off when a positive sign is encountered
in the second operand, and when a field separator
character is found in the mask. Significance
Indicator
(Initially OFF)

Turned OFF

+ ve Sign Field
Separator

© Copyright IBM Corp., 2000, 2004. All rights reserved.

39
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Formatting Packed Decimal Data with Edit Instruction

How do you format packed decimal data with Edit (ED)


Edit instruction? SS Format (1 length) – Storage to Storage

The Edit (ED) instruction is used to format packed ED MASK,PD1

decimal data. The first operand is an edit mask. 1st Operand 2nd Operand
The second operand is a field containing one or
more packed decimal numbers. PD1

The second operand is edited according to the


values in the mask, and the edited number EDIT PD1
PD1 MASK
replaces the mask in the first operand field. The According to MASK
single length specifies the length of the mask. The
two operands are processed left to right.
Replace
RESULT MASK
For example: ED MASK,PD1
CC setting – 0 – the last field edited
The value in PD1 is edited, according to the is zero or zero length.
specifications in MASK, and the edited number 1 – the last field edited is
less than zero
replaces MASK. 2 – the last field edited is
greater than zero

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Formatting Packed Decimal Data with Edit Instruction (cont’d)

The mask field is replaced by the edited Edit (ED)


number. It is necessary to move the mask to SS Format (1 length) – Storage to Storage
the location where the editing is to be done
ED MASK,PD1
before each repeated ED instruction to avoid
destroying the mask. 1st Operand 2nd Operand

PD1

PD1 MASK EDIT PD1


According to MASK

Replace
RESULT MASK

CC setting – 0 – the last field edited


is zero or zero length.
1 – the last field edited is
less than zero
2 – the last field edited is
greater than zero

© Copyright IBM Corp., 2000, 2004. All rights reserved.

40
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Formatting Packed Decimal Data with Edit Instruction (cont’d)

To devise the mask necessary to edit the 7


digit number specified in the earlier example,
you must remember that you will want the
edited number to represent a dollar and cents Value Formatted Value
amount.
1234567 12,345.67
If you look at this table, you can see that the
0123456 1,234.56
fill character is blank. That blank will be the first
0012345 123.45
character of the edit mask. If at least one digit
is desired to be printed prior to the decimal point, 0001234 12.34
you will need to specify a significance start 0000123 1.23
character two positions before the decimal
point, because the significance indicator is 0000012 0.12

turned on after the byte with the significance 0000001 0.01


start character is processed. From the last line
-1234567 12,345.67-
of the table, it seems that the last character of
the mask must be a minus sign.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Formatting Packed Decimal Data with Edit Instruction (cont’d)

To edit a positive number, the positive sign turns


the Significance Indicator off, so the fill character Contents Purpose
of blank would replace the minus sign message
character. Using a minus sign does not turn the Blank Fill character
Significance Indicator off, so the message X’20’ Digit select
character is printed.
X’20’ Digit select

The mask will need to contain the following Comma Message character
characters in the order shown in the table in the X’20’ Digit select
right.
X’21’ Significance start

X’20’ Digit select

Period Message character

X’20’ Digit select

X’20’ Digit select

Minus sign Message character

© Copyright IBM Corp., 2000, 2004. All rights reserved.

41
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Digit Selects and Significance Starts

MVC PRTAMT , MASK mc


ED PRTAMT , PK1 oc
.
.
.
PK1 DS PL4
PRTAMT DS CL11
MASK DC X’4020206B2021204B202060

There are seven digits being edited in this example, so the total number of digit selects and significance start
character is seven. When you add the fill character and message characters, the total length of the mask is
11 bytes. It is always a good idea to check that your masks have the proper number of digit selects and
significance starts. Having too many may cause a data exception. Having too few will cause truncated values
to be generated.

Assuming that the number to be edited was called PK1 and the field where you wanted to place the edited
number was called PRTAMT, the code to do the editing is as shown above.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Example Code

A complete code segment, to read 3 seven digit numbers from a dataset, add them and then
print the edited sum, is shown below

OPEN (INFILE,(INPUT),OUTFILE,(OUTPUT)) prepare dcbs for processing


GET INFILE,INAREA get next input record
PACK WKAREA,NUM convert number to packed
ZAP SUM,WKAREA move first number to sum
GET INFILE,INAREA get next input record
PACK WKAREA,NUM convert number to packed
AP SUM,WKAREA add second number to sum
GET INFILE,INAREA get next input record
PACK WKAREA,NUM convert number to packed
AP SUM,WKAREA add third number to sum
MVC OUTNUM,MASK move edit mask to output
ED OUTNUM,SUM edit the sum
PUT OUTFILE,OUTAREA write output record
CLOSE (INFILE,,OUTFILE) close dcbs

© Copyright IBM Corp., 2000, 2004. All rights reserved.

42
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Editing Decimal Data

Example Code (cont’d)

INAREA DS 0CL80
DS CL73
NUM DS CL7
WKAREA DS PL4
SUM DS PL5
MASK DC X’402020206B2020206B202120’
OUTAREA DS 0CL133
OUTASA DC C’0’ specify double spacing
DC CL10’The Sum is’
OUTNUM DS CL12
DC CL110’ ‘ make the rest spaces
INFILE DCB DDNAME=IN,RECFM=FB,LRECL=80,DSORG=PS,MACRF=GM, X
EODAD=ENDDATA
OUTFILE DCB DDNAME=OUT,RECFM=FBA,LRECL=133,BLKSIZE=6650, X
DSORG=PS,MACRF=PM

The EBCDIC codes for blank, comma, period and minus are 40, 6B, 4B and 60 respectively.
These are codes that you should memorize if you are going to be doing a lot of editing.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Comparing and Branching

Topics:
 Decision Making in Assembler

 Compare Instructions

 Branching

 Building Structured Constructs

© Copyright IBM Corp., 2000, 2004. All rights reserved.

43
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching

Unit Introduction

In order to write programs that take full advantage of the computer's capabilities, you must be able to do more
than execute a series of instructions sequentially. Interesting programs, programs that model significant real
world processes, must also make decisions and repeat sequences of instruction multiple times.

In this unit, we will cover conditional execution and looping which are basic programming components. You
will also learn how to implement these structures in Assembler Language.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Comparing and Branching

Topics:
 Decision Making in Assembler

 Compare Instructions

 Branching

 Building Structured Constructs

© Copyright IBM Corp., 2000, 2004. All rights reserved.

44
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Decision Making in Assembler

Implementing Conditional Execution

How is conditional execution implemented? EC Mode PSW

2 bit Field
In Assembler Language, conditional execution is
broken into parts. One instruction tests a
condition. A following instruction, a conditional CC Program Mask
branch, alters the execution path, depending on
the result of the test. The condition code, a two-bit 18 20
field in the Program Status Word (PSW) is where
the result of the test is held, awaiting subsequent
conditional branch instruction. CC Possible Values:

00 01 10 11 Binary

0 1 2 3 Decimal

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Decision Making in Assembler

Implementing Conditional Execution (cont’d)

Some instructions set the condition code and


some do not. If and how an instruction sets the Condition Code: - Some Examples
condition code is an important part of the
instruction description. Once the condition code is
set by an instruction, that value remains in the
condition code until a subsequent instruction AP
changes it. SP CC – is set
MVCL

MP Condition Code is
DP unchanged.
MVC

© Copyright IBM Corp., 2000, 2004. All rights reserved.

45
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Decision Making in Assembler

Compare Instructions

A condition code setting of 3 does not occur after


compare instructions. For brevity, these settings
CC Setting – Comparison:
will be referred to as CC setting - Comparison in
the instruction descriptions that follow.

Condition Code Setting:

0 - Equal

1 – First Operand is Low

2 – First Operand is High

3 – Not Used

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Comparing and Branching

Topics:
 Decision Making in Assembler

 Compare Instructions

 Branching

 Building Structured Constructs

© Copyright IBM Corp., 2000, 2004. All rights reserved.

46
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Compare Instructions

Numeric Comparison

What is numeric comparison?


Comparisons:
When two operands are compared, it is important
that you be clear about their format. If the
operands represent signed numeric quantities,
then a numeric comparison is needed. On the
1st Operand 2nd Operand
other hand, if the operands do not represent
signed numeric data, a logical comparison is
performed, treating the operands simply as strings = Arithmetic
NUMERIC NUMERIC
of bits.

TEXT TEXT = Logical

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Compare Instructions

Kinds of Comparison

If you compared R3 containing X'FFFFFFF6' and


Comparisons:
FDW1 containing X'0000000A', the condition code
that will be set will depend on the type of
comparison. 1st Operand 2nd Operand
R3 FWD1 ARITHMETIC
In an arithmetic comparison, the register contents
would be interpreted as -10. The storage operand
FFFFFFF6 0000000A
is interpreted as +10, so the first operand (the
register) would be lower, and the condition code is -10 < +10 = CC set to 1
set to 1.

R3 FWD1 LOGICAL
In a logical comparison, FFFFFFF6 is a higher
hex value than 0000000A, so the first operand
FFFFFFF6 0000000A
would be higher and the condition code be set to
2. Higher Hex > Lower Hex
= CC set to 2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

47
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Compare Instructions

Comparing data

Compare C Compare Registers CR

• Operand-1 is compared with the data • Operand-1 is compared with the operand-2
string located at the storage address
specifed by operand-2

59 RX BDDD 19 RR

Format : Format :

C R1,D2(X2,B2) CR R1,R2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Compare Instructions

Comparing data (cont’d)

Compare Halfword CH Compare Decimal (packed) CP

• Operand-1 is compared with the data • The data string located at the address
string located at the storage address specified by operand-2 is compared to
specifed by operand-2 the data string located at the address
specified by operand-1
• The operands may be different lengths
with a maximum length of 16 bytes (31
digits) for each operand.
• This is an arithmetic comparison

49 RX BDDD F9 L1 L2 BDDD BDDD

Format : Format :

CH R1,D2(X2,B2) CP D1(L1,B1),D2(L2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

48
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Compare Instructions

Comparing data (cont’d)

Compare Logical CL
Example :
• The compare logical instruction does a logical Left Right CC
compare instead of a mathematical or arithmetic
comparison 00000222 00000222 0
• The compare is done left to right and stops as soon 00000000 00000222 1
as a non-equal value is found. 00000999 00000222 2

• CLR can use two registers 98765432 12345678 1

For normal compare ( C, CR etc) operation


last example will set CC to 2 as left value
is negative
55 RX BDDD

Format :

CL R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Compare Instructions

Comparing data (cont’d)

Compare Logical Immediate CLI


Example :
• Operand-2 (the immediate data that is the second
byte of the instruction itself) is compared to the one- CLI 3(R8),C'#‘
byte at the storage address specifed by operand-1
R8 EQU 8

* Compare 4th byte of R8 with ‘#’

95 I BDDD

Format :

CLI D1(B1),I2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

49
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Compare Instructions

Comparing data (cont’d)

Compare Logical Characters CLC


Example :
• The data string located at the storage address
specified by operand-2 is compared to the data CLC DATA(10),MOREDATA
string located at the storage address specified by
operand-1.
• The number of bytes compared is determined by the * MOREDATA is compared to DATA for a
length specified
* length of 10 bytes
• The length of each operand is the same

D5 L BDDD BDDD

Format :

CLC D1(L,B1),D2(B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Compare Instructions

Comparing data (cont’d)

Compare Logical Characters Long CLCL


Example :
• The data string identified by operand-1 is compared the R6 EQU 6
storage location identified by operand-2 R7 EQU 7
• Both operands are even-odd register-pairs. The first R8 EQU 8
register needs to be loaded with the addresses of the data R9 EQU 9
strings. The second register needs to be loaded with the RECV DC C’SOMEDATA’
length of each operand.
SEND DC C’A LONGER FIELD’
• The data strings may be different length.
• The high-order byte of the second register of operand-2 is LA R6,RECV
treated as the padding character
LH R7,=H’8’
• As each character is moved the length registers are
LA R8,SEND
decremented
LH R9,=H‘14’
ICM R9,8,=C’ ‘ padding char
0F RR
CLCL R6,R8

Format :

CLCL R1,R2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

50
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Compare Instructions

Compare Logical Long – An Example

LM R2,R5,INITCOMP set up regs for clcl


CLCL R2, R4 compare fields
LH R6, =H ‘100’ total field length
SR R6,R5 less length minus equal bytes
STH R6,EQBYTES save count of equal bytes
.
.
.
INITCOMP DC A(DATA1)
DC F’100’
DC A(DATA2)
DC F ‘100’
EQBYTES DS H

To find out how many 100 byte fields called DATA1 and DATA2 are initially equal, you could use the code
shown above.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Comparing and Branching

Topics:
 Decision Making in Assembler

 Compare Instructions

 Branching

 Building Structured Constructs

© Copyright IBM Corp., 2000, 2004. All rights reserved.

51
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Branching

Branch on Condition Instruction

What is the Branch on Condition Instruction?


Branch on Condition
The way you implement conditional execution in
Assembler Language is with the Branch on
Condition (BC) Instruction. This instruction tests
the current setting of the condition code, using a BC
mask field in the instruction. MASK Corresponds YES
To Condition
Code
If the mask field corresponds to the setting of the
condition code, then the next instruction to be
executed is the one at the address specified as
NO
the second operand of Branch on Condition. If 2nd Operand
there is no correspondence, then execution of BC
proceeds sequentially with the instruction
Next Sequential
following Branch on Condition. Instruction

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Branching

The Mask Field

The mask field is 4 bits in length, allowing 16


possible values. The condition code is 2 bits long,
Mask Field:
allowing 4 possible values. The 4 bits of the mask
correspond to the 4 possible condition values.
Condition Code Setting 0 1 2 3
If the mask value is 8, the Branch on Condition Corresponding Mask Bit 0 1 2 3
instruction tests for a CC setting of zero. If the CC
= 0, the branch is taken, otherwise normal Mask Value 8 4 2 1
sequential execution occurs.

BIT Value 8 4 2 1
MASK BIT
X X X X (4 BITS)

BIT Position 0 1 2 3

© Copyright IBM Corp., 2000, 2004. All rights reserved.

52
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Branching

The Mask Field (cont’d)

Mask values can be any value in the range of 0 to


15. A mask value of 7 (4+2+1) tests if the CC is
1 or 2 or 3. If so, the branch is taken. Otherwise it Mask Values:
is not.

Any combination of condition codes can be tested MASK = 710 DECIMAL


in a single Branch on Condition instruction. A
mask value of 15 tests if the CC is 0 or 1 or 2 or 3. = 01112 BINARY
Since the CC must be one of these values, this
constitutes an unconditional branch.

CC = 1 CC = 2 CC = 3

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Branching

BC – An Example

When using the Branch on Condition (BC)


instruction the first operand of BC specifies a
mask, not a GPR. If the bit in the mask
corresponding to the current value of the condition BC 8, EQRTN
code is on, then the address specified as the
second operand replaces the next instruction 1st Operand 2nd Operand
address in the PSW. If not, the normal sequential 8 EQRTN
instruction sequencing occurs. ADDRESS TO
MASK BRANCH

BC
47 MX BDDD
If CC = 0
Branch to EQRTN
Format :
If CC is (1 or 2 or 3)
Continue to Next Instruction
BC M1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

53
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Branching

BC – An Example (cont’d)

EQRTN would be a label identifying the location in


your program that you wanted to branch to. Often
Assembler Language programmers put branch
labels on an instruction that does nothing.

EQRTN EQU *
EQRTN LR R7,R2
This instruction equates the label EQRTN to the
current value of the location counter. This is the LR R2,R3
value of EQRTN regardless, so the instruction
really does nothing. The reason you set up labels
this way is to ensure ease in maintenance.
Add New Instruction
Suppose you put the label on a machine
instruction as follows:
2 Instructions Change

EQRTN LR R2,R3

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Branching

BC – An Example (cont’d)

Later on, if you want to add an instruction at the


beginning of EQRTN but before the LR, you
would need to add a new instruction with the label
EQRTN, and then delete the label from the LR
instruction. If the label is on a do nothing EQRTN EQU *
instruction, your original code looks like this:

LR R7,R2
EQRTN EQU *
LR R2,R3
LR R2,R3
To add a new instruction, you simply insert it in
between the EQU and the LR instruction, as
shown on the right.
New Instruction

Just addition – No change to existing

© Copyright IBM Corp., 2000, 2004. All rights reserved.

54
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Branching

Branch on Condition Register Instruction

• With BCR instruction, the first operand


specifies a Mask If the bit in the mask
corresponding to the current value of the
condition code is on, then the address in the BCR 15, R11
second operand register replaces the next
instruction address in the PSW 1st Operand 2nd Operand
• If not, normal instruction sequencing occurs
• However, if the second operand specifies 0,
no branch is taken regardless of the mask UNCONDITIONAL
and condition code values. BRANCH

BCR M,R0
07 M R
* No branching occurs
Format :

BCR M1,R2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Branching

Extended Mnemonic Branch Instructions

What are Extended Mnemonic branch


instructions?
CR R3,R4
The two operands of the Branch on Condition
instruction specify the conditions under which a
branch is taken, and the branch address. ?

Branch if not high?


It can be hard to remember the actual condition
code for the various instructions and to calculate What is CC?
mask values, particularly when testing
combination of condition codes. What is Mask?

Therefore, the Assembler Program simplifies the


process by introducing extended mnemonic
Extended Mnemonics Branch
branch instructions.
Instructions Simplifies the Process

© Copyright IBM Corp., 2000, 2004. All rights reserved.

55
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Branching

Extended Mnemonic Branch Instructions (cont’d)

The extended mnemonic branch instructions


combine the operation code for branching and the
mask, which specifies the conditions under which
Extended Mnemonic Branch Instructions:
the branch is taken, into a simple extended
mnemonic.
Using BC
For example, if you wanted to branch to
NOROUND if the value in R3 was less than the
BC 4, NOROUND
value in R4, you would specify:
CR R3,R4
BC 4,NOROUND
Using Extended Mnemonic
With extended mnemonics, the code would be
as follows:
CR R3,R4 BL NOROUND
BL NOROUND

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Branching

Extended Mnemonics (Logical)

Set of extended mnemonics which can be used after comparison instructions are shown below

Extended Mnemonics Branch Instructions: (Logical)

Mask Value RX Mnemonic RR Mnemonic Interpretation


2 BH BHR Branch if Op1 is High
4 BL BLR Branch if Op1 is Low
7 BNE BNER Branch if Not Equal
8 BE BER Branch if Equal
11 BNL BNLR Branch if Op1 not Low
13 BNH BNHR Branch if Op1 not High
15 B BR Unconditional Branch

© Copyright IBM Corp., 2000, 2004. All rights reserved.

56
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Branching

Extended Mnemonics (Arithmetic)

Extended Mnemonics Branch Instructions: (Arithmetic)

Mask Value RX Mnemonic RR Mnemonic Interpretation


1 BO BOR Branch on Overflow
2 BP BPR Branch on Plus
4 BM BMR Branch on Minus
7 BNZ BNZR Branch on Not zero
8 BZ BZR Branch on zero
11 BNM BNMR Branch on Not minus
13 BNP BNPR Branch on Not Plus
14 BNO BNOR Branch on No Overflow

After arithmetic instructions, you use the extended mnemonics shown above.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Comparing and Branching

Topics:
 Decision Making in Assembler

 Compare Instructions

 Branching

 Building Structured Constructs

© Copyright IBM Corp., 2000, 2004. All rights reserved.

57
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Building Structured Constructs

Conditional Structures

What are conditional structures?


Conditional Constructs:

In High Level Languages, structures like IF-THEN


and IF-THEN-ELSE are used to build conditional
structures. When you are programming in
Assembler Language, you should design your
algorithms using the standard structured
constructs, and then implement those structures
using the Branch on Condition instruction.

The use of BC to create basic control structures,


with meaningful commenting, produces programs
that are much easier to understand and debug.

IF - THEN IF – THEN - ELSE

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Building Structured Constructs

IF-THEN Structure in C

A basic IF-THEN structure in C looks like this:


IF – THEN Structure:
if (a == b) {
IF
a = a + b;
}
NO
The corresponding code in Assembler Language a=b
would look like this: ?

CP A,B YES
BNE NOADD
AP A,B
NOADD EQU * a=a+b

NO ADD

© Copyright IBM Corp., 2000, 2004. All rights reserved.

58
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Building Structured Constructs

IF-THEN-ELSE Structure

A basic IF-THEN-ELSE structure in C looks like


this: IF – THEN – ELSE Structure:

IF
if(a == b) {
a = a + b;
YES
} a=b NO
else { ?
a = a - b;
}
The corresponding Assembler Language code
would be: a=a+b a=a-b
CP A,B
BNE SUBTR
AP A,B
B CONDEND
SUBTR EQU * CONDEND
SP A,B
CONDEND EQU *

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Comparing and Branching Topic: Building Structured Constructs

DO – WHILE Structure

The most common and flexible looping structure


is DO-WHILE. It must be built in Assembler DO – WHILE Structure:
Language using the Branch on Condition
A DO – WHILE loop in C might look like this: LOOPSTART

while (I > 1) {
prod = prod * i; l>1 NO
I = I - 1; ?
}
YES
Assembler Language code:
LOOPSTART EQU *
CP I,=P'1' Prod = prod*I
BNH LOOPEND I = i-1
MP PROD,I
SP I,=P'1'
B LOOPSTART
LOOPEND
LOOPEND EQU *

© Copyright IBM Corp., 2000, 2004. All rights reserved.

59
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Fixed-Point Binary Arithmetic

Topics:
 Decimal to Binary Conversion

 Addition and Subtraction

 Multiplication

 Division

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic

Unit Introduction

Arithmetic operations concerned with S/390 Architecture and the Assembler Language are needed to perform
input/output operations, data manipulation and program control of logic and flow.

S/390 architecture supports three types of arithmetic instructions:


• Decimal
• Fixed-point binary
• Floating-point

This unit introduces instructions associated with fixed-point arithmetic, which manipulates binary data.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

60
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Fixed-Point Binary Arithmetic

Topics:
 Decimal to Binary Conversion

 Addition and Subtraction

 Multiplication

 Division

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Decimal to Binary Conversion

Storing Numeric Data

How is numeric data read and stored?

Numeric data can be read and stored in a


computer in three formats: CONVERT TO CONVERT TO
DECIMAL BINARY BINARY
(CVD) (CVB)
• Binary
• Packed Decimal
EDIT
• Zoned decimal
(ED)
PACKED
This unit will introduce you to the instructions that DECIMAL
work with conversions, which are Convert to
UNPACK
Binary (CVB) and Convert to Decimal (CVD). PACK PRINT
(UNPK)
OUT

ZONED
DECIMAL

© Copyright IBM Corp., 2000, 2004. All rights reserved.

61
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Decimal to Binary Conversion

Convert to Binary Instruction

The second operand, an eight byte packed


decimal number, is converted to fixed-point format
and stored in GPR specified by the first operand.
CVB R3, PK1
For example: CVB R3, PK1
1st Operand 2nd Operand
PK1
The 8 byte packed value in PK1 is converted to a
00 00 00 00 02 45 8C
fixed point fullword and placed in R3.
00 00 09 9A
R3
Conversion of positive number

PK1 00 00 00 00 00 02 5D
4F RX BDDD
FF FF FF E7
Format : R3 Conversion of negative number

Twos complement form of negative number


CVB R1,D2(X2,B2) CC Settings: Condition Code is Unchanged

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Decimal to Binary Conversion

Convert to Decimal Instruction (cont’d)

The first operand, a fixed point full word in a


Convert to Decimal (CVD):
register, is converted to an eight byte packed RX Format - Register to indexed storage
decimal number, at the main storage location
specified by the second operand. CVD R4,NUM

1st Operand 2nd Operand


For example: CVD R4,NUM R4 NUM
FULLWORD DOUBLEWORD
The fullword in R4 is converted to a packed
Sending Receiving
decimal number and stored in main storage in Field
Field
eight bytes beginning at NUM. CVD

4E RX BDDD 00 00 02 14 74 83 64 7C

Format : Converts FULLWORD


to packed Decimal Format R4
011.........11
CVD R1,D2(X2,B2) 0 31
CC Setting: Condition Code is Unchanged

© Copyright IBM Corp., 2000, 2004. All rights reserved.

62
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Decimal to Binary Conversion

Convert to Decimal Instruction (cont’d)

The code to take two 4 byte zoned numbers


PACK DWD,Z1
called Z1 and Z2, then calculate their sum using CVB R2,DWD
bb bb b1 1b
fixed point arithmetic, and then put the sum into PACK DWD,Z2
CVB R3,DWD
an edited field called SUM. The code for this is
Character AR R2,R3
shown at the right. after EDIT CVD R2,DWD
ED SUM,DWD+5
.
.
.
Z1 DS ZL4
Z2 DS ZL4
SUM DC X’4020206B20212060
DWD DS D

00 00 00 00 00 00 00 5C convert first number


to fixed point
F0 F0 F0 C5
R2 00 00 00 05
convert second number
00 00 00 00 00 00 00 6C to fixed point
calculate sum
F0 F0 F0 C6
R3 00 00 00 06 convert sum to edited
form
00 00 00 0B R2 00 00 00 00 00 00 01 1C

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Fixed-Point Binary Arithmetic

Topics:
 Decimal to Binary Conversion

 Addition and Subtraction

 Multiplication

 Division

© Copyright IBM Corp., 2000, 2004. All rights reserved.

63
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Addition and Subtraction

Number Representation

S HALFWORD

S FULLWORD

S DOUBLEWORD

MAGNITUDE
High Order Bit (Sign) 0=(+) Positive (True Binary) 1=(-) Negative (Two's Complement)

The System/390 architecture provides 3 sets of arithmetic instructions:


• Fixed-point arithmetic instructions deals with integers
• Decimal arithmetic instructions deals with integers
• Floating point arithmetic deals with real numbers (numbers with fractional parts)

Fixed-point arithmetic is performed in the General Purpose Registers (GPRs). Operands for fixed-point
arithmetic may be halfwords (16 bits), fullwords (32 bits) or doublewords (64 bits). For all three
operand types, the leftmost, or high-order bit, is the sign, with zero representing positive and 1
representing negative. The remainder of the fixed-point number represents the magnitude. Positive
numbers are represented in true binary, negative numbers in two's complement form.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Addition and Subtraction

Number Representation (cont’d)

S HALFWORD
-215 to + 215 - 1
01 15
FULLWORD
S
-231 to + 231 - 1
01 31
DOUBLEWORD
S -263 to + 263- 1
01 63
MAGNITUDE

A halfword has 15 bits to represent the magnitude, and thus can represent values from -215 to +215-1
(32,768 to 32,767).

A fullword has 31 bits to represent the magnitude, and thus can represent values from -231 to +231-1
(-2,147,483,648 to 2,147,483,647).

A doubleword has 63 bits to represent the magnitude, and thus can represent values from -263 to 263-1
(-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).

© Copyright IBM Corp., 2000, 2004. All rights reserved.

64
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Addition and Subtraction

Add Instruction

Add A
Example :
• The full word (4 bytes) located at the address
specified by operand-2 is added to the register
specified by operand-1. L R1,REG1
• Operand-2 remains unchanged. A R1,REG2
• The condition code is set as shown below ST R1,REG1

0 - op1=0, 1 - op1<0, 2 - op1>0, 3 - overflow R1 EQU 1

REG1 DC XL4'00000001'
REG2 DC XL4’02010405’

5A RX BDDD
* REG1 = REG1 + REG2

Format :

A R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Addition and Subtraction

Add Instruction (cont’d)

Consider the following instruction sequence:

The value in R4 after the load instruction is 1710. L R4, = F’17’


A R4,NUM2
If represented in hex, the normal way to show .
.
register and storage contents, the register would .
contain 00000011. NUM2 DC F’ –6’

After the addition, R4 contains 1110, which would LOAD

appear as 0000000B. R4

00 00 00 11

ADD
+
NUM2 Replace R4 with SUM

FF FF FF FA = 00 00 00 0B

© Copyright IBM Corp., 2000, 2004. All rights reserved.

65
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Addition and Subtraction

Add instruction (Cont’d)

Add Registers AR
Example :
• The register specified by operand-2 ( is added to the
register specified by operand-1. Operand-2 remains
unchanged.
• The condition code is set as shown below AR R3,R4
ST R3,REG1
0 - op1=0, 1 - op1<0, 2 - op1>0, 3 - overflow

R3 EQU 3
R4 EQU 4

1A RR

Format :

AR R1,R2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Addition and Subtraction

Add Halfword instruction

Add Halfword AH
Example :
• The half word (2 bytes) located at the address
specified by operand-2 is added to the register
specified by operand-1 .
• Operand-2 remains unchanged. AH 5,X'6B0'(13,12)
• The condition code is set as shown below

0 - op1=0, 1 - op1<0, 2 - op1>0, 3 - overflow

4A RX BDDD

Format :

AH R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

66
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Addition and Subtraction

Add Halfword Instruction (cont’d)

A common programming situation is incrementing


a counter. Suppose you had a halfword counter
called COUNTER, and you wanted to add one to
it. LH R1, COUNTER
AH R1, =H’1’
All fixed-point arithmetic is done in registers, so STH R1, COUNTER

you first have to load the counter into a register,


then increment it, and then finally store the sum
back into the counter. The instruction sequence to
do this is shown on the right.
R1

00 00 00 05 00 05

COUNTER
+
Replace

00 01 = 00 00 00 06 00 06

AH Literal R1 STH

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Addition and Subtraction

Add Halfword Instruction (cont’d)

A common mistake is to mix up the instruction


type and the data type. Consider the following: AH
R3, DATA1
.
Instruction Type and
.
Data Type are mixed up.
The instruction is AH, but the storage operand .
To correct this, change
DATA1 DC F'8400
specified is a fullword. the operation from
AH to A or data
definition to DATA1
DC H ‘8400’

DATA1 00 00 20 D0
Computer adds
the Halfword
AH R3,DATA1 which starts at
DATA1 to R3

© Copyright IBM Corp., 2000, 2004. All rights reserved.

67
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Addition and Subtraction

Subtract instruction

Subtract S
Example :
• The full word (4 bytes) located at the address
specified by operand-2 is subtracted from the register
specified by operand-1. L R1,REG1
• Operand-2 remains unchanged. S R1,REG2
• The condition code is set as shown below ST R1,REG1

0 - op1=0, 1 - op1<0, 2 - op1>0, 3 - overflow R1 EQU 1

REG1 DC XL4'00000001'
REG2 DC XL4’02010405’

5B RX BDDD
* REG1 = REG1 - REG2

Format :

S R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Addition and Subtraction

Subtract Halfword instruction

Subtract Halfword SH
Example :
• The half word (2 bytes) located at the address
specified by operand-2 is subtracted from the register
specified by operand-1 .
• Operand-2 remains unchanged. SH 5,X'6B0'(13,12)
• The condition code is set as shown below

0 - op1=0, 1 - op1<0, 2 - op1>0, 3 - overflow

4B RX BDDD

Format :

SH R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

68
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Addition and Subtraction

Subtract Register instruction

Subtract Registers SR
Example :
• The register specified by operand-2 is subtracted
from the register specified by operand-1. Operand-2
remains unchanged.
• The condition code is set as shown below SR R3,R4
ST R3,REG1
0 - op1=0, 1 - op1<0, 2 - op1>0, 3 - overflow

R3 EQU 3
R4 EQU 4

1B RR

Format :

SR R1,R2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Addition and Subtraction

Subtract Register Instruction (cont’d)

One application of SR is to zero a register. You


could set a register to zero by loading a fullword ZERO a register:
of zero into it. L R4, = F’0’

For example: L R4,=F'0'

This method requires 4 bytes for the instruction (L Uses 8 bytes of storage
is an RX instruction) plus 4 bytes for the literal.

A better way to set R4 to zero is by subtracting its 0 Register R4

contents from itself.


Uses 2 bytes of storage
SR R4,R4

Regardless of the initial value of R4, the result will


SR R4, R4
be zero. This method only uses 2 bytes of
storage, for the RR instruction.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

69
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Fixed-Point Binary Arithmetic

Topics:
 Decimal to Binary Conversion

 Addition and Subtraction

 Multiplication

 Division

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Multiplication

Multiplication

When you perform addition and subtraction, the


result is of roughly the same magnitude, or less
Even-Odd Register Pair:
than the largest of the operands. Thus, it makes 99
sense that when you are adding two fullwords, the *
sum also be a fullword. However, when you do 99
=9801
multiplication, the number of digits in the product
is roughly the sum of the number of digits in the
multiplicand and multiplier. Multiplicand

Multiplier
The multiplier and multiplicand have two digits
each, and the products has four digits. It makes Product
sense, then, that when a multiply instruction is
designed as a part of a computer architecture, it
2 digits
should provide for a product larger than the +
multiplier and the multiplicand. 2 digits
=4 digits

© Copyright IBM Corp., 2000, 2004. All rights reserved.

70
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Multiplication

Multiply Instruction

What is the Multiply Instruction? Multiply Instruction (M):


RX Format - Register to Indexed Storage

The Multiply Instruction (M) multiplies a fullword in M R4, FWD4


the GPR (one greater than the first operand
register specified), by a fullword second operand 1st Operand 2nd Operand
storage. The product, a 64 bit fixed point
M
number is placed in the first operand register, and
Multiplier Multiplicand
in the register one greater. The first operand must
specify an even numbered register, or a FULLWORD FULLWORD
X
specification exception will occur. It is impossible
FWD4 R4 Located
for overflow to occur. Specifies in Odd
Even Register Register (R5)
For example: M R4, FWD4 PRODUCT PRODUCT placed
in Even-Odd Register
The fullword in R5 is multiplied by the fullword at
R4 Even R5 Odd
main storage location FWD4. The 64-bit product High Order Low Order
Register Register
is placed in GPRs 4 and 5.
CC Setting - Does not set the Condition Code

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Multiplication

Multiply Instruction

Multiply M
Example :
• The value located at the storage address specified by
operand-2 is multiplied with the 2nd-word (the second
register of the pair) of operand-1.
• The double word result is put in operand-1 M 4,X‘150'(11,12)
• Operand-2 remains unchanged.
* The contents of R4 are not significant.
* R5 contains 00 00 00 9A (multiplicand)
* R11 contains 00 00 06 00.
* R12 contains 00 00 30 00.
* Location 14160 contains 00 00 00 83

5C RX BDDD * After execution -----


* R4 contains 00 00 00 00.
* R5 contains 00 00 4E CE
Format :

M R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

71
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Multiplication

Multiplying Fullwords

L R3,NUM5 Put multiplicand into odd register


M R2,NUM6 Calculate product
ST R3,NUM7 Store low order 32 bytes of the product

How do you multiply two fullwords?

If you want to multiply two fullwords, such as NUM5 and NUM6, and put the product in NUM7, which is also a
fullword, assuming that the absolute value of the product will not be greater than 231 - 1

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Multiplication

Multiply Half word Instruction

Multiply Halfword MH
Example :
• The half word (2 bytes) located at the address
specified by operand-2 is multiplied by the register
specified by operand-1
• The product is put in operand-1 MH 5,X'6B0'(13,12)
• Operand-2 remains unchanged.

* R5 contains 00 00 00 19 = 2510.
* R12 contains 00 00 18 00.
* R13 contains 00 00 01 50
* Locations 2000 contains 210 (decimal).
* R5 after execution 527100 (decimal)
4C RX BDDD

Format :

MH R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

72
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Multiplication

Multiply Register Instruction

Multiply Register Instruction (MR):


RR Format - Register to Register Storage
The Multiply Register Instruction (MR) multiplies a
MR R2, R5
fullword in the GPR one greater than the first
operand register specified, by the fullword in the
1st Operand 2nd Operand
register specified as the second operand. The
R5 op2 located
product, a 64 bit fixed point number, is placed in MR
Specifies in Odd
the first operand register, and in the register one Even Register Register (R3)
greater. The first operand must specify an even FULLWORD FULLWORD
numbered register. X
Multiplier Multiplicand

For example: MR R2,R5


PRODUCT placed
PRODUCT
The fullword in R3 is multiplied by the fullword in in Even-Odd Register

R5. The 64 bit product is placed in GPRs 2 and 3.


R2 Even R3 Odd
High Order Low Order
Register Register

CC Setting - Does not set the Condition Code

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Multiplication

Multiply Instruction (cont’d)

Multiply Registers MR
Example :
• The value in operand-2 is multiplied with the 2nd-
word (the second register of the pair) of operand-1.
• The double word result is put in operand-1
• Operand-2 remains unchanged. MR R3,R4

R3 EQU 3
R4 EQU 4

* Multiply value of R4 by itself and put the


* result in R3, R4 pair
* This example is to square the number
1C RR

Format :

MR R1,R2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

73
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate

UNIT Fixed-Point Binary Arithmetic

Topics:
 Decimal to Binary Conversion

 Addition and Subtraction

 Multiplication

 Division

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Division

Fixed-Point Division

What are the two fixed-point division


operations? D R2,X

There are only two fixed-point division operations, 1st Operand 2nd Operand
D and DR. There is no DH instruction.
EVEN-ODD REGISTER PAIR
Fixed-point arithmetic is integer arithmetic, so DIVIDEND
division produces both an integer quotient and an DOUBLEWORD
integer remainder. The dividend is a 64-bit
BEFORE EXECUTION
quantity in an even-odd register pair. The divisor
EVEN ODD
is a fullword. DOUBLEWORD
REGISTER REGISTER

After the division, the remainder is placed in the Specified by One Greater then
1st Operand 1st Operand
even numbered register, specified by the first
operand, and the quotient goes into the odd REMAINDER QUOTIENT

numbered register one greater than the first FULLWORD FULLWORD


operand. AFTER EXECUTION

© Copyright IBM Corp., 2000, 2004. All rights reserved.

74
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Division

Signs of Quotient and Remainder

DIVIDEND DIVISOR QUOTIENT REMAINDER


17 5 3 2

-17 5 -3 -2
17 -5 -3 2

-17 -5 3 -2
15 5 3 0

The table above indicates the results of some division operations, showing which signs are generated for both
quotient and remainder.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Division

Divide Instruction (cont’d)

What is the Divide instruction? Divide Instruction(D):


RX Format – Register to Indexed Storage

The Divide Instruction (D) divides the 64 bit fixed


point number in the even-odd register pair D R4, DIVISOR
specified by the first operand (which must be
even), by the fixed point fullword in the storage 1st Operand 2nd Operand
location specified by the second operand. The
result consists of a fullword remainder that is D

placed in the even numbered register (R1), and a Dividend Divisor


fullword quotient which is placed in the odd DOUBLEWORD FULLWORD
numbered register (R1 +1).
R4 ÷ DIVISOR
Specifies
For example: D R4, DIVISOR Even Register

The 64 bit fixed point number in registers R4 and R4 R5


Even Remainder Quotient Odd
R5 is divided by the fullword at DIVISOR, and the Register Register
integer remainder is placed in R4 and the integer
CC Setting – Does not set the Condition Code
quotient in R5.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

75
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Arithmetic Operations

Divide Instruction

Divide D
Example :
• Operand-1 is an even/odd pair of registers (dividend)
that is divided by the location specified by operand-2 . L R8,HEX0
• The remainder is put in R1-even and the quotient is L R9,HEX5
put in R1-odd. D R8,HEX2

R8 EQU 8
R9 EQU 9
HEX0 DC XL4'00000000'
HEX2 DC XL4'00000002'
HEX5 DC XL4'00000005‘

5D RX BDDD * Load EVEN-NUMBERED R8 with 0


* Load ODD-NUMBERED R9 with 4
* After Divide R9 will have 2 and
Format :
* R8 will have remainder 1

D R1,D2(X2,B2)

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Division

Divide Instruction (cont’d)

If you wanted to divide a 64-bit variable called


DIVIDEND, by a fullword variable called
LM R2,R3,DIVIDEND
DIVISOR, to produce fullword QUOTIENT and D R2,DIVISOR
REMAINDER, you could use the code shown on ST R2,REMAINDER
the right. ST R3,QUOTIENT

LOAD MULTIPLE

R2 32-bit R3 32-bit

DIVIDEND

HIGH ORDER LOW ORDER


Even Register Odd Register

DIVIDE - (R2,R3) Dividend by Divisor

STORE - Remainder into Even Register R2

Quotient into Odd Register R3

© Copyright IBM Corp., 2000, 2004. All rights reserved.

76
12/12/2009

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Decimal Arithmetic Topic: Arithmetic Operations

Divide Register Instruction

Divide Registers DR
Example :
• Operand-1 is an even/odd pair of registers
(dividend) that is divided by by operand-2 . L R8,HEX5
• The remainder is put in R1-EVEN and the quotient SRDA R8,32(0) Shift value to R9
is put in R1-ODD L R3,HEX2
D R8,R3

R3 EQU 3
R8 EQU 8
R9 EQU 9

HEX2 DC XL4'00000002'
1D RR HEX5 DC XL4'00000005’

Format : * After Divide R9 will have 2 and


* R8 will have remainder 1
DR R1,R2

© Copyright IBM Corp., 2000, 2004. All rights reserved.

Assembler
AssemblerProgramming
Programming Part 2
Intermediate
Unit: Fixed-Point Binary Arithmetic Topic: Division

Divide Register Instruction

What is the Divide Register instruction?


Divide Register Instruction(DR):
RX Format – Register to Register
The Divide Register Instruction (DR) divides a 64
bit fixed point number in the even-odd register D R4, R7
pair specified by the first operand (which must be
even), by the fixed point fullword in the GPR 1st Operand 2nd Operand
specified by the second operand. The result
consists of a fullword remainder that is placed in DR

the even numbered register (R1), and a fullword Dividend Divisor


quotient that is placed in the odd numbered DOUBLEWORD FULLWORD
register (R1+1).
R4 ÷ R7
Specifies
For example: DR R4,R7 Even Register

The 64 bit fixed point number in registers R4 and R4 R5


Even Remainder Quotient Odd
R5 is divided by the fullword in R7, and the Register Register
integer remainder is placed in R4 and in the
CC Setting – Does not set the Condition Code
integer quotient in R5.

© Copyright IBM Corp., 2000, 2004. All rights reserved.

77

You might also like