1. Imagine that you are targeting an instruction set architecture that does not
ID: 3743099 • Letter: 1
Question
1. Imagine that you are targeting an instruction set architecture that does not have an integer division or modulus operation. 1. Write a function in C that takes two signed, 32-bit integers as arguments and returns a 2-element struct containing the quotient and remainder as 32-bit integer fields. For division by zero, return (0,0l. 2. Compile your function to a target machine of your choosing and use profiling or debugging tools of your choice to determine the number of instructions executed in a single call to your function. 3. If supporting a HW divider increased cycle time by 1%, CPI is exactly 1 for all non-DIV instructions, and DIV operations accounted for 5% of all instructions, how fast would a HW divider have to be to offer a better total program execution time than your SW division?Explanation / Answer
// We define a structure which will have the quotient and remainder.
// The assumption here is that a is the dividend and b is the divisor.
// For the divide by zero condition: assigning the quotient and remainder to zero.
// For the other conditions, simply keep subtracting the value of b from a until it is less than b.
// Every time we deduct the value, we increment the counter by one.
// In the end, the remainder is the value left in a, which is less than b.
#include <stdio.h>
struct answer {
int remainder;
int quotient;
};
struct answer divide(int a, int b) {
struct answer result;
if (b == 0) {
result.quotient = 0;
result.remainder = 0;
return result;
}
while (a >= b) {
result.quotient++;
a = a - b;
}
result.remainder = a;
return result;
}
int main() {
struct answer res = divide(10, 2);
printf("Divide answer: %d %d ", res.quotient, res.remainder);
res = divide(100, 7);
printf("Divide answer: %d %d ", res.quotient, res.remainder);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.