Week 12 Tutorial Questions

Objectives

Tutorial Questions

Revision

  1. Write a MIPS program that adds two numbers (read value from user) and prints the result.

    Your program should:

    • read two integers from the user
    • Add the integers
    • Print the result using a syscall
    • Exit the program
      int main() 
      {   
        int x;
        int y;
        scanf("%d", &x);
        scanf("%d", &y);
        int result = x + y;
        printf("%d", result);
      }
      
  2. Implement the following function in MIPS

    int everyKth(int *src, int n, int k, int *dest) 
    {   
       int i, j = 0;
       for (i = 0; i < n; i++) {
          if (i%k == 0) {
             dest[j] = src[i];
             j++;
          }
       }
       return j;
    }
    

  3. Write a MIPS program that returns the number of negative numbers in an array.

    You should write your program using the following steps

    • Define an array of 5 integers in the .data section (you may use the example below).
    • Loop through the array in your program.
    • In each iteration of the loop use an iteration constant to go through the array
    • Use lw to load word and check if the value is negative.

    Example array:

    .data
    array: .word 4, 8, -3, 10, 2
    
  4. Write a C program that counts how many consecutive 0 bits appear at the least significant end of a given 32-bit integer.

    For example:

    Input: 0x80      → Binary: 00000000 00000000 00000000 10000000 → Output: 7
    Input: 0x0C      → Binary: 00000000 00000000 00000000 00001100 → Output: 2
    Input: 0x01      → Binary: 00000000 00000000 00000000 00000001 → Output: 0
    

    Your program should:

    • Define a function int count_trailing_zeros(uint32_t x);
    • Use bit shifting to examine each bit from least significant to most significant.
    • Stop counting when the first 1 is found.

    Test your function with multiple values in the main function.

  5. Write a C program that packs two characters into a single 32-bit/16-bit integer.

    Your program should:

    • Take two char values (e.g. 'A' and 'Z')
    • Pack them into an int such that:
      • The first character goes into the upper byte (bits 8–15)
      • The second character goes into the lower byte (bits 0–7)
    • Print the result as a hexadecimal number (e.g. 0x415A)

    Example:

       Input: 'A', 'Z'
       Output: 0x415A
       

    Hint: Use bitwise shift << and bitwise OR |.

  6. Write a C program that opens a text file and counts how many times a specific character appears in it.

    Your program should:

    • Take two command-line arguments:
      • The name of the file to open
      • The character to count
    • Open the file using fopen
    • Read the file character by character using fgetc
    • Count how many times the given character appears

    Example:

        ./count_char story.txt e
        The character 'e' appears 42 times in story.txt.
        

    Hint: Make sure to check that the second argument is exactly one character long.

  7. Write a C program that removes a specified line from a text file.

    Your program should:

    • Take two command-line arguments:
      • The name of the input file
      • The line number to delete(number starting from 1)
    • Open the file for reading, and a temporary file for writing
    • Copy all lines from the input file to the temporary file except the specified line
    • Replace the original file with the updated file

    Example usage:

       ./delete_line myfile.txt 3
       

    After running the above command, the third line of myfile.txt should be removed.

    Hint: Use fgets to read lines, rename to overwrite the original file, and atoi(argv[2]) to get the line number.

  8. Write a function called void print_utf8_encoding(uint32_t code_point);

    The function takes a unicode code point and prints out the code_point, the utf 8 encoding for the code point then the unicode character.

    For example, if we have a main program

       int main(void) {
           print_utf8_encoding(0x42);
           print_utf8_encoding(0x00A2);
           print_utf8_encoding(0x10be);
           print_utf8_encoding(0x1F600);
       }
       

    We would end up with output

       U+42  UTF-8: 0x42  B
       U+a2  UTF-8: 0xc2 0xa2  ¢
       U+10be  UTF-8: 0xe1 0x82 0xbe  Ⴞ
       U+1f600  UTF-8: 0xf0 0x9f 0x98 0x80  😀
       
  9. Write a (silly) program that creates 20 nested directories. So by the end you should have a directory called d19 with a relative path of d0/d1/d2/d3/d4/d5/d6/d7/d8/d9/d10/d11/d12/d13/d14/d15/d16/d17/d18/d19
  10. Write a program to list the contents of directories

    You should use opendir(3) in your answer and use perror to print error messages for example if the directory does not exist or if you do not have permission to list its contents.

    For example:

    ./list_directory .
    .
    ..
    dir3
    list_directory.c
    list_directory
    dir2
    dir1
    ./list_directory dir1 dir2 dir3
    .
    ..
    f1
    .
    ..
    f2
    .
    ..
    f3
    f4