Module 2 Additional Programs
Module 2 Additional Programs
MODULE 2
2. String Search Program
Given two strings of ASCII-encoded characters, a long string T and a short string P, we want to determine if the
pattern P is contained in the target T. Since P may be found in T in several places, we will simplify our task by
being interested only in the first occurrence of P in T when T is searched from left to right. Let T and P consist of
n and m characters, respectively, where n > m. The characters are stored in memory in consecutive byte
locations. Assume that the required data are located as follows:
• T is the address of T(0), which is the first character in string T.
• N is the address of a 32-bit word that contains the value n.
• P is the address of P(0), which is the first character in string P.
• M is the address of a 32-bit word that contains the value m.
• RESULT is the address of a word in which the result of the search is to be stored. If the substring P is found
in T, then the address of the corresponding location in T will be stored in RESULT; otherwise, the value −1
will be stored.
Note: Each character in T and P is stored in different memory block
Solved Problems
Problem 1: Assume that there is a string of ASCII-encoded characters stored in memory starting at address
STRING. The string ends with the Carriage Return (CR) character. Write a RISC-style program to determine the
length of the string and store it in location LENGTH.
Solution: The characters in the string are compared to CR (ASCII code 0x0D), and a counter is incremented until
the end of the string is reached.
Problem 2: We want to find the smallest
number in a list of 32-bit positive integers.
The word at address 1000 is to hold the value
of the smallest number after it has been
found. The next word contains the number of
entries, n, in the list. The following n words
contain the numbers in the list. The program
is to start at address 400. Write a RISC-style
program to find the smallest number and
include the assembler directives needed to
organize the program and data as specified.
While the program has to be able to handle
lists of different lengths, include in your code
a small list of sample data comprising seven
integers.
Solution: Comments in the program explain
how this task is performed.
Problem 3: Write a RISC-style program that
converts an n-digit decimal integer into a binary
number. The decimal number is given as n
ASCII-encoded characters, as would be the case
if the number is entered by typing it on a
keyboard. Memory location N contains n, the
ASCII string starts at DECIMAL, and the
converted number is stored at BINARY.
Solution: Consider a four-digit decimal
number, D = d3d2d1d0. The value of this
number is((d3 × 10 + d2) × 10 + d1) × 10 + d0.
This representation of the number is the basis
for the conversion technique used in the
program in Figure 2.35. Note that eachASCII-
encoded character is converted into a Binary
Coded Decimal (BCD) digit before it is used in
the computation. It is assumed that the
converted value can be represented in no more
than 32 bit.
Problem 4: Consider an array of numbers A(i,j), where
i = 0 through n − 1 is the row index, and j = 0 through
m − 1 is the column index. The array is stored in the
memory of a computer one row after another, with
elements of each row occupying m successive word
locations. Assume that the memory is byte-addressable
and that the word length is 32 bits. Write a RISC-style
subroutine for adding column x to column y, element by
element, leaving the sum elements in column y. The
indices x and y are passed to the subroutine in registers
R2 and R3. The parameters n and m are passed to the
subroutine in registers R4 and R5, and the address of
element A(0,0) is passed in register R6.