Question: We will program in a _restricted_ high-level language to deal with some of the issues of programming in assembly language. In particular, we will emulate
We will program in a _restricted_ high-level language to deal with some of the issues of programming in assembly language. In particular, we will emulate a _strict_ load-store architecture. We want to detect _all_ occurrences of a particular string, called the 'pattern', in a longer string, called the 'target'. In the HLL, the pattern is indexed from 0 to 'm', while the target is indexed from 0 to 'n'. HLL indices correspond to assembly offsets. In the simplest iterative algorithm, 'i' is the current guess of a target index at which the pattern might begin. 'j' indexes the pattern, and 'k' indexes the target. We compare characters, one by one, as long as the characters match. If they do and the pattern is exhausted, we have found a match. However, if they do and the target is exhausted, we must be careful not to read irrelevant data past the target from memory. while target not exhausted do set i, j, and k while characters match do if pattern exhausted then record match leave inner loop fi if target exhausted then leave inner loop fi increment j and k od od The restrictions are: 1) As far as possible, calculate using registers. Register names must end in "_reg" (e.g., 'x_reg'). 2) Memory may be accessed only by separate load and store statements. The required syntax is one of: 'x_reg = array_name[y_reg]; // load' The comments are required. 'array_name[x_reg] = y_reg; // store' 'x_reg = scalar_name; // load' 'scalar_name = x_reg; // store' 3) All loops must be specified with either 'while( x_reg == y_reg ) {...}' or 'while( x_reg /= y_reg ) {...}'. 4) The only 'if' statement is 'if( x_reg == y_reg ) {...}', where the body may contain a 'break' statement. No 'return' statements are allowed. 5) The _only_ control constructs are: assignment, 'while', 'if', and 'break'. 6) Register initialization and arithmetic are allowed. Your mandatory test program begins: #include int main () { // declaration and initialization of memory variables and // constants ** This section must appear verbatim. ** char pattern[] = "abc"; // non-null pattern int m = 2; // highest pattern index char target[] = "aabcdababcxyabceabc"; // non-null target int n = 17; // highest target index int stack[5]; // match positions (indices) int top = -1; // restricted code ... //unrestricted code (recommended style) if( top == -1 ) { // no matches were found std::cout << "The pattern was not found. "; } else { // at least one match std::cout << "The pattern was found " << top + 1 << " time(s) "; std::cout << "at position(s): "; for( int j = 0; j < top + 1; j++ ) { std::cout << stack[j] << " "; } std::cout << " "; } } /* The pattern was found ... time(s) at position(s): ... */ Concatenate your output to your source code as a comment. In that way, your e-submission will be an executable program. Since you are forbidden to use fancy HLL features, your choice of HLL is secondary. Just remember there are few polyglots around. Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
