Lecture 3
Computer Architecture and Assembly Language
Lecture
Outline
- Computer organization.
- Processor.
- CPU performance.
- Instruction set architecture (ISA).
- RISC and RISC-V.
- Assembly language.
Workshop
Outline
- Getting familiar with the RARS simulator.
- Getting familiar with RISC-V registers.
- Getting familiar with RARS system calls to input/output integer values.
- PrintInt (number 1)
- PrintHex (number 34)
- PrintBinary (number 35)
- PrintIntUnsigned (number 36)
- ReadInt (number 5)
- Practicing with RISC-V arithmetic and bitwise instructions.
- Arithmetic:
add
,sub
,addi
- Bitwise:
and
,or
,xor
,andi
,ori
,xori
- Shifts:
sll
,srl
,sra
,slli
,srli
,srai
- Comparison:
slt
,sltu
,slti
,sltiu
- Multiplication:
mul
,rem
,div
,divu
,remu
- Arithmetic:
Hint: Use the RARS help system (F1) and RISC-V Greencard to quickly find the needed instructions.
Tasks
-
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. -
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.
-
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. -
Write a program that inputs two integer values
x
andy
, 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
-
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). -
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?
-
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. -
Write a program that inputs two integer values
x
andy
and perform their signed and unsigned comparison (using theslt
andsltu
instructions). -
Write a program that inputs two integer values
x
andy
, swaps them with the XOR operation, and outputs them back. -
Modify the add program to check whether an integer overflow has occurred in the addition instruction. Print
0
if no, and1
if yes. -
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.
-
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.
-
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:
(x + 5) - (y - 7)
(x >> 2) + ((y - 1) << 3)
(>>
- logical shift)(x << y) - 10
(x >> y) + 10
(>>
- arithmetical shift)((x << 2) - y + 5) >> 1
(>>
- arithmetical shift)x * 6 - y * 3
(do multiplication using shifts, adds, and subs)2 * x * x - 3 * y + 4
(x + 5) / y + 10 / (y - 1)
(x / y) * y + x % y
x & (-1 << 5)
x | (-1 >> 27)
(>>
- logical shift)- set the
y
-th bit ofx
to1
(bit numbers start from0
) - reset the
y
-th bit ofx
to0
(bit numbers start from0
) x > y ? 0 : 1
(x == (y + 3)) ? 0 : 1
x < -5 & y > 10
Commit the programs to your private GitHub account. Place it into the folder ca/lab03
.
References
- RISC-V web site.
- RISC-V (Wikipedia).
- RISC-V instructions. Chapters 1 and 2 in [CODR].
- The RISC-V Instruction Set Manual. Volume I: Unprivileged ISA.
- RISC-V Greencard.
- RISC-V Assembly Programmer’s Manual
- Central Processing Unit (Wikipedia).
- Motherboard (Wikipedia).
- System Bus (Wikipedia).
- Instruction Set Architecture (Wikipedia).
- Integer Overflow (Wikipedia).