Examples of programs

Back to:
index

The parallelepiped surface

Let's calculate full surface of the parallelepiped with ribs A, B and C, using "E97". A, B and C values is taken from memory cells.

The parallelepiped surface formula is the following:
S = 2 * (A * B + A * C + B * C) = 2 * [A * (B + C) + B * C]
Let's program it in "E97" codes:

AddressCode OperationComments
000001E0 (22) ==> R0 B ==> R0
00020022
00040101 R0 ==> R1 B ==> R1
000602E0 R0 + (24) ==> R0 B + C
00080024
000A05E0 R0 * (20) ==> R0 A * B (B + C)
000C0020
000E05E1 R1 * (24) ==> R1 B * C
00100024
00120210 R0 + R1 ==> R0 A * (B+C) + B * C
00140200 R0 + R0 ==> R0 2 * [A * (B + C) + B * C]
0016010E R0 ==> (26) result ==> S
00180026
001A0F00 halt
-------- -------------------------------------------
00200002 A
00220003 B
00240004 C
00260034 S (note, that hex number 34 is equal to decimal 52)
Back to:
index top


Latin letter input

The program gets latin letter and makes it upper-case. The upper- and lower-case characters differ in one bit only. For example, ASCII codes for letter R, are the following:
R - 0101 0010; r - 0111 0010.
To reset the 5-th bit, logical operation AND with constant 1101 1111 = DF can be used: if constant bit is zero, the result bit will be zero too, otherwise it repeats the congruent character's bit.

AddressCode OperationComments
00000E6D 30 ==> SP set SP for correct
00020030 subroutine calls
00049C0D call subroutine symbol input
000640FE 40FE (without echo!)
00080101 R0 ==> R1 save input character
000A07D0 DF and R0 ==> R0 make upper-case character
000C00DF
000E9C0D call subroutine result's output
00104088 4088
00120F00 halt

To convert input character to lower-case, it's enough to change the logic operation AND to OR:
000A08D0
000C 0020
Back to:
index top


Find maximum

Arbitrary integer numbers are stored in registers R1, R2 and R3. Find maximum of them.

At first we'll copy maximum of R1 and R2 to R0. Then, if R3>R0, it's necessary to replace R0 value by R3.
AddressCode OperationComments
00000412 compare R2 and R1
00023D04 if <0, then pc=pc+4to 0008
00040120 R2 ==> R0 remember R2
00061D02 pc=pc+2 to 000A - bypass second branch
00080110 R1 ==> R0 remember R1
000A0403 compare R3 with R0
000C3D02 if <0, then pc=pc+2to 0010
000E0130 R3 ==> R0 remember R3
00100F00 halt

We can rewrite our program with jump instructions: it will become more demonstrative, but longer. It also will lose relocation properties - we will not be able to move it to anoother address without changes.
AddressCode OperationComments
00000412 compare R2 and R1
00023C0D if <0, then to 000C
0004000C
00060120 R2 ==> R0 remember R2
00081C0D jump to 000E bypass second branch
000A000E
000C0110 R1 ==> R0 remember R1
000E0403 compare R3 with R0
00103C0D if <0, then to 0016
00120016
00140130 R3 ==> R0 remember R3
00160F00 halt
Back to:
index top


Serial natural numbers sum

Calculate the sum of first 100 natural numbers.

Let's place current natural number N to R3 (N varies from 1 to 100), and the sum result to R0. After assignment of initial values, we'll increment N and add it's value to S. The condition of cycle ending is N>100.
AddressCode OperationComments
00002113 1 ==> R3 1 ==> N
00022100 0 ==> R0 0 ==> S
00040230 R0 + R3 ==> R0 S = S + N
00062213 R3 + 1 ==> R3 N = N + 1
000804D3 compare R3 with 100 compare N with 100
000A0064
000C6DF6 if <=0, then pc=pc+F6if N<=100, repeat cycle (000E + FFF6 = 0004)
000E0F00 halt
Back to:
index top


Print latin alphabet

Form latin alphabet from "A" to "Z" on display screen..

The initial symbol "A" has code 65=41h. So we put it into R0 and call ROM subroutine, that outputs character. The next letter can be get after increment of R0. If the result exceeds 5Ah ("Z"), the cycle ends.
AddressCode OperationComments
00000E6D 30 ==> SP set SP for correct
00020030 subroutine calls
000401D0 41 ==> R0 first symbol code
00060041 ("A")
00089C0D call subroutine output to screen
000A4088 4088
000C2210 R0 + 1 ==> R0 next symbol
000E04D0 compare R0 with 5Acode <= "Z"?
0010005A ("Z")
00126DF4 if <=0, then pc=pc+F4 repeat cycle (to 0008)
00140F00 halt
Back to:
index top


Copy data array

Copy data, allocated from address, stored in R1, to address, placed in R2. The number of bytes is set in R0.

Because we consider R0 - R2 were set, our program is very short. It's main command C156 works with bytes and copies (R1)b into (R2)b. Then addresses in R1 and R2 are modified, and cycle counter decrements. The cycle repeats until R0<>0.
AddressCode OperationComments
0000C156 (R1)b ==> (R2)b copy current byte
00022211 R1 + 1 ==> R1get new sourse-array address
00042211 R2 + 1 ==> R2get new destination-array address
00062310 R0 - 1 ==> R0 decrement byte counter
00084DF6 if <>0, then pc=pc+F6repeat the cycle (to 0000)
000A0F00 halt
Back to:
index top


How many times this symbol is met in the text

Some text, terminated by zero byte, is allocated in memory from 0016 address. How many times a character from R0 meets in this text?

Let's place the conjunction counter to R1, and current address of the text to R2. The algorithm compares each symbol of the text with pattern and increments R1 if they are equal. The cycle ends, when address in R2 points to zero byte.
AddressCode OperationComments
00002101 0 ==> R1 clear conjunction counter
000201D2 16 ==> R2 set the beginning
00040016 of the text
0006C460 compare R0b with (R2)b compare symbol with pattern
00084D02 if <>0, then pc=pc+2 bypass if not equal (to 000C)
000A2211 R1 + 1 ==> R1 increment the conjunction counter
000C2212 R2 + 1 ==> R2calculate the next address
000EE406 compare (R2)b with 0b is text finished?
00104DF4 if <>0, then pc=pc+F4 if not - repeat the cycle (to 0006)
00120F00 halt
00140000
00164854 "TH"text
00185349 "IS""THIS IS MY TEXT"+
001A4920 " I"+code 0
001C2053 "S "
001E594D "MY"
00205420 " T"
00225845 "EX"
00240054 "T"(0)
Back to:
index top