This semester, we have a quite unique Lab at CEID about the architecture of a computer. What we do is building the assembly commands of a system using 1s and 0s. You know, in order to make a computer first you take the hardware, then you make the assembly commands and then the high level programming languages.
In the last exercise before the final exams, we were asked to construct some commands that will help us find the maximum number in a sequence of memory blocks. The numbers are all natural. So, let me show you the commands we had in our toolbox.
LDA $K: Load the content of address K to the register Accumulator (an arbitrary register)
STA $K: Store the content of Accumulator to address K.
MVI R,#K: Move the number K to register R.
MOV R1,R2: Copy the content of register R2 to register R1.
MOV R,M: Load the content of the address M to register R.
MOV M,R: Store the content of the register R to the memory’s address that is equal to M.
INR R: Add 1 to register R
INX RP: Add 1 to the registers R and P pair.
DCR R: Subtract 1 from register R.
DCX RP: Subtract 1 from the registers R and P pair.
CMP R: Compare register Accumulator with register R.
CMP M: Compare register Accumulator with the content of the address M.
CPI #K: Compare register Accumulator with the number K.
JZ $K: If zero flag equals 1 go to K.
JNZ $K: If zero flag equals 0 go to K.
JC $K: If carry flag equals 1 go to K.
JNC $K: If carry flag equals 0 go to K.
We were given two numbers, X and Y, which represent where in the main memory is the first and last number of our data, as well as the rest of the numbers between them.
Well, how do we find the maximum number? Obviously, we have to use the compare and conditional branch commands but there is a problem. When we want to compare two numbers, a and b, we subtract them and then we observe if the result is positive negative or zero. Then we use this information to satisfy a condition, like an [if(…),go to] conditional statement. Basic programming ideas. Alright, can you see the problem here? We don’t have have any [if(…),go to] for negative results. Although, the system supports it, we must make our program using only these commands.So, we must exploit the carry (=1 when the result is greater than 256) and zero (=1 when the result equals zero) flags.
Here’s my idea. Let’s say we have two numbers, a and b. Using the compare command would be useful only if a=b. Then, all we have to do is go for the next number in our list, because we are just searching for the maximum of them. What if a is not equal to b? How can we figure this out using the carry or the zero flag? Well, take the first number and start decreasing it using the DCR command and after every decrease use the compare command until one of the following happens. If CMP gives a zero flag after some using of DCR then a>b. Now, if DCR gives a zero flag then a