View on GitHub

Computer Architecture and Operating Systems

Course taught at Faculty of Computer Science of Higher School of Economics

Lecture 3

Computer Architecture and Assembly Language

Lecture

Slides (PDF, PPTX).

Outline

Workshop

Outline

Hint: Use the RARS help system (F1) and RISC-V Greencard to quickly find the needed instructions.

Tasks

  1. Open in RARS and run the “Hello World” program. Get familiar with its structure. Pay attention to the .text and .data segments, labels, and system calls.

  2. Open in RARS and run the “Add” program. This is an example of a program that inputs two numbers, adds them, and prints the result. It can be used a basis for other programs.

  3. Write a program that inputs integer value x and prints it in the following formats: decimal, unsigned, hexadecimal, and binary. Hint: Use RARS help (F1) to find proper system calls.

  4. Write a program that inputs two integer values x and y, calculates the result of the following expressions, and prints the result.

    x + 5 - y 
    (x - y) << 3
    (x + y) >> 2
    (x + 5) + (x - 7)
    x >> 3 + y << 3
    
  5. Write a program that inputs integer value x, performs logic and arithmetical shifts to the left and to the right by 3 digits, and prints the result (decimal and binary format).

  6. Write a program that inputs integer value x, performs the following operations, and prints the result. Use the binary format for printing.

    x * 2
    x * 3
    x * 4
    x * 5
    x * 8
    x * 31
    

    How to perform these operations without using a multiplication instruction?

    x / 2
    x / 3
    x / 5
    x / 8
    x / 31
    

    Is it possible to do this without using a division instruction?

    x % 2
    x % 3
    x % 5
    x % 8
    x % 31
    

    Is it possible to do this without using a remainder instruction?

  7. Write a program that inputs integer value x, sets its 3-rd bit, resets its 6-th bit, and prints the result. Use the binary format for printing.

  8. Write a program that inputs two integer values x and y and perform their signed and unsigned comparison (using the slt and sltu instructions).

  9. Write a program that inputs two integer values x and y, swaps them with the XOR operation, and outputs them back.

  10. Modify the add program to check whether an integer overflow has occurred in the addition instruction. Print 0 if no, and 1 if yes.

  11. Write a program that performs the following bit tricks:

    • x & (x - 1) - turning off the rightmost 1-bit (e.g. 01011000 => 01010000).
    • x | (x + 1) - turning on the rightmost 0-bit (e.g. 10100111 => 10101111).
    • x | (x - 1) - turning on the trailing 0’s (e.g. 10101000 => 10101111).

    Print the input and output values in the binary format.

Notes

What is an integer overflow? This is a situation when an arithmetic operation produces a value that is outside the range that can be represented with a given number of bits. This value is truncated, which gives a mathematically incorrect result.

Let us consider how such a situation can happen when we add two 4-bit values.

  1. The values are unsigned.

    Here is the simplest example of the overflow situation:

    1111(15) + 0001(1) = 10000(16) -> to 4 bits -> 0000(0) == OVERFLOW 
    

    The result of this addition is a 5-bit value. When we truncate it to 4 bits, we get 0000 (0). The 5-th bit of the result (1) is truncated. The result is mathematically incorrect.

    How can we detect this? Rule: if the sum is smaller than any of the values, this is an overflow.

  2. The values are signed (2’s complement).

    This case is more complicated. Here are examples of 4 possible situations:

    1111(-1) + 0001 (1) = 10000 -> to 4 bits -> 0000 (0) == OK
    1111(-1) + 1111(-1) = 11110 -> to 4 bits -> 1110(-2) == OK
    1000(-8) + 1111(-1) = 10111 -> to 4 bits -> 0111 (7) == OVERFLOW
    0111 (7) + 0001 (1) =                       1000(-8) == OVERFLOW
    

    What rule can we draw from these examples? Rule: if both values have the same sign (their most significant bits are equal) and it is different from the sign of the result, this is an overflow.

How to find out that two values are equal? Use the XOR operation. For equal values, its result will be 0.

Homework

Write programs that input two integer values x and y, calculate the following expressions, and print the result:

  1. (x + 5) - (y - 7)
  2. (x >> 2) + ((y - 1) << 3) (>> - logical shift)
  3. (x << y) - 10
  4. (x >> y) + 10 (>> - arithmetical shift)
  5. ((x << 2) - y + 5) >> 1 (>> - arithmetical shift)
  6. x * 6 - y * 3 (do multiplication using shifts, adds, and subs)
  7. 2 * x * x - 3 * y + 4
  8. (x + 5) / y + 10 / (y - 1)
  9. (x / y) * y + x % y
  10. x & (-1 << 5)
  11. x | (-1 >> 27) (>> - logical shift)
  12. set the y-th bit of x to 1 (bit numbers start from 0)
  13. reset the y-th bit of x to 0 (bit numbers start from 0)
  14. x > y ? 0 : 1
  15. (x == (y + 3)) ? 0 : 1
  16. x < -5 & y > 10

Commit the programs to your private GitHub account. Place it into the folder ca/lab03.

References