X86 Assembly Calling Another Function

In summary: 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
  • #1
whitehorsey
192
0
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.
 
Physics news on Phys.org
  • #2
Seems like there's missing code from this. It appears the goal is a recursive version of factorial, but the factorial function 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:
  • #3
rcgldr said:
Seems like there's missing code from this. It appears the goal is a recursive version of factorial, but the factorial function 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?
 
  • #4
whitehorsey said:
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

whitehorsey said:
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:

Code:
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:
  • #5
rcgldr said:
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?

rcgldr said:
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:
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?
 
  • #6
whitehorsey said:
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.

whitehorsey said:
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.

whitehorsey said:
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.
 
  • #7
rcgldr said:
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!

rcgldr said:
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! :smile:
 

1. What is X86 assembly?

X86 assembly is a low-level programming language used to write instructions that can be directly executed by a computer's central processing unit (CPU). It is the most common type of assembly language used in Intel and AMD processors.

2. What does it mean to call another function in X86 assembly?

Calling another function in X86 assembly means transferring control from one function to another within a program. This allows for modular programming and the ability to reuse code.

3. How do you call another function in X86 assembly?

To call another function in X86 assembly, you must first push any required parameters onto the stack and then use the CALL instruction followed by the name of the function. The called function will then execute and return control to the calling function using the RET instruction.

4. What is the difference between a near call and a far call in X86 assembly?

In X86 assembly, a near call is used to call a function within the same code segment, while a far call is used to call a function in a different code segment. A far call requires the use of a segment register to specify the code segment, while a near call does not.

5. What are some best practices for calling functions in X86 assembly?

Some best practices for calling functions in X86 assembly include properly managing the stack, preserving important registers, and following the calling convention specific to your compiler. It is also important to properly handle any return values or errors from the called function.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
13
Views
13K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
5K
  • Programming and Computer Science
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top