Laboratory Assignments of System Software

Asignment 1
  1. Write a C program that reads text from a file and prints on the terminal each input line, preceded by the line number. The output will look like -
    1     This is the first trial line in the file,
    2     and this is the second line.
    Try the problem once using fgetc() function and once using fgets() function for reading the input. Why is fread() not suitable for this purpose?

    Do not ignore the value returned by the functions fgetc() and fgets(). After this exercise the you should be comfortable with the formatted input and output functions of C.

  2. Write a program that takes from the user the name of a file and a "field-number", and then reads that file and for each line in that file prints on the terminal word at position "field-number". For example if there are the following lines in the specified file -
    C is a programming language.
    lex produces a lexical analyser

          cc is a compiler
    and if the field-number specified is 4, then the output of the program is -
    programming
    lexical
    (NULL)
    compiler

    After this exercise you should be able to deal with individual words in a text file.

  3. Write a C program that reads the names (as character strings of length upto 20 bytes) and corresponsing roll-numbers (as integers) of 10 students from the user and stores them in a file whose name is specified by the user-
    After running the program check the size of the file created using ls -l command. Also see the content of the binary file using the command
    od -c filename.

    After this exercise you should be clear about the difference between a text file and a binary file.

  4. Write a C program that works like the od program of LINUX (UNIX).

  5. Write a C program that works like the strings program of LINUX (UNIX).
[To be completed in 1 week]

A Sample Assembly Language

Asignment 2

Write a lexical analyser for the assembly language described above. The program should take as input a text file containing an assembly program and print the stream of token values corresponding to the items in the input file. For identifiers and numbers the actual input item should also appear within brackets along with the token value. e.g. the output may look like:

17 20(100) 21(AGAIN) 14 20(3) 21(TERM) ....

Write the program once in plain C, and once using flex utility.

[Create the file token.h to put the token definitions. In the plain C implementation, you should also define the mnemonic table containing the mnemonic strings and the corresponding numeric token values.]

[To be completed in 1 week]

Asignment 3

For the assembly language discussed in the theory class, write a program that reads a file containing an assembly language program, and for each statement in the assembly program it prints the numeric opcode on the terminal in readable format, and in an output file in binary format. Also it should build the symbol table that contains each symbol encountered in the assembly program along with the line number in which that symbol is defined. After analysing all the lines and printing the opcodes, the symbol table should be printed on the terminal.
[You will have to define a mnemonic-table for this program]

[To be completed in 1 week]

Asignment 4

For the assembly language discussed in the theory class, write a program that reads a file containing an assembly language program, and prints the message "CORRECT" if the program is (syntactically) correct and "ERROR n" if the program is correct only upto line number n-1 (i.e., if there is an error in line number n).
[Use a Finite State Machine for this exercise]

[To be completed in 1 week]

Asignment 5

For the assembly language discussed in the theory class, create a lexical analyser using flex utility of Linux. Use this lexical analyser in a program that detects the following errors along with corresponding line numbers in input assembly language programs-

  1. Illegal characters in the program, eg. $, ^, etc.
  2. Syntax errors
  3. Use of undefined symbols
[For the lexical analyser part, first identify the tokens to be recognized, and then write the regular expressions for each. Read man pages of flex to find out details of using it.]
[To be completed in 2 week]

Asignment 6

Assume that in the assembly language discussed in the theory class, there is a statement -

DEFINE name replace-pattern
The meaning of the statement is that afterwards in the program wherever the pattern name occurs, it will be replaced by replace-pattern. For example if there is the statement DEFINE TRUE 1, then everywhere in the subsequent part in the program if there is the word TRUE, it will be replaced by 1. An input program may contain several such define statements. Write a program to implement this feature.

[This is a very simple macro pre-processor]

[To be completed in 1 week]

Asignment 7

For the assembly language discussed in the theory class, write a program that reads a file containing an assembly language program, processes the different types of statements (imperative statements, declarative statments and assembler directives) appropriately, and produces a relocation table for the program. The program should write this table in a file as binary data (i.e., non-ASCII format) as well as print this table on the terminal in readable format.

[Appropriate location counter processing is very important in this exercise. The size of each instruction is to be maintained in the mnemonic opcode table. The ideas/routines developed for the previous assignments may be used in this assignment]

[To be completed in 2 week]