Keil Logo

C51: 16-bit Multiply with 32-bit Result

Information in this article applies to:

  • C51 Version 5.50a or later
  • C51 Version 6.12 or later


Is it possible to multiply two 16-bit numbers (without casting them to long) and get a 32-bit result?


This is not accounted for in the C language specification.

However, the following assembler function does this math operation:


        PUBLIC  _uint_mult

        RSEG  ?PR?_uint_mult?MULT
; x stored in R6+R7
; y stored in R4+R5

          mov     a,r7
          mov     b,r5
          mul     ab      ; ab = R7*R5

          mov     r3,b
          xch     a,r7    ; a = R7

          mov     b,r4
          mul     ab      ; ab = R4*R7

          add     a,r3
          mov     r3,a
          mov     a,#0
          addc    a,b
          xch     a,r5    ; a = R5

          mov     b,r6
          mul     ab      ; ab = R5*R6

          add     a,r3
          xch     a,r6

          xch     a,b     ; b = R6

          addc    a,r5
          mov     r5,a
          mov     a,#0
          addc    a,#0
          xch     a,r4

          mul     ab      ; ab = R4*R6

          add     a,r5
          mov     r5,a
          mov     a,b
          addc    a,r4
          mov     r4,a

; Result returned in R4+R5+R6+R7



You must prototype this routine in your C code as follows:

unsigned long uint_mult (
  unsigned int x,
  unsigned int y);

and use it as follows:

unsigned long z;
unsigned int x, y;

unsigned long z1;

for (x = 0; x < 0xFF00; x += 13)
  for (y = 0; y < 0xFF00; y += 17)
    z1 = (unsigned long) x * y;
    z = uint_mult (x, y);

    if (z != z1)
      printf ("0x%4.4X x 0x%4.4X = ", x, y);
      printf ("0x%8.8LX (0x%8.8LX)\n", z, z1);

Last Reviewed: Friday, December 11, 2020

Did this article provide the answer you needed?
Not Sure
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.