In: Computer Science
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!
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