Unit 2.1 -- Basic Notions--Adding Two Numbers Together
PROG
Add two numbers together. The two numbers are in memory
locations A and B. The result will be put in the memory
location in C.
PED
Learn about memory locations, registers, and arithmetic
operations, and the layout of an Assembler Program.
CONCEPTS
Memory location - a contiguous set of bytes and the address
of the first byte that contains one thing, e.g., a full word
integer.
Register - One of sixteen four-byte entities that can
contain a full word. They are numbered from 0 to 15. You
should NOT use the one in the "USING" statement. NOR should
you use register 14.
Addressability - established with the BALR and USING
statement. This register in the BALR instruction is loaded
with the address of a memory location in the program. All
The assembler sets up references to memory
automatically relative to this base register.
If you copy the BALR and USING statement from the template,
you do not need to worry about this.
SF
instruction layout:
LABEL space INSTRUCTION space OPERANDS space COMMENTS
The LABEL may be omitted.
The INSTRUCTION (operator or pseudo-op) conventionally goes
in column 10.
The OPERANDS conventionally go in column 16.
comments:
Comments can go in the position shown above in the
instruction layout, or there can be a whole comment line.
A whole comment line is indicated with a "*" in column 1.
instructions
Load instruction (L)
-copies memory location in second operand to register
indicated by number in first operand
Add instruction (A)
-adds memory location in second operand to register
indicated by number in first operand
Store instruction (ST)
-copies contents of register indicated by number in first
operand to memory location in second operand
WARNING, there are NO memory to memory instructions on the
IBM 360!
There ARE register to register instructions, but they have
DIFFERENT OPCODES than those given above.
Define Constant (DC)
label DC f'nn'
-before program is executed, the memory location indicated
by "label" will be filled with "nn"
-since "f" was used, four bytes of memory was allocated
starting at the memory location indicated by "label"
Define Storage (DS)
label DS f
-allocates four bytes of memory starting at the address
given by "label." The contents are undefined.
-note that the "f" stands for "fullword" so 4 BYTES of
memory was the amount allocated by the "DS" statement.
(Remember: fullword = 32 bits = 4 bytes)
Other statements in the template
label START
-says where the computer is to branch to when you execute
the program
BALR reg,0
-loads register "reg" with address of next statement.
(BALR is also used for subroutines.)
USING memory location,reg
-tells ASSEMBLER that register "reg" will contain the
address of "memory location"
-Necessary, since the operating system is entitled to load
your program at any address it chooses.
-The beginning of your program will NOT be at address zero.
* - If not in column 1, this symbol stands for current
"location counter."
This has a different value in every statement in which
it
is used.
BR 14
-This provides a place for the breakpoint necessary in
using the debugger. It also returns control to the
operating
system. NOTE that this latter feature REQUIRES that you
DON'T
use register fourteen for other purposes.
END
-needed to tell ASSEMBLER that this is the last statement of
the
program; it does NOT generate any kind of execution-time
STOP
Section 2: Fundamentals and Arithmetic
In this section, you will learn how to translate assignment
statements into IBM/360 ASSEMBLER language.
We will learn about assignment statements as simple as:
C:=A
and as complicated as:
C:=(D-(C+F-(C+G)))
Our first example will be:
C:=A+B
(Note that there are no multiplications or divisions in
either of these examples. There are multiply and divides on
some computers but not others. We will learn in Unit 14 how
to do multiplies and divides by successive additions or
subtractions, respectively. That will work on all machines.
Some students might learn about the Multiply and Divide
instructions on the IBM 360 architecture by looking at the
manual or SCHAUM or SILVER.)
(HISTORICAL aside:
I will refer to a whole bunch of machines as IBM/360's.
International Business Machines Corporation has turned out
several series of computers, all of which run approximately
the same instruction set. This was the instruction set and
architecture that appeared on the IBM 360 architecture in
approximately 1966. A few instructions, chiefly for
something called "virtual memory" were added. Most of the
IBM 370 would run on the original IBM/360's. All the
programs that run on the IBM/360 would run on the IBM/370.
We thus say that the IBM/370 is "upward compatible" with the
IBM/360. This is important as it means that all the
programs that ran on the IBM/360 would also run on the
IBM/370. Since that time we have seen the IBM 3090, IBM
3080, IBM 43xx and IBM 93xx machines. These all run the
IBM/370, and hence the IBM/360, instructions Other firms
such as Amdahl, National Advanced Systems and Formation also
introduced computers than run the IBM/370 series
instructions. Remember that one could take a program
compiled and ASSEMBLED on the IBM/360 and run it on any of
the above-mentioned machines. One could not run it on an
IBM PC, a VAX, a Burroughs or many other architectures.
END of Historical Aside)
Before we look at the instructions, or how we convert PASCAL
assignment statements to them, we need to learn about some
things that are found inside the IBM computer.
CONCEPTS
One of the most important are memory locations. Memory
locations can store lots of things such as characters,
integers, floating point numbers and even the computer
instructions that are executed. On the IBM/360, the memory
is organized into something called bytes, Each byte
contains eight bits. That means it could contain a signed
number between -128 and 127.
However, the instructions for dealing with these are
difficult. Thus, we will deal with integers, or to use the
IBM term, FULLWORD integers. These take four bytes. That
means if memory location 20000 contained one integer, the
next integer (or other type of information) could be put at
20004. The next one would be at 20008. The next one would
be at 2000C (assuming all the numbers before were hex
numbers).
Since there are four eight-bit bytes comprising one
fullword, there are a total of thirty-two bits in a fullword
on the IBM 360. It could contain a number between -231 and
231-1. This is a number between -2147483648 and 2147483647-
-some reasonably large numbers.
(There are also half words that are two bytes each. We
won't discuss these in class.)
These memory locations are given names like A,VAR0001,BLAH (
like the variable names you saw in PASCAL). They each
contain one integer. We will learn that there are DC F and
DS F instructions to create them. The DC F also gives the
memory location an initial value.
There are also sixteen boxes called registers. They also
contain an integer--thirty two bits. They are numbered zero
through fifteen. We will see that registers twelve and
fourteen are used for special purposes and should not be
used.
Unlike PASCAL, the only thing we will be able to do with a
quantity in a memory location is to move it to a register.
This is done with a LOAD instruction. We can also perform an
operation such as addition or subtraction between an integer
in memory and a register. In addition, we can take a
quantity in a register and transfer it to memory. This is
done with the store instruction. These are all RX
instructions.
In addition to performing operations between memory and
register, it is also possible to perform operations between
registers and registers. One can do the full complement of
arithmetic, etc., between registers and registers.
In order for any instruction to work with a memory location
(A,BLAH, etc.) including branches, such memory location must
be addressable. Two things must happen--we must load
register twelve with a specific value using the BALR
instruction. This value must remain intact until the
program is finished. If you should inadvertently change
register twelve, lots of weird symptoms will occur. Also
register fourteen will contain the address in the operating
system to return to. If you change this, your program will
function correctly, but you will get a weird message before
the READY prompt since the final "BR 14" will fail.
Thus, you can use registers 0 to 11, 13 and 15 without any
problems.
The instructions from the "P1 START" statement to the
comment "YOUR PROGRAM GOES HERE" establish addressability.
You need to ensure that these appear in your program. Right
now, these are magic. The "BR 14" is magic that will get
your program back to the operating system without weird
error messages.
We will discuss what is happening--pull the hood up off the
magic--in section nine. For now, please bear with us.
We now look at the instructions that you will be writing
yourselves. First a few words about syntax.
Each line of ASSEMBLER contains the following parts
LABEL INSTRUCTION OPERANDS COMMENTS
The LABEL is separated from the INSTRUCTION by one or more
spaces. The INSTRUCTION is separated from the OPERANDS by
one or more spaces. The OPERANDS are separated from the
COMMENTS by one or more spaces. The LABEL is usually
omitted. COMMENTS are often (sometimes, too often) omitted.
There are usually two parts in the operand field, separated
by a comma. Note that you should have a contiguous sequence
of characters--no blanks between the first character of the
first operand and the last character of the last operand.
Otherwise, what you meant to be an operated, will be treated
as a COMMENT. Remember that the COMMENT field of the line
is separated from the OPERAND field by a blank.
If the LABEL is omitted, you still must have one or more
spaces before the INSTRUCTION. That way the computer can
tell that you did omit the LABEL.
It is customary to put all the INSTRUCTION fields in column
ten and each operand in column sixteen. The program will
work fine if your stuff is all higgly-piggly with nothing
aligned. (Of course, you must obey the above rules.)
When we learn how to convert IF and DO statements to
ASSEMBLER, we will see cases where we begin some
instructions in columns 12 and 14 to match the normal
indentation used in such statements.
Full-line comments are a special case. When a "*" appears
in column one, the entire line is treated as a comment and
ignored.
Some examples from the first program:
L 1,A
instruction-- L
operands-- 1,A
A 1,B
instruction--A
operands--1,B
P1 START
label--P1
instruction--START
A DC F'3'
A--label
instruction--DC
operand--F'3'
Now we talk about the storage definition. Memory is
allocated, and in the case of DC's, initialized, before
anything else happens.
A DC F'3'
will cause the memory location A to be assigned and to have
the initial contents three.
B DC F'4'
will cause the memory location B to be assigned and to have
the initial contents four.
C DS F
will cause a memory location to be assigned. It will have
some arbitrary initial contents--depending upon what program
was sitting in memory before your program got loaded.
After all this, we get to talk about the three instructions
that
we will learn in the first example.
L 1,A
will load the contents of the memory location A into the box
called Register One. After this instruction is executed,
register one will contain three.
A 1,B
will add the contents of the memory location B to the box
called Register One. After this instruction is executed,
register one will contain seven.
ST 1,C
will copy the contents of register one to the memory
location C. Memory location will contain seven. Register
one will still contain seven.
Let's mention a few facts about the LISTING file and
ASSEMBLE file. The item in your classnotes is a listing
file. If you were to type this program into the machine,
you would type everything from the "P1" on down to the lower
right. This would be typed into a file whose file type is
ASSEMBLE and then run through the ASSEMBLER to generate a
LISTING file and an executable.
The columns LOC, OBJECT, CODE, ADDR1, ADDR2 and STMT are
added by the IBM ASSEMBLER in the process of generating the
LISTING file. The numbers in the section "OBJECT CODE" are
the instructions. Instructions are bit patterns just like
integers created with DC F'3'. Both get put in memory. It
is possible to "execute" data, i.e., things generated with
DC's or DS's. It is also possible to load or store from the
memory locations that comprise instructions. Unless, you
really know what you are doing, neither is likely to give
meaningful results.
(For example, if we left out the BR 14, upon storing
register one into C, the computer would attempt to execute
the bit pattern for the integer three as if it were an
instruction. It would probably get an "operation exception"
as this doesn't correspond to a legal IBM 360 architecture
instruction.)
The entries in the LOC column give the address from the
beginning of the program for each instruction. In our
example, the instruction BALR 12,0 would be at location
zero. The hex for that instruction would be 05C0.
At instruction 2, is the hex for the instruction L 1,A. The
bit pattern for that instruction would be 5810 C00E. Note
that this instruction takes four bytes instead of two like
the BALR 12,0.
The BALR 12,0 is an RR instruction (register-register),
while the L 1,A is an RX instruction (register-storage).
Not only do RX instructions take more memory--in most
architectures, they take more time to execute.
Likewise, A is located at memory location ten, B at 14 and C
at 18.
Remember that all the entries in the LOC column are HEX
numbers. In fact, all numbers to the left of the STMT
column are in HEX!
Your program is loaded at location 20,000 hex. This will
affect you when you debug your programs--more about this
later. The "BALR 12,0" and USING are part of the mechanism
that the IBM computer uses to allow your program to be put
in any location. The program would run just as well if it
were put at hex location 100, hex location 1000 or hex
location 40000. We call this "location independence."
The program executes as follows. The "L 1,A" copies the 3
from the box called "A" to register one. "A 1,B" adds the
contents of B, 4, to register one. Now, register one is
seven. Then the "ST 1,C" copies the 7 from register one to
the box called C.