So I'm working on an assignment for my computer science course, We have to take an algorithm thats written in C and replicate it in assembly (SPARC). My issue is that I know very little C code since I specialize in java. Can anyone help me look at this code and give me its Java equivalent? If you guys have any tips on using it straight for SPARC, I'm open to those ideas too. Thanks!
neg = multiplier >= 0 ? 0 : 1;
product = 0;
for (i = 32; --i >= 0; ) {
if (multiplier & 1)
product += multiplicand;
(product and multiplier registers) >> 1;
}
if (neg)
product -= multiplicand;
This is a code to implement multiplication (though the given code is wrong) by yourself, without using
*
operator. See this snippet,Now code yourself to handle negative numbers.(Hope that now you know what the code does).The
neg
in your code is supposed to check if the multiplier is negative, but its not efficient to check only the multiplier,you should check bothmultiplier
andmultiplicand
.