ICS312 - Spring 2009 - Homework #6 -

Work alone
You are expected to do your own work on all homework assignments. You may (and are encouraged to) engage in general discussions with your classmates regarding the assignments, but specific details of a solution, including the solution itself, must always be your own work. (See the statement of Academic Dishonesty on the course's syllabus.)

What to turn in?
Turn it you NASM file(s), driver.c, asm_io.inc, and asm_io.asm, along with a makefile that compiles all your executables as part of an archive via a single e-mail to henric@hawaii.edu with a subject line like "ICS312: HW#6" before midnight on the day the assignment is due.


Exercise #1: Binary Representation of a Number(15 pts)

Write an assembly program called hw6_ex1, stored in file hw6_ex1.asm. This program should prompt the user to enter a signed 32-bit integer. The program should print out the binary representation of the integer.

Here is an example interaction with the program:

% ./hw6_ex1
Enter an integer: 324
The binary representation is: 00000000000000000000000101000100
% ./hw6_ex1
Enter an integer: -13523
The binary representation is: 11111111111111111100101100101101
You can assume that the user always enters a number (and not random input).

Exercise #2: Finding a Simple Motif (30 pts)

Extend the program from the previous question so that it prints out the number of times the motif "1101" occurs in the binary representation of the entered number. For instance, the number "1101101001101" would contain the motif 3 times. Note that the occurrences of the motif can overlap. Motif recognition and identification is a very important problem in many areas (computer vision, bioinformatics, automated learning, searching), and this is a very, very simple example of it.

Here is an example interaction with the program:

% ./hw6_ex2
Enter an integer: 4321
The binary representation is: 00000000000000000001000011100001
The number of '1101' motifs is: 0
% ./hw6_ex2
Enter an integer: 123041231
The binary representation is: 00000111010101010111010111001111
The number of '1101' motifs is: 2
% ./hw6_ex2
Enter an integer: 14540253
The binary representation is: 00000000110111011101110111011101
The number of '1101' motifs is: 6
Again, you can assume that the user always enters a valid integer.

Exercise #3: Finding an Arbitrary Motif (15pts)

Modify your program from the previous question so that the program looks for an arbitrary motif. The user is prompted for this motif after the binary representation of the integer is printed. Here in an example interaction with the program:

% ./hw6_ex3
Enter an integer: 4123
The binary representation is: 00000000000000000001000000011011
Enter a binary motif: 11
The number of motifs is: 2
% ./hw6_ex3
Enter an integer: 512512
The binary representation is: 00000000000001111101001000000000
Enter a binary motif: 0
The number of motifs is: 25
% ./hw6_ex3
Enter an integer: -1251234
The binary representation is: 11111111111011001110100001011110
Enter a binary motif: 01
The number of motifs is: 5
As usual you can assume the user enters correct input.

WARNING:Recall that there is one annoying difficulty with using read_char (meaning that sometimes we have to call it twice because of the carriage return, see homework #4). The problem is really with read_int. read_int does not consume the "line feed" character that corresponds to the user typing "enter" after entering the integer. For this question we need to read an arbitrary number of characters because the user can enter a small motif ("01") or a big motif ("01101010101"). So you have to call read_char in a loop, as expected. You need to exit the loop when the character read is the one with ASCII code 10 (in decimal), i.e., when the user has typed enter. The problem is that since there is a left over line feed character (from read_int) your loop may exit right away.

Solution: To avoid all problems place a bogus call to read_char after calling read_int!

Exercise #4: Finding an Arbitrary Motif with wildcard [EXTRA CREDIT: 10pts)

Modify your program from the previous question so that use may enter a motif with wildcards. For instance the motif "01**10" means that two of the bits do not matter. So this motif could for instance match "011110" or "010110". Of course the program should still work if the motif contains no wildcard.

% ./hw6_ex4
Enter an integer: 4123
The binary representation is: 00000000000000000001000000011011
Enter a binary motif: 1*0
The number of motifs is: 2
% ./hw6_ex3
Enter an integer: 512512
The binary representation is: 00000000000001111101001000000000
Enter a binary motif: 1**1
The number of motifs is: 4
% ./hw6_ex3
Enter an integer: -1251234
The binary representation is: 11111111111011001110100001011110
Enter a binary motif: 0*11
The number of motifs is: 3
As usual you can assume the user enters correct input.
henric@hawaii.edu