Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Translate the C program you wrote to do multiplication in constant time into an

ID: 3835687 • Letter: T

Question


Translate the C program you wrote to do multiplication in constant time into an assembly program
called multAssembly.s.
1. The label for the value a in a * b is a
1. 4 bytes of space should be made for a2. The label for the value b in a * b is b
1. 4 bytes of space should be made for b

3. Place the upper 32 bits of the product in EDX
4. Place the lower 32 bits of the product in EAX
5. Don't forget that if you want to shift by a variable amount the shift amount must be placedin CL. Your assembly code won't work if you try to place it in any other register.
6. Pay careful attention when the bits are crossing boundaries between 1 register and the other
1. If you shift a number to the left and a 1 is shifted out of the register the carry bit of the
flags register will be set.
2. Similarly if the result of your addition would produce 33 bits than the carry bit will alsobe set7. AFTER the last line of code that you wish to be executed in your program please place thelabel done.
1. Make sure that there is an instruction after the done line and a new line after that
instruction. If you don't your output won't match mine.
8. IT IS OF VITAL IMPORTANCE THAT YOU NAME YOUR LABELS ASSPECIFIED AND MAKE THE APPROPRIATE AMOUNT OF SPACE FOR EACHVARIABLE! I will be using gdb to test your code and if your labels do not match then thetests will fail. You must also make sure to include the done label AFTER the last line ofcode you want executed in your program so that I know where to set break points.
C program to translate, in Assembly AT&T Syntax(MAKE SURE TO FOLLOW CONSTRAINTS ABOVE):

#include <stdio.h>
#include <stdlib.h>

long long mult(long long a, long long b) {
long long ret = 0;

while (b) {
if (b & 1) ret += a;
a <<= 1;
b >>= 1;
}
return ret;
}

int main(int argc, char *argv[]) {
unsigned int a = 0;
unsigned int b = 0;

sscanf(argv[1], "%u", &a);
sscanf(argv[2], "%u", &b);
printf("%u * %u = %llu ", a, b, mult(a, b));
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

long long mult(long long a, long long b) {
long long ret = 0;

while (b) {
if (b & 1) ret += a;
a <<= 1;
b >>= 1;
}
return ret;
}

int main(int argc, char *argv[]) {
unsigned int a = 0;
unsigned int b = 0;

sscanf(argv[1], "%u", &a);
sscanf(argv[2], "%u", &b);
printf("%u * %u = %llu ", a, b, mult(a, b));
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote