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...
Hi this is Assembly Language MASM x86 program. Please write it in the language and please...
Hi this is Assembly Language MASM x86 program. Please write it in the language and please explain it with comments thank you Please answer it I really need help this question was refunded before so please answer. Thank you so much also these are two separate programs thank you. 1) Write a procedure to read in decimal or hex number (byte-sized) Then write a procedure using shifts and ANDS to convert the string to a binary number (if is backward,...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the Irvine library for the print function #include <iostream> // The string that needs to be printed char word[] = "Golf\0"; // Pointer to a specific character in the string char * character = word; //NOTE: This main() function is not portable outside of Visual Studio void main() { // Set up a LOOP - See the while loop's conditional expression below int ecx =...
in assembly language x86 Masm, Write a program that calculate the first seven values of the...
in assembly language x86 Masm, Write a program that calculate the first seven values of the Fibonacci number sequence, described by the following formula: Fib(0) = 0, Fib(1) = 1, Fib(2) = Fib(0)+ Fib(1), Fib(n) = Fib(n-1) + Fib(n-2). You NEED to calculate each value in the series "using registers and the ADD operation" You can also use variables, Have your program print out "The first seven numbers is" Use WriteInt for the printing, Place each value in the EAX...
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
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...
Please write in x86 Assembly language on Visual Studio. IRVINE32 Write a program to copy one...
Please write in x86 Assembly language on Visual Studio. IRVINE32 Write a program to copy one array of size 24 to another array of size 24 using string instructions. Write 3 versions of this code. One code must copy byte at a time. One code must copy word at a time and one code must copy double word at a time. Cut and paste the array in memory to show your code is working.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT