×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Implementing functions using x86 assembly assignment help

July 09, 2024
Rehana Magnus
Rehana Magnus
🇨🇦 Canada
Assembly Language
Rehana Magnus, PhD in Computer Science from the esteemed Acadia Institute of Technology, Canada. With 6 years of experience, specializes in assembly language programming. Proficient in low-level coding, optimizing performance, and enhancing system functionality.
Key Topics
  • Processing and Storing Functions in EAX Register
Tip of the day
Use Python libraries effectively by importing only what you need. For example, if you're working with data, using libraries like pandas and numpy can save time and simplify complex tasks like data manipulation and analysis.
News
In 2024, the Biden-Harris Administration has expanded high-dosage tutoring and extended learning programs to boost academic achievement, helping programming students and others recover from pandemic-related setbacks. These initiatives are funded by federal resources aimed at improving math and literacy skills​

Processing and Storing Functions in EAX Register

implemeting-functions-x86-assembly

INCLUDE Irvine32.inc .DATA prompt DB "Enter a value: ",0 message DB "The absolute value is: ", 0 result DD 0 .CODE main PROC ; Print prompt for value mov edx, OFFSET prompt ; load string address call WriteString ; print the string call ReadInt ; Read a number push eax ; pass value to function call abs ; abs(value) add esp, 4 ; remove value from stack mov [result], eax ; save result in variable ; Print result message mov edx, OFFSET message ; load string address call WriteString ; print the string mov eax, [result] ; load result call WriteInt ; print the number call CrLf ; move to a new line call WaitMsg ; wait until user presses a key exit ; exit the program main ENDP ; Function abs abs PROC push ebp ; save frame pointer mov ebp, esp ; create new stack frame mov eax, DWORD PTR[ebp + 8] ; load argument value from stack cmp eax, 0 ; compare value with zero jge return ; if it's positive, return without changes neg eax ; else convert to positive by inverting the value return: pop ebp ; restore frame pointer ret ; return to caller abs ENDP END main INCLUDE Irvine32.inc .DATA prompt DB "Enter a value: ",0 oddMsg DB "The value is odd!", 0 evenMsg DB "The value is even!", 0 .CODE main PROC ; Print prompt mov edx, OFFSET prompt ; load string address call WriteString ; print the string call ReadDec ; Read a number push eax ; pass value to function call isEven ; isEven(value) add esp, 4 ; remove value from stack cmp eax, 0 ; if the return value is 0, je odd ; is odd ; else, print is even mov edx, OFFSET evenMsg ; load string address call WriteString ; print the string jmp done ; terminate program odd: ; Print is odd mov edx, OFFSET oddMsg ; load string address call WriteString ; print the string done: call CrLf ; move to a new line call WaitMsg ; wait until user presses a key exit ; exit the program main ENDP ; Function isEven isEven PROC push ebp ; save frame pointer mov ebp, esp ; create new stack frame mov eax, 0 ; return 0 by default test DWORD PTR[ebp + 8], 1 ; test stack argument lsb bit to see if the value is odd jne return ; if it's not zero, it's odd, return mov eax, 1 ; else, is even, return 1 return: pop ebp ; restore frame pointer ret ; return to caller isEven ENDP END main INCLUDE Irvine32.inc .DATA promptx DB "Enter a value for x: ",0 prompty DB "Enter a value for y: ",0 message DB "x to the y power is: ", 0 x DD 0 y DD 0 result DD 0 .CODE main PROC ; Print prompt for x mov edx, OFFSET promptx ; load string address call WriteString ; print the string call ReadDec ; Read a number mov [x], eax ; save number in variable ; Print prompt for y mov edx, OFFSET prompty ; load string address call WriteString ; print the string call ReadDec ; Read a number mov [y], eax ; save number in variable push DWORD PTR[y] ; pass y value to function push DWORD PTR[x] ; pass x value to function call power ; power(x, y) add esp, 8 ; remove x and y values from stack mov [result], eax ; save result in variable ; Print result message mov edx, OFFSET message ; load string address call WriteString ; print the string mov eax, [result] ; load result call WriteDec ; print the number call CrLf ; move to a new line call WaitMsg ; wait until user presses a key exit ; exit the program main ENDP ; Function power power PROC push ebp ; save frame pointer mov ebp, esp ; create new stack frame push ecx ; save registers push edx mov eax, 1 ; load 1 to return 1 if y is zero mov ecx, DWORD PTR[ebp + 12] ; load value of y cmp ecx, 0 ; see if y is zero je return ; if zero return 1 pow_loop: mul DWORD PTR[ebp + 8] ; multiply x by previous power loop pow_loop ; decrement y and repeat loop if y is not zero return: pop edx ; restore registers pop ecx pop ebp ; restore frame pointer ret ; return to caller power ENDP END main

Similar Samples

Explore our comprehensive collection of programming assignment samples at ProgrammingHomeworkHelp.com. Covering a wide range of languages including Java, Python, C++, and specialized topics like Assembly Language and SQL, our samples exemplify our expertise in delivering precise solutions. Dive into our examples to see how we can assist you in mastering programming concepts and achieving academic excellence.