# Homework Help: X86 Assembly Calling Another Function

1. Nov 25, 2012

### whitehorsey

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.

2. Nov 25, 2012

### 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.

Last edited: Nov 25, 2012
3. Nov 25, 2012

### whitehorsey

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?

4. Nov 25, 2012

### 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

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:

Code (Text):

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);
}

Last edited: Nov 25, 2012
5. Nov 26, 2012

### 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?

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?

6. Nov 26, 2012

### 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.

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.

7. Nov 27, 2012

### whitehorsey

Ooooh I see!!

Okay! Thank You!!