How to do it…

To divide two numbers using assembly code in C, perform the following steps:

  1. Load the dividend into the eax register.
  2. Load the divisor into the ebx register.
  3. Initialize the edx register to zero.
  4. Execute the divl assembly statement to divide the content of the eax register by the content of the ebx register. By doing this division, the quotient will be assigned to any available register and the remainder will be assigned to the ebx register.
  5. The quotient is retrieved from the available register and the remainder is retrieved from the ebx register, and both are displayed on the screen.

The program for dividing two digits using inline assembly code is as follows:

asmdivide.c
#include <stdio.h>
void main() {
int var1=19,var2=4, var3=0, remainder, quotient;
asm("divl %%ebx;"
"movl %%edx, %0"
: "=b" (remainder) , "=r" (quotient)
: "a" (var1), "b" (var2), "d" (var3)
);
printf ("On dividing %d by %d, you get %d quotient and %d remainder\n", var1, var2, quotient, remainder);
}

Now, let's go behind the scenes.