home *** CD-ROM | disk | FTP | other *** search
-
-
-
- ELEMENTARY COMPUTER PROGRAMMING
- Lesson 2
-
- Copyright 1990
- Castle Oaks Computer Services
- Post Office Box 36082
- Indianapolis, IN 46236-0082
-
- TRAC, the hypothetical computer introduced in lesson 1, can be
- simulated on an IBM PC compatible computer. (In fact, it can be
- simulated on any MS-DOS computer even if it is not fully IBM PC
- compatible.) Therefore, it is possible to execute TRAC programs
- in order to gain a familiarity with coding in machine language.
- In this lesson, the information needed to actually execute a TRAC
- program will be given.
-
- First, the problem must be coded according to the rules of the
- language and the format given in Lesson 1. After coding the
- problem on paper, then it must be entered into a disk file on the
- computer. This package does not provide the software necessary
- to do this, but you may use any suitable text editor or word
- processor in non-document mode. At Castle Oaks we usually use
- WORDSTAR (TM MicroPro International Corporation) in non-document
- mode, but there are several shareware text editors that would
- also be adequate. Any file name can be used for your file, but
- we recommend that the name have an extension of 'OBJ'. The files
- that you run with TRAC are called 'object' files and therefore it
- is desirable to identify them as such. In the following lessons
- we will discuss another type of file.
-
- The general command line format for running TRAC is:
-
- TRAC [object file] [trace file]
-
- The parameters in brackets are optional. If the first parameter
- is omitted, the program will prompt you for a file descriptor of
- the object file to be run. This file descriptor may include a
- drive letter and subdirectories, if required. If the second
- parameter is also omitted from the command line, the program will
- ask if you want the program traced; and, if so, will ask if you
- want it traced on the screen or to a file. If to a file, you
- will also be prompted for a file name. If the object file and
- the trace file are both given on the command line, the program
- will start to execute without any queries.
-
- The purpose of tracing a program is to trouble shoot it in case
- it does not function correctly. It is a good idea to trace the
- exercises so you can analyze what is happening at each step in
- order to gain a fuller understanding of the process. When trac-
- ing a program, each instruction is displayed in the following
- format:
-
-
-
-
-
-
- II-1
-
-
- LLLL T OO AAAA SVVVVVVVVV SAAAAAAAAA
-
- Where LLLL is the location of the instruction;
-
- T is the TAG;
-
- OO is the operation code;
-
- AAAA is the effective address of the operand;
-
- SVVVVVVVVV is the value of the operand; and
-
- SAAAAAAAAA is the current value in the accumulator.
-
- Note that the resulting value of an operation does not appear in
- the accumulator until the following line is printed. If you
- trace your program on the screen, it may print too fast for you
- to analyze. You may pause the display at any time by pressing ^S
- (control-S) or you may send the screen display to the printer by
- pressing ^P (control-P) before starting your program. If you
- send it to a file, later you may either view it on the screen or
- you can send it to the printer. It is suggested that when you
- name a trace file that you give it an extension of 'LST' which
- will help you identify it as a file to list.
-
- Figure II-1 is the source code of a simple program to illustrate
- the execution and tracing of a program. This program reads in
- two numbers, finds their sum and difference and prints out the
- numbers, and the results to the screen.
-
- -----------------------------------------------------------------
- 0010 400100 Read X, Y into 100 and 101
- 0011 000100 Load X
- 0012 010101 Add Y
- 0013 030102 Store sum at 102
- 0014 410100 Print 100, 101, 102, (also 103 and 104)
- 0015 000100 Load X
- 0016 020101 Subtract Y
- 0017 030102 Store difference at 102
- 0018 410100 Print 100, 101, 102, (also 103 and 104)
- 0019 500000 Halt
- 9999 000010 Start program at 10
-
- Figure II-1
- -----------------------------------------------------------------
-
- Figure II-2 shows what appears on the screen when this program is
- executed.
-
-
-
-
-
-
-
-
-
- II-2
-
-
- -----------------------------------------------------------------
- A:\TRAC
- TRAining Computer (TRAC)
- Version 1.0
- Copyright 1990
- CASTLE OAKS COMPUTER SERVICES
- Post Office Box 36082
- Indianapolis, IN 46236-0082
-
- What is the name of your object code file?
- FIGII-1.OBJ
- Do you want to trace the program? (Y or N) Y
- Do you want to trace it on screen (S) or on file (F)? S
- 10 0 40 100 0 0
- ? 123456789 876543210
- 11 0 0 100 123456789 0
- 12 0 1 101 876543210 123456789
- 13 0 3 102 0 999999999
- 14 0 41 100 123456789 0
- 123456789 876543210 999999999 0 0
- 15 0 0 100 123456789 999999999
- 16 0 2 101 876543210 123456789
- 17 0 3 102 999999999 -753086421
- 18 0 41 100 123456789 -753086421
- 123456789 876543210 -753086421 0 0
- 19 0 50 0 0 -753086421
-
- Figure II-2
- -----------------------------------------------------------------
-
- If the program is run with the object file named on the command
- line and with no tracing, the resultant screen display will be as
- shown in Figure II-3.
-
- -----------------------------------------------------------------
- A:\TRAC FIGII-1.OBJ
- TRAining Computer (TRAC)
- Version 1.0
- Copyright 1990
- CASTLE OAKS COMPUTER SERVICES
- Post Office Box 36082
- Indianapolis, IN 46236-0082
-
- Do you want to trace the program? (Y or N) N
- ? 123456789 876543210
- 123456789 876543210 999999999 0 0
- 123456789 876543210 -753086421 0 0
-
- Figure II-3
- -----------------------------------------------------------------
-
- Note: Locations 103 and 104 print zero because zero was entered
- into them implicitly during the read. Note also that TRAC first
- clears all memory locations to zero before loading and executing
- the program. Do not count on other computers doing this.
-
-
- II-3
-
-
- Note that in Figure II-2 the trace printout and the program were
- both intermixed on the screen. This may be desirable or it may
- be undesirable. If you do not want them together, you can send
- the trace printout to a file and view it later; or you may send
- your program output to the printer while tracing on the screen.
-
- The remainder of this lesson will be devoted to showing some
- other examples. The coding and tracing of these examples also
- will be given.
-
- The following Figure gives a flow chart for finding the factorial
- of a number. A factorial is the product of all integers up to
- and including the given number. Thus factorial 3 is 1*2*3=6;
- factorial 4 is 1*2*3*4=24; etc.
-
- -----------------------------------------------------------------
- |
- v
- ---------
- / Read N /<------------------+
- --------- |
- | |
- v |
- / \ <= +------+ |
- <N:0>----->| STOP | |
- \ / +------+ |
- +------------------+ | > ^ |
- Note: N>12 is | v | |
- not allowed | / \ | |
- since the result | / \ >= | |
- would exceed the | - - <N:13 >---------+ |
- capacity of the | \ / |
- A register. | \./ |
- +------------------+ | < |
- v |
- +-------+ |
- | F = 1 | |
- | K = 2 | |
- +-------+ |
- | |
- v |
- +-------+ / \ > ------------ |
- | F=F*K |-><K:N>----->/ Print N,F /--+
- | K=K+1 | \ / ------------
- +-------+ |
- ^ | <=
- +--------+
-
- Figure II-4
- -----------------------------------------------------------------
-
- Figure II-5 gives the coding for the flow chart of Figure II-4.
-
- Note: In a flow chart, a diamond shaped box is used to indicate a
- decision point. An expression such as K:N in a diamond means to
- compare K and N (usually by subtraction) and then taking the
- appropriate path based on the symbols shown.
- II-4
-
-
- -----------------------------------------------------------------
- 0010 401901 Read value for N
- 0011 001901 Load N into A
- 0012 240016 If negative go to stop
- 0013 250016 If zero go to stop
- 0014 021801 Subtract 13
- 0015 240017 If negative skip around stop
- 0016 500000 Stop
- 0017 001802 Load 1
- 0018 031902 Store at F
- 0019 001803 Load 2
- 0020 031804 Store at K
- 0021 021901 Subtract N
- 0022 240026 Jump, if negative, to update F
- 0023 250026 Jump, if zero, to update F
- 0024 411901 Print
- 0025 260010 Branch back to beginning
- 0026 001902 Load F
- 0027 151804 Multiply by K
- 0028 031902 Store at F
- 0029 001804 Load K
- 0030 011802 Add 1
- 0031 031804 Store at K
- 0032 260021 Branch to test if done
- 1801 13 Constant 13
- 1802 1 Constant 1
- 1803 2 Constant 2
- 9999 000010 End of code
-
- Figure II-5
- -----------------------------------------------------------------
-
- There are a few features of this program that should be noted.
-
- Almost any location can be picked to receive the value of N. In
- this case, 1901 was chosen.
-
- Constants can go almost anyplace. In this case, 1801, 1802, and
- 1803 were used for constants.
-
- F could have been stored almost anyplace; but looking ahead, it
- was noted that it was to be printed along with N. Therefore, at
- print time they need to be in consecutive locations. Since N is
- already in 1901 and 1902 was not used, it is logical to assign
- 1902 for F.
-
- At location 0021 we have the beginning of a loop. Since location
- 0021 can be entered two ways, once from 0020 and later from 0032,
- we must be careful about what is in the accumulator at the begin-
- ning of this loop. Normally we would load the accumulator at the
- beginning of the loop. But by careful planning we see that we
- can have K already in the accumulator when we enter from either
- direction. This saves the load instruction.
-
- Figure II-6 shows what would appear on the screen if the program
- is traced and the values 3 and 13 are entered for N.
-
- II-5
-
-
- A:\TRAC FIGII-5
- TRAining Computer (TRAC)
- Version 1.0
- Copyright 1990
- CASTLE OAKS COMPUTER SERVICES
- Post Office Box 36082
- Indianapolis, IN 46236-0082
-
- Do you want to trace the program? (Y or N) y
- Do you want to trace it on screen (S) or on file (F)? s
- 10 0 40 1901 0 0
- ? 3
- 11 0 0 1901 3 1
- 12 0 24 16 500000 3
- 13 0 25 16 500000 3
- 14 0 2 1801 13 3
- 15 0 24 17 1802 -10
- 17 0 0 1802 1 -10
- 18 0 3 1902 2 1
- 19 0 0 1803 2 1
- 20 0 3 1804 3 2
- 21 0 2 1901 3 2
- 22 0 24 26 1902 -1
- 26 0 0 1902 1 -1
- 27 0 15 1804 2 1
- 28 0 3 1902 1 2
- 29 0 0 1804 2 2
- 30 0 1 1802 1 2
- 31 0 3 1804 2 3
- 32 0 26 21 21901 3
- 21 0 2 1901 3 3
- 22 0 24 26 1902 0
- 23 0 25 26 1902 0
- 26 0 0 1902 2 0
- 27 0 15 1804 3 2
- 28 0 3 1902 2 6
- 29 0 0 1804 3 6
- 30 0 1 1802 1 3
- 31 0 3 1804 3 4
- 32 0 26 21 21901 4
- 21 0 2 1901 3 4
- 22 0 24 26 1902 1
- 23 0 25 26 1902 1
- 24 0 41 1901 3 1
- 3 6 0 0 0
- 25 0 26 10 401901 1
- 10 0 40 1901 3 1
- ? 13
- 11 0 0 1901 13 1
- 12 0 24 16 500000 13
- 13 0 25 16 500000 13
- 14 0 2 1801 13 13
- 15 0 24 17 1802 0
- 16 0 50 0 0 0
-
- Figure II-6
-
- II-6
-
-
- Another simple problem is shown in Figure II-7. In this program
- we will raise a number, X, to any positive power, N. We cannot
- do a negative power since that would result in a fraction.
- Therefore, we will use a negative N to terminate execution of the
- program.
-
- -----------------------------------------------------------------
- |
- v
- -----------
- / Read N,X /<-----------------+
- ----------- |
- | |
- v |
- +-------+ |
- | F = 1 | |
- | K = N | |
- +-------+ |
- +------->| |
- | v |
- +-------+ > / \ = -------------- |
- | F=F*X |<-<K:0>--->/ Print N,X,F /--+
- | K=K-1 | \ / --------------
- +-------+ | <
- |
- v
- +------+
- | STOP |
- +------+
-
- Figure II-7
- -----------------------------------------------------------------
- The coding is shown in Figure II-8.
-
- -----------------------------------------------------------------
- 1001 400011 Read N, X into 11, 12
- 1002 001501 Load A with a 1
- 1003 030013 Store F at 13
- 1004 000011 Load N into A
- 1005 031502 Store at K
- 1006 241017 Jump to stop instruction
- 1007 251015 Jump to print
- 1008 000013 Load F
- 1009 150012 Multiply by X
- 1010 030013 Store at F
- 1011 001502 Load K
- 1012 021501 Subtract 1
- 1013 031502 Store at K
- 1014 261006 Go back to start of loop
- 1015 410011 Print N, X, F
- 1016 261001 Jump to beginning of program
- 1017 500000 Stop the program
- 1501 1 Constant 1
- 9999 001001 End of code
-
- Figure II-8
- -----------------------------------------------------------------
- II-7
-
-
- Figure II-9 shows some sample results of the program as they
- would appear on the screen.
-
- -----------------------------------------------------------------
- A:\>TRAC FIGII-8.OBJ
- TRAining Computer (TRAC)
- Version 1.0
- Copyright 1990
- CASTLE OAKS COMPUTER SERVICES
- Post Office Box 36082
- Indianapolis, IN 46236-0082
-
- Do you want to trace the program? (Y or N) n
- ? 10 2
- 10 2 1024 0 0
- ? 10 4
- 10 4 1048576 0 0
- ? 3 1234
- 3 1234 879080904 0 0
- ? -1
-
- A:\>
-
- Figure II-9
- -----------------------------------------------------------------
-
- In this program there is no simple way to determine that the
- answer will exceed the capacity of the A register. Therefore
- there is a possibility of having an overflow. That happened in
- the case of raising 1234 to the 3rd power. The correct answer is
- 1879080904.
-
- Each of the example programs are included in the package. The
- ones associated with this lesson are FIGII-1.OBJ, FIGII-5.OBJ,
- and FIGII-8.OBJ. You should run each of these and experiment
- with various inputs, tracing, etc. before trying one of your own
- programs.
-
- When you are comfortable with how to execute and trace a program
- then you might experiment with the above programs such as chang-
- ing from printing to the screen to printing to the printer. You
- should try executing your Exercise I-1 now. Then you should code
- and execute exercise II-1. The flow chart for this exercise
- appears on the next page.
-
- In this program, you are to enter 10 values (done in two reads
- and the second read should use locations that immediately follow
- those of the first read.) The program will do a "minimum-in-
- pass" sort and the sorted array is printed. It will be necessary
- to use two index registers.
-
-
-
-
-
-
-
- II-8
-
-
- |
- v
- --------------------------------
- / Read A(0),A(1),A(2),A(3),A(4) /
- --------------------------------
- |
- v
- --------------------------------
- / Read A(5),A(6),A(7),A(8),A(9) /
- --------------------------------
- |
- v
- +-----+
- | J=0 |
- +-----+
- |
- v
- +---------+
- | S=A(J) |<-------------+
- | I=J+1 | |
- +---------+ |
- | |
- v |
- / \ |
- / \ >= |
- +----><A(I) >------+ |
- | \ :S/ | |
- | \ / | |
- | | < | |
- | v | |
- | +---------+ | |
- | | T=S | | |
- | | S=A(I) | | | +-------+
- | | A(I)=T | | | | STOP |
- | +---------+ | | +-------+
- | | | | ^
- | v | | |
- | +-------+ | | -------------------------
- | | I=I+1 |<----+ | / Print /
- | +-------+ | /A(5),A(6),A(7),A(8),A(9)/
- | | | -------------------------
- | v | ^
- | / \ | |
- | < / \ | -------------------------
- +-----<I:10 > | / Print /
- \ / | /A(0),A(1),A(2),A(3),A(4)/
- \./ | -------------------------
- | >= | ^
- v < | |
- +-------+ +-------+ / \ >= |
- | A(J)=S|->| J=J+1 |-><J:9>-----------+
- +-------+ +-------+ \./
-
- Exercise II-1
-
-
-
- II-9
-
-
- Here (Figure II-10) is a method of multiplying two 9-digit numbers
- to obtain an 18-digit product as promised earlier. The factors are
- each divided into three 3-digit groups. Then individual products
- are found and summed with proper attention to overflow digits. See
- the comments for more detail. Try tracing this program (on disk as
- DOUBLMPY.OBJ) using the factors 999888777 and 666555444. These
- factors will help illustrate the isolation of the groups and how the
- product is reassembled. Note that if the lower part of the product
- has leading zeroes, TRAC does not print them. A shorter program for
- doing this multiply could probably be devised but this method was
- chosen because of its symmetry. If it is known that the product
- will contain fewer than 18 digits, the program could be modified so
- fewer steps would be required.
-
- 0010 401000 Read A and B 0050 151503 Times Z
- 0011 001000 Load A 0051 031507 V*Z --> T
- 0012 360006 Isolate high 3 0052 001506 Load W
- 0013 031501 Store as X 0053 151502 Times Y
- 0014 001000 Load A 0054 011507 Plus V*Z
- 0015 370003 Isolate 0055 011003 Plus LOWER
- 0016 360006 middle 3 0056 031003 V*Z+W*Y+???000
- 0017 031502 Store as Y 0057 001506 Load W
- 0018 001000 Load A 0058 151503 Times Z
- 0019 370006 Isolate 0059 031507 W*Z --> T
- 0020 360006 low 3 0060 360003 Isolate high 3
- 0021 031503 Store as Z 0061 011003 Plus LOWER
- 0022 001001 Load B 0062 031003 Save at LOWER
- 0023 360006 Isolate high 3 0063 360006 Isolate carry
- 0024 031504 Store as U 0064 031508 Save at CARRY
- 0025 001001 Load B 0065 001003 Load LOWER
- 0026 370003 Isolate 0066 370003 Shift off carry
- 0027 360006 middle 3 0067 031003 Save at LOWER
- 0028 031505 Store as V 0068 001507 Load W*Z
- 0029 001001 Load B 0069 370006 Isolate
- 0030 370006 Isolate 0070 360006 low 3
- 0031 360006 low 3 0071 011003 Plus LOWER
- 0032 031506 Store as W 0072 031003 Save at LOWER
- 0033 151501 Times X 0073 001504 Load U
- 0034 031507 X*W --> T 0074 151502 Times Y
- 0035 001505 Load V 0075 031507 U*Y --> T
- 0036 151502 Times Y 0076 001505 Load V
- 0037 011507 Plus X*W 0077 151501 Times X
- 0038 031507 X*W+V*Y --> T 0078 011507 Plus U*Y
- 0039 001504 Load U 0079 011002 Plus UPPER
- 0040 151503 Times Z 0080 011508 Plus CARRY
- 0041 011507 Plus X*W+V*Y 0081 031002 Save at UPPER
- 0042 031507 X*W+V*Y+U*Z --> T 0082 001504 Load U
- 0043 360003 Isolate high 3 or 4 0083 151501 Times X
- 0044 031002 Save in UPPER 0084 370003 Position U*X
- 0045 001507 Load X*W+V*Y+U*Z 0085 011002 Plus UPPER
- 0046 370006 Isolate 0086 031002 Save at UPPER
- 0047 360003 low 3 0087 411000 Print A,B,Product
- 0048 031003 Save in LOWER 0088 500000 Halt
- 0049 001505 Load V 9999 000010
-
- Figure II-10
-
- II-10