# x86 Assembly Calling Another Function

by whitehorsey
Tags: assembly, calling, function
 P: 177 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.
 HW Helper P: 6,756 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.
P: 177
 Quote by rcgldr 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?

HW Helper
P: 6,756

## x86 Assembly Calling Another Function

 Quote by whitehorsey 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 by whitehorsey 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);
}
P: 177
 Quote by rcgldr 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 by rcgldr 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?
HW Helper
P: 6,756
 Quote by whitehorsey 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 by whitehorsey 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 by whitehorsey 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.
P: 177
 Quote by rcgldr 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 by rcgldr 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!!

 Related Discussions Engineering, Comp Sci, & Technology Homework 4 Engineering, Comp Sci, & Technology Homework 4 Programming & Computer Science 2 Programming & Computer Science 7 Engineering, Comp Sci, & Technology Homework 2