Ask Question, Ask an Expert


Ask Programming Language Expert

This assignment assumes you are programming a 64-bit processor. You are to prepare one short C file, big_mult.c and two short assembly language files mulq.s. and subfaq.s. Details on each of these code files is as follows.

Problem1): big_mult.c

You have to prepare the C program called big_mult.c that multiplies two unsigned 64-bit integers, x and y, read from the command line. Output is a pair of unsigned 64-bit integers representing most significant and least significant 64 bits of the full 128-bit product x * y. Inputs and outputs are to be given in hexadecimal format. Your C program will take care of reading inputs and printing the output, but it will call the function mulq.s to do the actual multiplication.

Your C program mustuse only unsigned long long int variables and should not do any arithmetic. To decrease the length of our type declarations

typedef unsigned long long int ulli;
typedef long long int lli;
This allows us to abbreviate unsigned long long int with the shorter name ulli and long long int with lli.

The function defined by mulq.s should have the following declaration in C before the function main.
void mulq(ulli x, ulli y, ulli *high, ulli *low);

The least significant 64 bits of the product are to be assigned to low, and the most significant 64 bits of the product are to be assigned to high. Remember to put an appropriate header comment into your assembly file (the ordinary C comment /* ... */ will work for assembly too). You will also need to read carefully the description of the mulq instruction in the Intel 64/IA32 instruction set reference manual, Intel 64/IA32 instruction set reference manual, or equivalent documentation. Learning to read this two-volume 800+ page document is part of your learning experience on this assignment.

One way to approach writing this assembly program is to prepare a similar program in C, compile it to assembly code using the -S option, and modify the resulting assembly code to do what you need. Your final assembly code should be very short and should contain only one multiplication instruction mulq.
The compile command to test your programs will look like this:
gcc64 -Wall -std=gnu99 -o big_mult big_mult.c mulq.s .

Here are 32- and 64-bit sample outputs to use in testing your programs.
C:>big_mult 2f432f43 629b03cb
2f432f43 x 629b03cb = 12345678 87654321

C:>big_mult 99d0c486a0fad481 76a185cea6f497c7
99d0c486a0fad481 x 76a185cea6f497c7 = 4747474747474747 4747474747474747

Remember that arguments are passed differently in the 64-bit architecture than in the 32-bit architecture. The registers used for parameter passing in Windows are different from those described in the textbook for Unix. Windows passes only the first four parameters in registers. The registers used by Unix are %rdi, %rsi, %rdx, %rcx, %r8, %r9 in that order. The registers used by Windows are %rcx, %rdx, %r8, %r9 . See Class10.pdf for details on register usage.

Problem 2): subfac.s

You are to prepare an assembly language program called subfaq.s that computes the generalized subfactorial function of nonnegative integer inputs i0 and n. The generalized subfactorial function is defined by the following recursion.

gs(0) = 1
inc(0) = i0

gs(n) = n gs(n-1) - inc(n-1)
inc(n) = -inc(n-1)

The generalized subfactorial function is closely related to the factorial function:
Factorial(n) can be written n! and is equal to gs(n) when i0 = 0.
Subfactorial(n) can be written !n and is equal to gs(n) when i0 = 1.

The values of both factorial and subfactorial get large very rapidly as n increases, so your program should return a 64-bit number representing gs(n) mod 264. In other words, just let your 64-bit register overflow. Try to make your code as short and efficient as possible. I recommend not using recursion, but rather writing a loop that starts from 0 and increments up to n. prepare your assembly code from scratch rather than writing C and compiling to assembly. It takes only 10 to 20 instructions, and this will be your only assignment to prepare code in assembly. Don't miss out on the experience. You may copy your procedure entry and exit code from the textbook or the gcc compiler output without citing the source. Try to get your assembly code for subfac.s to occupy at most 50 bytes (30 is possible). To see your machine code, use
gcc64 -c subfaq.s
to produce object file subfaq.o , then use
objdump64 -d subfaq.o .

The following C program (called run_subfaq.c ) can be used to test your subfaq function.

lli subfaq(int n, lli i0);
int main(int argc, char* argv[])
    int i = 1, n;
    lli i0, ans;
    if(argc < 3) {printf("Usage: gs i0 n1 n2 n3 ... (list of non-negative integers)"); return -1;}
    sscanf(argv[i++], "%lld", &i0);
    while (i < argc) {
        sscanf(argv[i], "%d", &n);
        if(n < 0) printf("%d out of range\n", n);
        else {
            ans = subfaq(n, i0);
            printf("gs(%d, %lld) = %lld\n", n, i0, ans);

The compile command to test your programs will look like this:

gcc64 -Wall -std=gnu99 -o gs run_subfaq.c subfaq.s .

Here are two sample outputs to use in testing your programs.

C:>gs 0 0 1 2 3 4 5 6 13
gs(0, 0) = 1
gs(1, 0) = 1
gs(2, 0) = 2
gs(3, 0) = 6
gs(4, 0) = 24
gs(5, 0) = 120
gs(6, 0) = 720
gs(20, 0) = 2432902008176640000
gs(21, 0) = -4249290049419214848

C:>gs 1 0 1 2 3 4 5 6 13
gs(0, 1) = 1
gs(1, 1) = 0
gs(2, 1) = 1
gs(3, 1) = 2
gs(4, 1) = 9
gs(5, 1) = 44
gs(6, 1) = 265
gs(20, 1) = 895014631192902121
gs(21, 1) = 348563181341392924
(The outputs for n = 21 have overflowed the 64-bit word size.)

The problems that go with this assignment are the Piazza Resources page: Problems_3.rtf. Fill in the answers and put it in your drop box.
Submission requirements and grading system

It must contain one problems file (.rtf), and three source code files only, with the exact names as given above.

A separate file hw3.s containing the assembly source code for functions mulq.s and subfaq.s may be (optionally) submitted to the automatic grader via the link (also found on my website): Homework Submission.

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M9560

Have any Question? 

Related Questions in Programming Language

Write a program that creates a picture of a mountain

Write a program that creates a picture of a mountain panorama from a height profile entered by the user. The following screenshot shows what the output could look like: The picture shall consist of 5 text lines of length ...

Assignmentinstructionsthe first programming project

Assignment Instructions The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of four classes. The first class is the ...

Programming oneusing jgrasp and the software development

Programming One Using jGrasp and the Software Development Kit, write a program in response to the following prompt: Write a program that prompts the user to input three numbers. This program should then output the number ...

Suppose you have a class cbirdsuppose you have a class

Suppose you have a class CBird Suppose you have a class CBird, as follows, that you want to use as a base class for deriving a hierarchy of bird classes: class CBird { protected: int wingSpan; int eggSize; int airSpeed; ...

Programming languages assignment write the following as

Programming Languages Assignment Write the following as Prolog rules:  1. Implement a rule "dogEnthusiast". Someone is a "dogEnthusiast" if they own AT LEAST TWO dogs. Assume that the only types of facts available are: " ...

Rainfall programwrite a rainfall program that stores the

Rainfall Program Write a RainFall program that stores the total rainfall for each of 12 months into an array of doubles. The program should have methods that return the following: * The total rainfall for the year * The ...

Suppose that we implement the functions of figure using a

Suppose that we implement the functions of Figure using a display. Show the display at the moment the first call to fib0(1) is about to return. Also, indicate the saved display entry in each of the activation records on ...

Construct a loop which will allow for the daily fan count

Construct a loop which will allow for the daily fan count to be entered for each game. The games number (1-4) should be part of the prompt displayed to the user. Within this loop, you will need other loop(s) to validate ...

The program will store the realty listings data as a

The program will store the realty listings data as a dynamically allocated LINKED LIST, instead of an array of records. The program will allow the realtors to both maintain and use the listings data. 1. Again, begin the ...

James wants to buy a motorcycleinstructions the following

James wants to buy a motorcycle Instructions: The following programming problem can be solved by a program that performs three basic tasks-Input Data, Process Data, and Output Results. Use RAPTOR to design a suitable pro ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Section onea in an atwood machine suppose two objects of

SECTION ONE (a) In an Atwood Machine, suppose two objects of unequal mass are hung vertically over a frictionless

Part 1you work in hr for a company that operates a factory

Part 1: You work in HR for a company that operates a factory manufacturing fiberglass. There are several hundred empl

Details on advanced accounting paperthis paper is intended

DETAILS ON ADVANCED ACCOUNTING PAPER This paper is intended for students to apply the theoretical knowledge around ac

Create a provider database and related reports and queries

Create a provider database and related reports and queries to capture contact information for potential PC component pro

Describe what you learned about the impact of economic

Describe what you learned about the impact of economic, social, and demographic trends affecting the US labor environmen