Here you can find few exercises that are not part of the final grading, but might help you to understand some of the concepts.
Please, note, completing these tasks wont give any additional point
Module¶
Naive modulo (1p)¶
When we first learned division in primary school, we learned division with a remainder also known as Euclidean division. As it only deals in whole numbers, the result doesn't have a fractional part, and this why the remainder exists.
To get only the remainder of a division, we use an operation called modulo, which is called
mod in math and the %-operator in many programming languages:10 % 3 = 1 12 % 4 = 0 123 % 17 = 4 5 % 9 = 5
Like with division, modulo by zero is undefined, and it has the same precedence in order-of-operations as multiplication and division.
This remainder is actually often useful, and thus you will implement it here.
The dividend will be in register
%r8 and the divisor in %r9. Put the result in register %rax. Only nonnegative numbers are used in this task. The divisor will also never be zero.HINT: You can just keep substracting the divisor from the dividend until the dividend is smaller than the divisor. What is left in the dividend is the result.
Hints
Messages
Root Square¶
Square root (3p)¶
Calculate the square root of a number in Y86 assembly using the following algorithm.
The number which square root is calculated (variable num in the algorithm) is stored in
%r12 and the result (variable res in algorithm) must be returned in %rcx. The numbers are 16 bits and always positive// num is the number which square root is calculated
// res is the result
int32_t res = 0;
int32_t bit = 1 << 16;
while (bit > num) {
bit >>= 2;
}
while (bit != 0) {
if (num >= res + bit) {
num -= res + bit;
res = (res >> 1) + bit;
} else {
res >>= 1;
}
bit >>= 2;
}
If the result is not exact, present the result without decimals and always rounding down. For instance:
sqrt(2345) = 48.425200051. -> 48.In this case, you can use up to 85000 instructions (in contrast to 10000 that are required normally). However,if for any of the test you run more than 10000 the exercise grade will be reduced.
Hox! You can implement the algorithm better if you understand it. Remember the value of a division, can be greater than 2.
Hox 2!: Although we do not recommend it in this exercise, the algorithm can be optimized using lookuptables.
Hints
Messages