Question

Write an assembly language program to calculate the following. (((((20 + 21)X 22)+ 23)X 24)+ 25...)+ 2n Hint:Note that w...

Write an assembly language program to calculate the following.

(((((20 + 21)X 22)+ 23)X 24)+ 25...)+ 2n

Hint:Note that when n is even, carrying result is multiplied by 2 n . When n is odd, carrying result is added to 2 n (above formula shows the case where n is odd).

As an example, when n= 5, it should print 352.

•Your test caller should do the followings.

– Read n from keyboard.

– Call the sub procedure to do the calculation.

– Print the appropriate value, if possible or display appropriate messages for returned values -1 and -2 (see below)

•Your callee should do the followings

–Procedure should take n as a parameter.

–If n ≥ 0 , procedure should calculate the correct value. if n <0, -1 should be returned.

–As n increases, at one point, you will not be able to hold the result in a 32-bit register. You should check for it (probably using jo instruction). You should return -2 in that case.

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

Please Refer Comments for help.

f: /*Called Function */

push rbp

mov rbp, rsp

mov DWORD PTR [rbp-20], edi

cmp DWORD PTR [rbp-20], 0 /*comparing n with 0 */

jns .L2

mov eax, -1

jmp .L3

.L2:

mov QWORD PTR [rbp-8], 20 /*initializing result with 20 */

mov DWORD PTR [rbp-12], 21 /* starting with 21 */

jmp .L4

.L5:

mov eax, DWORD PTR [rbp-12]

cdqe

add QWORD PTR [rbp-8], rax

add DWORD PTR [rbp-12], 1

mov eax, DWORD PTR [rbp-12]

cdqe

mov rdx, QWORD PTR [rbp-8]

imul rax, rdx /*multily with even no. */

mov QWORD PTR [rbp-8], rax

add DWORD PTR [rbp-12], 1 /* increment iterator */

mov eax, 2147483648 /* Check for range of 32 bit integer */

cmp QWORD PTR [rbp-8], rax

jl .L4

mov eax, -2

jmp .L3

.L4:

mov eax, DWORD PTR [rbp-20]

add eax, eax

cmp DWORD PTR [rbp-12], eax

jle .L5

mov rax, QWORD PTR [rbp-8]

.L3:

pop rbp

ret

.LC0:

.string "%d"

main: /* Main Method */

push rbp

mov rbp, rsp

sub rsp, 16

lea rax, [rbp-4]

mov rsi, rax

mov edi, OFFSET FLAT:.LC0

mov eax, 0

call __isoc99_scanf

mov eax, DWORD PTR [rbp-4]

mov edi, eax

call f

mov esi, eax

mov edi, OFFSET FLAT:.LC0

mov eax, 0

call printf /* Print Result */

nop

leave

ret

Please give feedback for my answer if it helped you or raise your query in case of any confusion.

Happy coding...

Add a comment
Know the answer?
Add Answer to:
Write an assembly language program to calculate the following. (((((20 + 21)X 22)+ 23)X 24)+ 25...)+ 2n Hint:Note that w...
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
  • MIPS ASSEMBLY PROGRAM: PLEASE Write in MIPS Assembly language. Take strings as input and calculate and...

    MIPS ASSEMBLY PROGRAM: PLEASE Write in MIPS Assembly language. Take strings as input and calculate and print a simple checksum for each string. Make your string long enough to hold 50 characters. Don't forget to leave space for the null byte. Our checksum algorithm will produce a value which you can print with the syscall for printing a character. Stop reading strings when the user enters ".". The syscall to read a string (sycall code 8) adds a newline to...

  • PLEASE USE VERY BASIC REGISTERS AND CODE TO DO THE FOLLOWING Objectives: -write assembly language programs...

    PLEASE USE VERY BASIC REGISTERS AND CODE TO DO THE FOLLOWING Objectives: -write assembly language programs to:             -define a recursive procedure/function and call it.             -use syscall operations to display integers and strings on the console window             -use syscall operations to read integers from the keyboard. Assignment Description: Implement a MIPS assembly language program that defines "main", and "function1" procedures. The function1 is recursive and should be defined as: function1(n) = (2*n)+9                      if n <= 5              =...

  • Write a complete Java program, including comments in both the main program and in each method,...

    Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...

  • LC-3 Programming Help!! The Stack Protocol The following outline is the protocol for passing arguments to...

    LC-3 Programming Help!! The Stack Protocol The following outline is the protocol for passing arguments to a function and returning values. Everything is stored on the runtime stack so that space is used only when the function is executing. As a result the actual address of arguments and locals may change from call to call. However, the layout of the stack frame (activation record) is constant. Thus, the offests from the frame pointer (FP) to the parameters/locals are constant. All...

  • 1. Write a program in Assembly language using MIPS instruction set that reads two integer numbers...

    1. Write a program in Assembly language using MIPS instruction set that reads two integer numbers from the user named as start and end number and finds out all the prime numbers between start and end (including start and end). Your program should do the validation of both the numbers as follows: i. start number must be smaller or equal to the end number. ii. Both numbers must be positive. iii. The maximum value for the end number is 10000...

  • Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate...

    Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate the number of test scores the user enters; have scores fall into a normal distribution for grades -Display all of the generated scores - no more than 10 per line -Calculate and display the average of all scores -Find and display the number of scores above the overall average (previous output) -Find and display the letter grade that corresponds to the average above (overall...

  • 1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer...

    1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...

  • ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

    ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...

  • Write a C++ program that prompts the user with the following menu options: [E]rase–ArrayContent [C]ount–Words [R]ev–Words...

    Write a C++ program that prompts the user with the following menu options: [E]rase–ArrayContent [C]ount–Words [R]ev–Words [Q]uit 1. For Erase–ArrayContent option, write complete code for the C++ function Erase described below. The prototype for Erase is as follows: void Erase( int a[ ], int * N, int * Search-Element ) The function Erase should remove all occurrences of Search-Element from the array    a[ ]. Note that array a[ ] is loaded with integer numbers entered by the user through the...

  • Write VBA functions to calculate sin (x) using the Maclaurin arcsine series, and compare the values...

    Write VBA functions to calculate sin (x) using the Maclaurin arcsine series, and compare the values for sin-1(x) from your program to those given by the Excel spreadsheet function ASIN(x). The Maclaurin arcsine expansion is given by x 3x 6 40 (2n)! sin1(x)-2((2n+1) Note: This function by definition is only defined for-1 SxS1. When you write the code for calculating it, you will need to include code that assigns a value to it that reflects it is undefined for values...

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