Question

In: Computer Science

Develop an x86 assembly language program that properly executes an "is even" function: bool isEven(int value)...

Develop an x86 assembly language program that properly executes an "is even" function:

bool isEven(int value)
If the value passed in is even, return 1
If the value passed in is odd, return 0
Remember that the return value must be placed in the EAX register
Make sure that any registers (not EAX) used by this function are placed back to their initial states prior to exiting

Allow an end user to test this function in the following manner:

Prompt the user to enter an integer value ("Enter a value: ")
Allow the user to input their value
Pass the user input value to the isEven() function
Let the function do its work and return the correct value
Print out the result as the result of a conditional expression
The value is even!
The value is odd!

Solutions

Expert Solution

ALGORITHM

1.Take input value from user.

2.Pass value to iseven() function and store the return value in variable res;

3.Check if res==1 then print "The value is even!" else goto step 4.

4.Print "The value is odd!"

iseven(value):

1.Check if value%2==0 if yes then return 1 else return 0

X86 Assembly program to implement above algorithm:

Code                                                                             Comment

iseven(int):                                                              //iseven function started

push rbp                                                                        //pushing base pointer to stack

mov rbp, rsp                                                          //moving content of stack pointer to base pointer

mov DWORD PTR [rbp-4], edi                         //moving edi register value to ptr

mov eax, DWORD PTR [rbp-4]                      //moving ptr to register eax

and eax, 1                                                   //performing and operation between eax and 1

test eax, eax                                             //perform test operation

jne .L2                                                       //if not equal jump to L2 block

mov eax, 1                                              //Assign 1 to eax register

jmp .L3                                                  //jump to L3 block

.L2:                                                                   //L2 block started

mov eax, 0                                                       //Assign 0 to eax register

.L3:                                                           //L3 block started

pop rbp                                                   //pop the base pointer from stack

ret                                                             //return to the function call

.LC0:                                                             //LCO block

.string "Enter a value"                                  //defining string for input

.LC1:                                                               //LC1 block

.string "The value is even!"                           //defining string for output

.LC2:                                                              //LC2 block

.string "The value is odd!"                            //defining string for output

main:                                                          //main function started

push rbp                                                       //push base pointer to stack

mov rbp, rsp                                                //move stack pointer to base pointer

sub rsp, 16                                                 //subtract 16 from stack pointer

mov esi, OFFSET FLAT:.LC0                         //Calling string LC0

mov edi, OFFSET FLAT:_ZSt4cout

call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

lea rax, [rbp-8]                                       //declaring variable value

mov rsi, rax                                            //move rax to rsi register

mov edi, OFFSET FLAT:_ZSt3cin            //input from user

call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)

mov eax, DWORD PTR [rbp-8]                   //move ptr value to regsiter eax

mov edi, eax                                           //move eax to edi register

call iseven(int)                                        //call function iseven

movzx eax, al                                          //move al to register eax

mov DWORD PTR [rbp-4], eax                //move register eax to ptr

cmp DWORD PTR [rbp-4], 1                   //check if return value is equal to 1

jne .L5                                                     //if not goto block L5

mov esi, OFFSET FLAT:.LC1                    //else call string "The value is even"

mov edi, OFFSET FLAT:_ZSt4cout             //print output

call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

jmp .L6                                                    //jump to block L6

.L5:                                                            //L5 block started

mov esi, OFFSET FLAT:.LC2                      //call string "The value is odd"

mov edi, OFFSET FLAT:_ZSt4cout             //print output

call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

.L6:                                              //L6 block started

mov eax, 0                                   //Assign 0 to eax register

leave                                             //end of program

ret                                           //return


Related Solutions

Develop an x86 assembly language program that properly executes an "x to the y power" function:...
Develop an x86 assembly language program that properly executes an "x to the y power" function: int power(int x, int y) Compute the integer value that is x to the y power Do not concern yourself with the possibility of overflow You may only use only a loop structure to compute the value Remember that the return value must be placed in the EAX register Make sure that any registers (not EAX) used by this function are placed back to...
Assembly Language for x86 processors You are to write a program which should first ask for...
Assembly Language for x86 processors You are to write a program which should first ask for 4 random numbers from 0-20 (user will inpute these numbers in no preset order). Input these 5 numbers in variables called num1, num2, num3, num4, and num5. When done, your program should sort these numbers (you will use lots of conditions to check order). num1 should contain smallest number while num5 should contain the biggest. display the contents of num1 through num5 on the...
Write an X86-series assembly language program that checks whether input string is palindrome or not. A...
Write an X86-series assembly language program that checks whether input string is palindrome or not. A palindrome is a word, number, phrase or any other sequence which reads the same backward as forward e.g. madam, racecar. Sample Execution: Please enter a String: redivider The string is a palindrome Another Sample Execution: Please enter a String: abracadabra The string is not a palindrome
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your...
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your code in main
Please write in x86 Assembly language on Visual Studio Write a program to compare two strings...
Please write in x86 Assembly language on Visual Studio Write a program to compare two strings in locations str1 and str2. Initialize str1 to Computer and initialize str2 to Compater. Assume Str1 holds the correct spelling and str2 may have an incorrect spelling. Use string instructions to check if str2 is correct and if not correct the mistake in str2.
Write an x86 assembly language program that performs equivalently to the C++ source code file shown...
Write an x86 assembly language program that performs equivalently to the C++ source code file shown below.Please note that commented out behavior must be implemented in x86 assembly language. There is no standard, portable way to perform some of these actions in C++. #include void main() { // Use registers for these in your x86 assembly language program // Only use the .data segment for string (character array) variables int eax; int esi; int ecx; int edi; // Loop the...
Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3]...
Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3] = {1, -2, 15 }; int result = 0, index = 0; int numElements = 3; while (index < numElements) { if ( index >= 1 && numberArray[index] > 3 ) { result += numberArray[index] ; } else { result -= 3; } index++; } return result; } int problem2_ ( ) { int a, modulo int answer = 0, b = 3; for...
Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3]...
Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3] = {1, -2, 15 }; int result = 0, index = 0; int numElements = 3; while (index < numElements) { if ( index >= 1 && numberArray[index] > 3 ) { result += numberArray[index] ; } else { result -= 3; } index++; } return result; } int problem2_ ( ) { int a, modulo int answer = 0, b = 3; for...
Please use assembly language x86 Visual Studio Write a program to add the following word size...
Please use assembly language x86 Visual Studio Write a program to add the following word size numbers:15F2, 9E89, 8342, 99FF, 7130 using adc instruction and a loop. The result must be in DX, AX. Show the result in debug window.
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number;...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number; Scanf (“%d”, & number); if (number % 2 ==0) { printf (“Even\n”); } else { printf(“Odd\n”); } Return 0; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT