Question

Assembly Language 64- bit system Description: You are responsible to implement several assembly functions to perform...

Assembly Language 64- bit system

Description:

You are responsible to implement several assembly functions to perform the simple arithmetic calculations for 2 64-bit integers. These functions will use the C function signature but the main logic within this function should be inline assembly code using the ASM block similar to the assembly example shown in class.

1. long XOR ( long op1, long op2 )- xor will return the result of bit exclusive OR of op1 / op2- can use XOR instruction

Template:

p1sample.cpp

/* CS47 - Project #1 template */

#include

#include

#include

long factorial (long n)

{

if (n == 1)

return 1;

else

return n * factorial(n - 1);

}

int main(int argc, char** argv)

{

long op1, op2, result;

op1 = op2 = result = 0;

if (argc != 3)

{

printf("Usage: %s op1 op2 (two integers)\n", argv[0]);

return 1;

}

op1 = atol(argv[1]);

op2 = atol(argv[2]);

printf("Operand 1 = %ld x%016lx Operand 2 = %ld x%016lx\n", op1,op1,op2,op2);

result = add(op1, op2);

printf("Add():\t%10ld x%016lx\n", result, result);

result = mult(op1, op2);

printf("Mult():\t%10ld x%016lx\n", result, result);

if (op2 == 0)

{

printf("Mod Error: Divide by 0\n");

result = 0;

}

else

{

result = mod(op1, op2);

printf("Mod():\t%10ld x%016lx\n", result, result);

}

if (op2 < 0)

{

printf("Error: Shift count must be >= 0\n");

result = 0;

}

else

{

result = shift(op1, 0, op2);

printf("ShiftL:\t%10ld x%016lx\n", result, result);

result = shift(op1, 1, op2);

printf("ShiftR:\t%10ld x%016lx\n", result, result);

}

if (op1 <= 0)

{

printf("Error: Factorial input must be a positive integer >=1\n");

result = 0;

}

else

{

result = factorial(op1);

printf("Fact():\t%10ld x%016lx\n\n", result, result);

}

return 0;

}

Sample output in this sequence:

Operand 1 = 10 x000000000000000a Operand 2 = 5 x0000000000000005

Add(): 15 x000000000000000f

XOR(): 15 x000000000000000f

Mult(): 50 x0000000000000032

Mod(): 0 x0000000000000000

ShiftL: 320 x0000000000000140

ShiftR: 0 x0000000000000000

RotateL: 320 x0000000000000140

RotateR:-6341068275337658368 xa800000000000000

Fact(): 3628800 x0000000000375f00

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

#include<stdlib.h>
#define INT_BITS 32

long factorial (long n)
  
{
  
if (n == 1)
  
return 1;
  
else
  
return n * factorial(n - 1);
  
}
long add(long op1,long op2){
       return op1+op2;
   }
long mult(long op1,long op2){
   return op1*op2;
   }
long mod(long op1,long op2){
       return op1%op2;
   }
long shift(long op1,int a,int op2){
   if(a==0){
       return op1<<op2;
   }
   else{
       return op1>>op2;
   }
}

int main(int argc, char** argv)

{

long op1, op2, result;
  
op1 = op2 = result = 0;
  
if (argc != 3)
  
{
  
printf("Usage: %s op1 op2 (two integers)\n", argv[0]);
  
return 1;
  
}
  
op1 = atol(argv[1]);
  
op2 = atol(argv[2]);
  
printf("Operand 1 = %ld x%016lx Operand 2 = %ld x%016lx\n", op1,op1,op2,op2);
  
result = add(op1, op2);
  
printf("Add():\t%10ld x%016lx\n", result, result);
  
result = mult(op1, op2);
  
printf("Mult():\t%10ld x%016lx\n", result, result);
  
if (op2 == 0)
  
{
  
printf("Mod Error: Divide by 0\n");
  
result = 0;
  
}
  
else
  
{
  
result = mod(op1, op2);
  
printf("Mod():\t%10ld x%016lx\n", result, result);
  
}
  
if (op2 < 0)
  
{
  
printf("Error: Shift count must be >= 0\n");
  
result = 0;
  
}
  
else
  
{
  
result = shift(op1, 0, op2);
  
printf("ShiftL:\t%10ld x%016lx\n", result, result);
  
result = shift(op1, 1, op2);
  
printf("ShiftR:\t%10ld x%016lx\n", result, result);
  
}
  
if (op1 <= 0)
  
{
  
printf("Error: Factorial input must be a positive integer >=1\n");
  
result = 0;
  
}
  
else
  
{
  
result = factorial(op1);
  
printf("Fact():\t%10ld x%016lx\n\n", result, result);
  
}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Assembly Language 64- bit system Description: You are responsible to implement several assembly functions to perform...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression...

    Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression. Right now, it works with single digits. I need to modify it so that it can evaluate expressions with also 2 digits, example: (60+82)%72. Additionally I need to display an error when the parenthesis don't match like (89+8(. I have muted some line that would print the postfix expression with a space like: 22 8 + when the input...

  • a) Write the following C function in Assembly. You must follow the System V 64-bit calling...

    a) Write the following C function in Assembly. You must follow the System V 64-bit calling convention and use AT&T Syntax notation. long fibonacci (long n) { if (n == 0) return 0; else if (n == 1) return 1; else return (fibonacci (n - 1) + fibonacci (n - 2)); } b) The Windows x86-64 calling convention passes function parameters in the registers RCX, RDX, R8 and R9 and returns values on register RAX. Caller saved registers are: RAX,...

  • What are the description of approach or technique used in this? #include <stdio.h> long charCount=0; long...

    What are the description of approach or technique used in this? #include <stdio.h> long charCount=0; long digitCount=0; long lineCount=0; long wordCount=0; long digitFreq[10]; int c=0; int outOfWord=0; int state=0; int inWord=1; void printStats(); void totalWords(); void totalLines(); void digitFrequency(); int main(int argc,char **argv) { int i; for(i=0;i<10;i++) { digitFreq[i]=0; } state=outOfWord; c=getchar(); while((c !=EOF)) { charCount++; digitFrequency(c); totalLines(c); totalWords(c); c=getchar(); } printStats(); return 1; } void totalLines(int c) { if(c == '\n') { lineCount++; } } void digitFrequency(int c) {...

  • This is the given code: /** * This program uses a Taylor Series to compute a...

    This is the given code: /** * This program uses a Taylor Series to compute a value * of sine. * */ #include<stdlib.h> #include<stdio.h> #include<math.h> /** * A function to compute the factorial function, n!. */ long factorial(int n) { long result = 1, i; for(i=2; i<=n; i++) { result *= i; } return result; } int main(int argc, char **argv) { if(argc != 3) { fprintf(stderr, "Usage: %s x n ", argv[0]); exit(1); } double x = atof(argv[1]); int...

  • T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer...

    T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer is true or false in order to receive credit. #include <stdio.h> #include <string.h> int run_through(int num, char **a) { int i; int check=0; for(i=0;i<num;i++) { printf("%s\n", *(a+i)); if(strcmp(*(a+i), "filename")==0) { check=1; } } return check; } char** find_filename(int n, char **b) { int i; int check=0; for(i=0;i<n;i++) { if(strcmp(*b, "filename")==0) { b++; break; } b++; } return b; } int main(int argc, char **argv)...

  • Assembly language 64 bit please ! An example file for set up ==========+ ;| Data Segment...

    Assembly language 64 bit please ! An example file for set up ==========+ ;| Data Segment BEGINS Here | ;+======================================================================+ segment .data ;Code this expression: sum = num1+num2 num1 dq 0 ;left operand of the addition operation num2 dq 0 ;right operand of the addition operation sum dq 0 ;will hold the computed Sum value RetVal dq 0 ;Integer value RETURNED by function calls ;can be ignored or used as determined by the programmer ;Message string prompting for the keyboard...

  • a) Write the following C function in Assembly. You must follow the System V 64-bit calling convention and use AT&T S...

    a) Write the following C function in Assembly. You must follow the System V 64-bit calling convention and use AT&T Syntax notation. Note: You cannot change the algorithm in any way so your assembly function must still be recursive. (20 points) long Catalan(long n) { long sum = 0; if (n == 0) return 1; for (int i = 0; i < n; i++) { sum += Catalan(i) * Catalan(n - i - 1); } return sum; } b) The...

  • Implement the following statements using MS430 assembly instructions. You may use more than one, ...

    Implement the following statements using MS430 assembly instructions. You may use more than one, but you should minimize the number of instructions required. You can use both native and emulated instructions. Use hex notation for all numbers 1. (a) Move the word located in register R14 to R15 (b) Increment the word in R6 by 2. (c) Perform a bitwise ANDing of the word located at address 0x0240 with the datum in R15, placing the results in R15. (d) Rotate...

  • LC3 stack (factorial) I need help in writing factorial in Lc3 language by using stack.. ; Begin ...

    LC3 stack (factorial) I need help in writing factorial in Lc3 language by using stack.. ; Begin reserved section: do not change ANYTHING in reserved section! .ORIG x3000 BR Main ; Parameter and result Param .FILL x0004 Result .BLKW 1 ; Constants Stack .FILL x4000 One .FILL #1 MinusOne .FILL #-1 ; End reserved section: do not change ANYTHING in reserved section! ;------------------------------------------------------------------------------- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; int Factorial(int N) ; Returns N! (must be a recursive function) ; Factorial ;__________________...

  • Translate the following C program to Pep/9 assembly language. It multiplies two integers using a ...

    Translate the following C program to Pep/9 assembly language. It multiplies two integers using a recursive shift-and-add algorithm. mpr stands for multiplier and mcand stands for multiplicand. A recursive integer multiplication algorithm #include <stdio.h> int times(int mpr, int mcand) {    if (mpr == 0) {       return 0;    }    else if (mpr % 2 == 1) {       return times(mpr / 2, mcand * 2) + mcand;    }    else {       return times(mpr / 2, mcand * 2);    } } int main() {    ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT