Register to reply

X86 Assembly Calling Another Function

by whitehorsey
Tags: assembly, calling, function
Share this thread:
whitehorsey
#1
Nov25-12, 03:44 PM
P: 192
1.
firstFunction (parameters has an input and an output){
(ebx holds the input, ecx holds the output)
...........
mov eax, 1

push ebx
call factorial
pop ebx

.......
}

factorial(unsigned int n){
push ebx
mul ebx
}

3. I understand that you push ebx in the first function because you want to pass the input number to the factorial function. However, why does factorial function need to push ebx again? In addition, the line mul ebx (multiplies with eax) how does it know to multiply with eax. I thought I had to do something like mul ebx, eax yet that causes the build to fail.
Phys.Org News Partner Science news on Phys.org
Sapphire talk enlivens guesswork over iPhone 6
Geneticists offer clues to better rice, tomato crops
UConn makes 3-D copies of antique instrument parts
rcgldr
#2
Nov25-12, 06:58 PM
HW Helper
P: 7,033
Seems like there's missing code from this. It appears the goal is a recursive version of factorial, but the factorial fucntion is not calling itself. The destination for mul is always edx:eax, so the destination operand is not specified. The operation is edx:eax = eax x (source operand), where edx receives the upper 32 bits of the product, and eax receives the lower 32 bits of the product. There are other instructions where the register used is fixed, such as div, or the scan instructions such as scasb.
whitehorsey
#3
Nov25-12, 08:27 PM
P: 192
Quote Quote by rcgldr View Post
Seems like there's missing code from this. It appears the goal is a recursive version of factorial, but the factorial fucntion is not calling itself. The destination for mul is always edx:eax, so the destination operand is not specified. The operation is edx:eax = eax x (source operand), where edx receives the upper 32 bits of the product, and eax receives the lower 32 bits of the product. There are other instructions where the register used is fixed, such as div, or the scan instructions such as scasb.
Ooh so, edx has 0000 0000 0000 0000 while eax has whatever n is?

Sorry about the missing code! After the multiply stuff it calls the function again but before it calls the function it pushes ebx again. Why does it do that?

rcgldr
#4
Nov25-12, 08:42 PM
HW Helper
P: 7,033
X86 Assembly Calling Another Function

Quote Quote by whitehorsey View Post
Ooh so, edx has 0000 0000 0000 0000 while eax has whatever n is?
eax would should have the multiplicand or multiplier before, and the product after. until you try to calculate 13!, which is greater than 2^32. In this case the two numbers to be multiplied are 13 x 12! = hex 17328CC00, so in this case edx = hex 1 and eax = hex 7328CC00

Quote Quote by whitehorsey View Post
Sorry about the missing code! After the multiply stuff it calls the function again but before it calls the function it pushes ebx again. Why does it do that?
Because the input parameter to factorial needs to be pushed onto the stack before each call. The code is also missing the part where the parameter is decremented (subtract 1) before a call is made, and a check as the start of the factorial function to see if the input parameter is 0.

I'm going to assume that you were supposed to have already been given an example of how to do recursive factorial in C, and the point of this exercise is to implement the same function in assembly. Here is an example written in C:

unsigned int factorial(unsigned int n)
{
    if(n == 0)
        return(1);
    else
        return n * factorial(n-1);
}

int main()
{
unsigned int result;

    result = factorial(12);

/* ... */
    return(0);
}
whitehorsey
#5
Nov26-12, 01:49 AM
P: 192
Quote Quote by rcgldr View Post
eax would should have the multiplicand or multiplier before, and the product after. until you try to calculate 13!, which is greater than 2^32. In this case the two numbers to be multiplied are 13 x 12! = hex 17328CC00, so in this case edx = hex 1 and eax = hex 7328CC00
Oh sooo would I also need to have a mov edx, 1 in case I'm tested with integers >= 13? If so, how would I combine eax and edx together to get it's output?

Quote Quote by rcgldr View Post
Because the input parameter to factorial needs to be pushed onto the stack before each call. The code is also missing the part where the parameter is decremented (subtract 1) before a call is made, and a check as the start of the factorial function to see if the input parameter is 0.

I'm going to assume that you were supposed to have already been given an example of how to do recursive factorial in C, and the point of this exercise is to implement the same function in assembly. Here is an example written in C:

unsigned int factorial(unsigned int n)
{
    if(n == 0)
        return(1);
    else
        return n * factorial(n-1);
}

int main()
{
unsigned int result;

    result = factorial(12);

/* ... */
    return(0);
}
Ah I get it now! I'm so sorry again for not stating that I have also tested cases where the input parameter is 0 and I also did the subtraction.

I was wondering what is the difference between imul and mul?
rcgldr
#6
Nov26-12, 05:07 AM
HW Helper
P: 7,033
Quote Quote by whitehorsey View Post
Oh sooo would I also need to have a mov edx, 1 in case I'm tested with integers >= 13? If so, how would I combine eax and edx together to get it's output?
Since you're doing 32 bit code, I wouldn't worry about the >= 13 cases, so you can ignore edx for now. If you were doing 64 bit code, you would be using rax, rbx, ... , instead of eax, ebx, ... . I would assume this is not part of your class assignment.

Quote Quote by whitehorsey View Post
sorry again for not stating that I have also tested cases where the input parameter is 0 and I also did the subtraction.
It would help to show all of the code for your factorial function. It's not going to be that large.

Quote Quote by whitehorsey View Post
I was wondering what is the difference between imul and mul?
imul is for signed integers, mul is for unsigned integers. Might as well use unsigned integers in this case.
whitehorsey
#7
Nov27-12, 12:30 AM
P: 192
Quote Quote by rcgldr View Post
Since you're doing 32 bit code, I wouldn't worry about the >= 13 cases, so you can ignore edx for now. If you were doing 64 bit code, you would be using rax, rbx, ... , instead of eax, ebx, ... . I would assume this is not part of your class assignment.
Ooooh I see!!

Quote Quote by rcgldr View Post
It would help to show all of the code for your factorial function. It's not going to be that large.

imul is for signed integers, mul is for unsigned integers. Might as well use unsigned integers in this case.
Okay! Thank You!!


Register to reply

Related Discussions
Calling function im matlab? Engineering, Comp Sci, & Technology Homework 4
C++ Function Calling Help Engineering, Comp Sci, & Technology Homework 4
Beginner Function Calling Question Programming & Computer Science 2
C++ Function Calling Programming & Computer Science 7
Problems With Calling A Function Engineering, Comp Sci, & Technology Homework 2