Need help converting c code to something Borland can handle

From: Andrew Kennedy (andrewkennedy2_at_LOGev1.net)
Date: 02/19/04


Date: Thu, 19 Feb 2004 22:46:05 -0000


I would like to convert this for use with my Borland compiler
which is little old. :-)

I have 3.1 which is rather old, that may be part of the problem
as this uses 32 bit registers.

There is bound to be a newer stdio.h include file as well I may need
to download.

Borland is saying it expects a comma on the first typedef.

/* undiv.c

Program to determine algorithm, multiplier, and shift factor to be
used to accomplish unsigned division by a constant divisor. Compile
with MSVC.
*/
#include <stdio.h>
typedef unsigned __int64 U64;
typedef unsigned long U32;
U32 log2 (U32 i)
{
U32 t = 0;
i = i >> 1;
while (i) {
i = i >> 1;
t++;
}
return (t);
}
U32 res1, res2;
U32 d, l, s, m, a, r, n, t;
U64 m_low, m_high, j, k;
int main (void)
{
fprintf (stderr, "\n");
fprintf (stderr, "Unsigned division by constant\n");
fprintf (stderr, "=============================\n\n");
fprintf (stderr, "enter divisor: ");
scanf ("%lu", &d);
printf ("\n");
if (d == 0) goto printed_code;
if (d >= 0x80000000UL) {
printf ("; dividend: register or memory location\n");
printf ("\n");
printf ("CMP dividend, 0%08lXh\n", d);
printf ("MOV EDX, 0\n");
printf ("SBB EDX, -1\n");
printf ("\n");
printf ("; quotient now in EDX\n");
goto printed_code;
}
/* Reduce divisor until it becomes odd */
n = 0;
t = d;
while (!(t & 1)) {
t >>= 1;
n++;
}
if (t==1) {
if (n==0) {
printf ("; dividend: register or memory location\n");
printf ("\n");
printf ("MOV EDX, dividend\n", n);
printf ("\n");
printf ("; quotient now in EDX\n");
}
else {
printf ("; dividend: register or memory location\n");
printf ("\n");
printf ("SHR dividend, %d\n", n);
printf ("\n");
printf ("; quotient replaced dividend\n");
}
goto printed_code;
}
/* Generate m, s for algorithm 0. Based on: Granlund, T.; Montgomery,
P.L.: "Division by Invariant Integers using Multiplication".
SIGPLAN Notices, Vol. 29, June 1994, page 61.
*/
l = log2(t) + 1;
j = (((U64)(0xffffffff)) % ((U64)(t)));
k = (((U64)(1)) << (32+l)) / ((U64)(0xffffffff-j));
m_low = (((U64)(1)) << (32+l)) / t;
m_high = ((((U64)(1)) << (32+l)) + k) / t;
while (((m_low >> 1) < (m_high >> 1)) && (l > 0)) {
m_low = m_low >> 1;
m_high = m_high >> 1;
l = l - 1;
}
if ((m_high >> 32) == 0) {
m = ((U32)(m_high));
s = l;
a = 0;
}
/* Generate m, s for algorithm 1. Based on: Magenheimer, D.J.; et al:
"Integer Multiplication and Division on the HP Precision Architecture".
IEEE Transactions on Computers, Vol 37, No. 8, August 1988, page 980.
*/
else {
s = log2(t);
m_low = (((U64)(1)) << (32+s)) / ((U64)(t));
r = ((U32)((((U64)(1)) << (32+s)) % ((U64)(t))));
m = (r < ((t>>1)+1)) ? ((U32)(m_low)) : ((U32)(m_low))+1;
a = 1;
}
/* Reduce multiplier for either algorithm to smallest possible */
while (!(m&1)) {
m = m >> 1;
s--;
}

/* Adjust multiplier for reduction of even divisors */
s += n;
if (a) {
printf ("; dividend: register other than EAX or memory location\n");
printf ("\n");
printf ("MOV EAX, 0%08lXh\n", m);
printf ("MUL dividend\n");
printf ("ADD EAX, 0%08lXh\n", m);
printf ("ADC EDX, 0\n");
if (s) printf ("SHR EDX, %d\n", s);
printf ("\n");
printf ("; quotient now in EDX\n");
}
else {
printf ("; dividend: register other than EAX or memory location\n");
printf ("\n");
printf ("MOV EAX, 0%08lXh\n", m);
printf ("MUL dividend\n");
if (s) printf ("SHR EDX, %d\n", s);
printf ("\n");
printf ("; quotient now in EDX\n");
}
printed_code:
fprintf(stderr, "\n");
exit(0);
return(0);
}
Thanks,
          Andrew Kennedy

"It's the start that stops most people."

andrewkennedy2@ev1LOG.net
To respond by email remove the LOG from the address.



Relevant Pages

  • Re: what is wrong with this little piece of code?
    ... Sys_write wants the length in edx. ... If you're allowed to call libraries, ... If you're expected to write your own "ftoa", ... If that's more than you wanted to know, "just call printf". ...
    (comp.lang.asm.x86)
  • Re: x86-64 assembly problems
    ... it moves it into a register like RDI or RSI and calls printf. ... The `printf` function expects a _pointer_ to a format string on ... does not need the use of the stack. ...
    (alt.lang.asm)
  • Re: RE: Reboot on "shutdown -r" hangs after final "uptime ..." string
    ... I've got a printf at the end of ehci_pci_givecontroller that never gets ... I wonder if the register write is triggering an SMI, ... the CPU off into the weeds. ...
    (freebsd-current)