addition (original) (raw)
| | | OSdata.com | | ---------------------------------------------------------------------------------------------- | | ---------- |
summary
This subchapter looks at addition.
addition
This subchapter looks at addition.
The ADD statement is used for addition.
In a simple version of the ADD statement, you name two variable identifiers that are to be added and the variable identifier where the resulting answer is to be stored:
005010 ADD OLD-SUBTOTAL, CURRENT-COUNT
005020 GIVING NEW-SUBTOTAL.
| before execution | | | | | | | | | | | | | | | | | | | | | | | | ------------------- | - | ------------- | - | --------- | | ------------ | - | - | - | - | | - | - | - | - | - | | - | - | - | - | - | | OLD-SUBTOTAL | | CURRENT-COUNT | | WORK AREA | | NEW-SUBTOTAL | | | | | | | | | | | | | | | | | | 1 | 1 | 1 | 1 | 1 | | 2 | 2 | 4 | 4 | 4 | | | | | | | | | | | | | | partial computation | | | | | | | | | | | | | | | | | | | | | | | | OLD-SUBTOTAL | | CURRENT-COUNT | | WORK AREA | | NEW-SUBTOTAL | | | | | | | | | | | | | | | | | | 1 | 1 | 1 | 1 | 1 | | 2 | 2 | 4 | 4 | 4 | | 3 | 3 | 5 | 5 | 5 | | | | | | | | after execution | | | | | | | | | | | | | | | | | | | | | | | | OLD-SUBTOTAL | | CURRENT-COUNT | | WORK AREA | | NEW-SUBTOTAL | | | | | | | | | | | | | | | | | | 1 | 1 | 1 | 1 | 1 | | 2 | 2 | 4 | 4 | 4 | | 3 | 3 | 5 | 5 | 5 | | 3 | 3 | 5 | 5 | 5 |
This example adds whatever number is stored in OLD-SUBTOTAL (11,111) with whatever number is stored in CURRENT-COUNT (22,444) and places the answer into NEW-SUBTOTAL (33,555).
The word ADD must appear in Area B of the COBOL card. The statement is terminated wiith a period.
There can be any number of fields (items named by identifiers) added together (possibly a huge list). The commas between identifiers are optional, but should be used to make your program more readable.
It is possible to place the answer into more than one variable (again the commas are optional).
005010 ADD OLD-SUBTOTAL, CURRENT-COUNT
005020 GIVING NEW-SUBTOTAL, MASTER-COUNT.
| before execution | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ------------------- | - | ------------- | - | --------- | | ------------ | - | ------------ | - | - | | - | - | - | - | - | | - | - | - | - | - | | - | - | - | - | - | | OLD-SUBTOTAL | | CURRENT-COUNT | | WORK AREA | | NEW-SUBTOTAL | | MASTER-COUNT | | | | | | | | | | | | | | | | | | | | | | 1 | 1 | 1 | 1 | 1 | | 2 | 2 | 4 | 4 | 4 | | | | | | | | | | | | | | | | | | | | partial computation | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | OLD-SUBTOTAL | | CURRENT-COUNT | | WORK AREA | | NEW-SUBTOTAL | | MASTER-COUNT | | | | | | | | | | | | | | | | | | | | | | 1 | 1 | 1 | 1 | 1 | | 2 | 2 | 4 | 4 | 4 | | 3 | 3 | 5 | 5 | 5 | | | | | | | | | | | | | | after execution | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | OLD-SUBTOTAL | | CURRENT-COUNT | | WORK AREA | | NEW-SUBTOTAL | | MASTER-COUNT | | | | | | | | | | | | | | | | | | | | | | 1 | 1 | 1 | 1 | 1 | | 2 | 2 | 4 | 4 | 4 | | 3 | 3 | 5 | 5 | 5 | | 3 | 3 | 5 | 5 | 5 | | 3 | 3 | 5 | 5 | 5 |
In the example, OLD-SUBTOTAL and CURRENT-COUNT are added together in a temporary storage area created by the compiler, and then the same answer is placed into both NEW-SUBTOTAL and MASTER-COUNT.
It is possible to use literals in the ADD statement.
005010 ADD 58, CURRENT-COUNT
005020 GIVING NEW-COUNT.
The literal 58 (the actual number) is added to the contents of the variable CURRENT-COUNT and the answer is stored in the vaariable NEW-COUNT.
005010 ADD 20, 30, 40
005020 GIVING SUB-TOTAL.
The literal numbers 20, 30, and 40 are added together and the answer (90) is stored in the variable SUB-TOTAL.
005010 ADD 30.00, 40.00
005020 GIVING SUB-TOTAL.
The literal numbers 20.00 and 40.00 are added together and the answer (70.00) is stored in the variable SUB-TOTAL.
Numeric literals must have at least one digit and not more than 18 digits. Numeric literals may contain the dedimal digits 0 through 9, inclusive, the plus sign, the minus sign, and/or the decmal point. The decimal point may not be the right most character. An unsigned numeric literal is assumed to be pisitive. A numeric literal without a decimal point is assumed to be a whole number.
The TO option may be used instead of the GIVING option.
005010 ADD CURRENT-COUNT TO MASTER-COUNT.
| before execution | | | | | | | | | | | | | | | | | | ------------------- | - | --------- | - | ------------ | | - | - | - | - | - | | - | - | - | - | - | | CURRENT-COUNT | | WORK AREA | | MASTER-COUNT | | | | | | | | | | | | | | 1 | 1 | 2 | 2 | 2 | | | | | | | | 2 | 2 | 4 | 4 | 4 | | partial computation | | | | | | | | | | | | | | | | | | CURRENT-COUNT | | WORK AREA | | MASTER-COUNT | | | | | | | | | | | | | | 1 | 1 | 2 | 2 | 2 | | 3 | 3 | 6 | 6 | 6 | | 2 | 2 | 4 | 4 | 4 | | after execution | | | | | | | | | | | | | | | | | | CURRENT-COUNT | | WORK AREA | | MASTER-COUNT | | | | | | | | | | | | | | 1 | 1 | 2 | 2 | 2 | | 3 | 3 | 6 | 6 | 6 | | 3 | 3 | 6 | 6 | 6 |
The number stored in the variable CURRENT-COUNT (11,222) is added to the number stored in the variable MASTER-COUNT (22,444) and the answer (33,666) is stored in MASTER-COUNT.
It is possible to use numeric literals with this option also.
005010 ADD 1 TO LINE-COUNT.
The example increments (adds one to) the variable LINE-COUNT.
A more complicated version of the ADD statement with the TO option:
005010 ADD HERB-COUNT, SPICE-COUNT TO INGREDIENT-COUNT, FLAVORING-COUNT.
| before execution | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | -------------------------- | - | ----------- | - | --------- | | ---------------- | - | --------------- | - | - | | - | - | - | - | - | | - | - | - | - | - | | - | - | - | - | - | | HERB-COUNT | | SPICE-COUNT | | WORK AREA | | INGREDIENT-COUNT | | FLAVORING-COUNT | | | | | | | | | | | | | | | | | | | | | | 0 | 0 | 1 | 1 | 1 | | 0 | 0 | 2 | 2 | 2 | | | | | | | | 1 | 2 | 3 | 4 | 5 | | 0 | 0 | 1 | 2 | 3 | | first partial computation | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | HERB-COUNT | | SPICE-COUNT | | WORK AREA | | INGREDIENT-COUNT | | FLAVORING-COUNT | | | | | | | | | | | | | | | | | | | | | | 0 | 0 | 1 | 1 | 1 | | 0 | 0 | 2 | 2 | 2 | | 0 | 0 | 3 | 3 | 3 | | 1 | 2 | 3 | 4 | 5 | | 0 | 0 | 1 | 2 | 3 | | first computation complete | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | HERB-COUNT | | SPICE-COUNT | | WORK AREA | | INGREDIENT-COUNT | | FLAVORING-COUNT | | | | | | | | | | | | | | | | | | | | | | 0 | 0 | 1 | 1 | 1 | | 0 | 0 | 2 | 2 | 2 | | 0 | 0 | 3 | 3 | 3 | | 1 | 2 | 6 | 7 | 8 | | 0 | 0 | 1 | 2 | 3 | | after execution | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | HERB-COUNT | | SPICE-COUNT | | WORK AREA | | INGREDIENT-COUNT | | FLAVORING-COUNT | | | | | | | | | | | | | | | | | | | | | | 0 | 0 | 1 | 1 | 1 | | 0 | 0 | 2 | 2 | 2 | | 0 | 0 | 3 | 3 | 3 | | 1 | 2 | 6 | 7 | 8 | | 0 | 0 | 4 | 5 | 6 |
In the example, the numbers stored in HERB-COUNT (111) and SPICE-COUNT (222) are added together and temporarily stored in a compiler generated work area (333). The subtotal in the work area (333) is then added to the number stored in the variable INGREDIENT-COUNT (12,345), with the result stored in INGREDIENT-COUNT (12,678). The subtotal in the work area (333) is then added separately to the number stored in the variable FLAVORING-COUNT (123) and this (in this case, a different answer) is stored in the variable FLAVORING-COUNT (456).
The ROUNDED option may be placed after any identifier that follows the GIVING option or the TO option. This rounds off the results of the ADD before the answer is stored.
005010 ADD OLD-SUBTOTAL, CURRENT-COUNT
005020 GIVING NEW-SUBTOTAL ROUNDED, MASTER-COUNT.
The addition of OLD-SUBTOTAL and CURRENT-COUNT is stored into both NEW-SUBTOTAL and MASTER-COUNT, but the answer stored in NEW-SUBTOTAL is rounded, while the answer stored in MASTER-COUNT is not rounded.
005010 ADD CURRENT-COUNT TO MASTER-COUNT ROUNDED.
The numbers stored in the variables CURRENT-COUNT and MASTER-COUNT are added together, and the rounded off answer is tored in MASTER-COUNT.
All fields added toegtehr with the ADD statement are aligned to the decimal point in each field. All additions are algebraiac, so the resulting answer will have the correct sign according to the rules of elementary algebra.
The programmer needs to be careful about overflow. If the variable receiving the answer does not have enough digits to hold the entire answer (the variable is to small or the answer is too large, depending on how you view it), then significant digits will be lost and the variable will hold an incorrect answer. It is the responsibility of the programmer to make sure that the receiving field is large enough to hold all of the digits for the largest possible answer.
An integer variable may be incremented by either of the following assignment statements:
i=i = i=i + 1;
-OR-
$i++;
number systems
Here is a little bit of college level work on addition.
A system of numbers os considered to be closed under an operation if it reproduces itself.
S is a set of numbers. S is closed under addition if for any two numbers a and b in the set S, the sum of a + b is also a number in the set S. It follows that any sum of two, three, four, or any finite number of elements in S also belong to S.
The set of all natural numbers {1, 2, 3, …} is closed under addition.
The set of all even integers {0, ±2, ±4, ±6, …} is closed under addition. The set of all odd integers is not closed under addition.
The set of all positive real numbers is closed under addition.
The ste of all integers is closed under addition.
The set of all rational numbers is closed under addition.
The set of all real numbers is closed under addition.
The set of all complex numbers is closed under addition.
functions
+(x) — APL dyadic function that returns the sum (addition) of a scalar, vector, or matrix. Form is A+B.
ADD1(x) — LISP function that takes one argument of type fixed or float and increments the value.
ADD(x,y,p[.q]) — Pl/I built-in function that returns the sum of two variables (x plus y) in a field of p digits with the option of q fractional digits.
add(x) — PosScript arithmetic operator that places on the top of the stack the sum of the two numbers previously on the top of stack. Returns an integer if both operands are integers and the result is within the integer range, otherwise returns a real.
SUM(x) — Pl/I built-in function that returns the total sum of all of the values of the elements in an arithmetic array x. Note that the IBM PL/I-F compiler converts all array values to floating point to perform the operation and returns a result in floating point. This may create small rounding errors for commercial programmers needing dollar and cents accuracy.
assembly language instructions
For most processors, integer arithmetic is faster than floating point arithmetic. This can be reversed in special cases such digital signal processors.
On many processors, floating point arithmetic is in an optional unit or optional coprocessor rather than being included on the main processor. This allows the manufacturer to charge less for the business machines that don’t need floating point arithmetic.
The basic four integer arithmetic operations are addition, subtraction, multiplication, and division. Arithmetic operations can be signed or unsigned (unsigned is useful for effective address computations). Some older processors don’t include hardware multiplication and division. Some processors don’t include actual multiplication or division hardware, instead looking up the answer in a massive table of results embedded in the processor.
A specialized, but common, form of addition is an increment instruction, which adds one to the contents of a register or memory location. For address computations, “increment” may mean the addition of a constant other than one. Some processors have “short” or “quick” addition instructions that extend increment to include a small range of positive values.
- ADD Arithmetic Addition; DEC VAX; signed addition of scalar quantities (8, 16, or 32 bit integer or 32 or 64 bit floating point) in general purpose registers or memory, available in two operand (first operand added to second operand with result replacing second operand) and three operand (first operand added to second operand with result placed in third operand) (ADDB2 integer add byte 2 operand, ADDB3 integer add byte 3 operand, ADDW2 integer add word 2 operand, ADDW3 integer add word 3 operand, ADDL2 integer add long 2 operand, ADDL3 integer add long 3 operand) (ADDF2 add float 2 operand, ADDF3 add float 3 operand, ADDD2 add double float 2 operand, ADDD3 add double float 3 operand, ADDG2 add G float 2 operand, ADDG3 add G float 3 operand, ADDH2 add H float 2 operand, ADDH3 add H float 3 operand); clears or sets flags
- ADD Add Integers; Intel 80x86; integer add of the contents of a register or memory (8, 16, or 32 bits) to a memory location or a register; sets or clear flags
- ADD Add; Motorola 680x0, Motorola 68300; signed add of the contents of a data register (8, 16, or 32 bits) to a memory location or adds the contents of a memory location (8, 16, or 32 bits) to a data register; sets or clear flags
- ADD Add; ARM; Integer add of operand2 and Register n with result stored in register d; conditionally executed; optionally updates flags
- ADD Add Wide; ARM (Thumb 2); Integer add of right rotated immediate constant and Register n with result stored in register d
- ADD Add; MIX; add word or partial word field contents of memory to A-register (accumulator), overflow toggle set if result is too large for A-register
- AR Add Register; IBM 360/370; RR format; signed add of the contents of a general purpose register (32 bits) to a general purpose register (32 bits); register to register only; sets or clears flags
- A Add; IBM 360/370; RX format; signed add of the contents of a memory location (32 bits) to a general purpose register (32 bits); main memory to register only; sets or clears flags
- AH Add Half-word; IBM 360/370; RX format; signed add of the contents of a memory location (16 bits) to a general purpose register (low order 16 bits); main memory to register only; sets or clears flags
- ADDA Add Address; Motorola 680x0, Motorola 68300; unsigned add of the contents of a memory location or register (16 or 32 bits) to an address register; does not modify flags
- ADDI Add Immediate; Motorola 680x0, Motorola 68300; signed add of immediate data (8, 16, or 32 bits) to a register or memory location; sets or clears flags
- ADDQ Add Quick; Motorola 680x0, Motorola 68300; signed add of an immediate value of 1 to 8 inclusive to a register or memory lcoation; sets or clears flags for data registers and memory locations, does not modify flags for an address register
- INC Increment; DEC VAX; increments the integer contents of a general purpose register or contents of memory (INCB byte, INCW word, INCL longword); equivalent to ADDx2 #1, sum, but shorter and executes faster; clears or sets flags
- INC Increment by 1; Intel 80x86; increments the contents of a register or memory (8, 16, or 32 bits); sets or clear flags (does not modify carry flag)
- ADWC Add With Carry; DEC VAX; integer addition (32 bit) in general purpose registers or memory, first operand added to second operand and the C (carry) flag with result replacing second operand; used for multiprecision arithmetic; clears or sets flags
- ADC Add Integers with Carry; Intel 80x86; integer add of the contents of a register or memory (8, 16, or 32 bits) and the carry flag to a memory location or a register, used to implement multi-precision integer arithmetic; sets or clear flags
- ADC Add with Carry; ARM; Integer add of operand2 and Register n and Carry with result stored in register d; conditionally executed; optionally updates flags
- ADDX Add Extended; Motorola 680x0, Motorola 68300; (signed add of a data register [8, 16, or 32 bits] and the extend bit to a data register) or (signed add of the contents of memory location [8, 16, or 32 bits] and the extend bit to the contents of another memory location while predecrementing both the source and destination address pointer registers), used to implement multi-precision integer arithmetic; sets or clears flags
- ADAWI Add Aligned Word Interlocked; DEC VAX; adds (16 bit integer) a source operand from a register or memory to a memory location that is word aligned while interlocking the memory location so that no other processor or device can read or write to the interlocked memory location, used to maintain operating system resource usage counts; and sets or clears flags
See also Integer Arithmetic Instructions in Assembly Language and Floating Point Arithmetic Instructions in Assembly Language
free music player coding example
Coding example: I am making heavily documented and explained open source code for a method to play music for free — almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).
View music player in action: www.musicinpublic.com/.
Create your own copy from the original source code/ (presented for learning programming).
Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.
†UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.