# AW: [Oberon] DIV and MOD

Stauber, Sven Philipp staubesv at student.ethz.ch
Sat Aug 9 01:56:35 CEST 2003

```Hi!

According to "The Programming Language Oberon (Report)" the behaviour of DIV and MOD (for any dividend x and a positive divisor) should be:

x = (x DIV y)*y + (x MOD y)

0 <= (x MOD y) < y

The IA32 IDIV operation has a different behaviour. The division rest always has the same sign as the dividend (x IDIV y always rounds to zero, in contrast to the Oberon DIV which always rounds down).

Example:

Oberon:    -5 DIV 3 = -2,     -5 MOD 3 = 1    ->    -5 = -2*3 + 1

IA32:     -5 DIV 3 = -1,      -5 MOD 3 = -2    ->    -5 = -1*3 + -2

If EDX is negative, the dividend is also negative and then DEC EAX is used to "round down" the negative quotient which was rounded to zero by IDIV. The IA32 code pattern for the Oberon MOD operation should look similar (if EDX<0 then EDX = ABS(EDX)-1 else EDX ).

Regards,

Sven

> Hi
>
> I recently noticed that the compiler (at least the 2.3.8 Linux version for
> Intel) now attempts to compensate for fractions when performing signed
> integer division, adjusting the result returned by the CPU. This requires
> a minimum of three additional instructions and can create problems when
> routines are written in assembler since the result will deviate in some
cases
> from what Oberon says the answer should be. Does anyone know what the
> reasoning behind this change was?
>
> Previously, something like x DIV y (with x, y: LONGINT) would result in
>
> MOV EAX, x[EBP]
> CDQ
> IDIV y[EBP]
>
> but now, the compiler generates
>
> MOV EAX, x[EBP]
> CDQ
> IDIV y[EBP]
> CMP EDX, 0
> JGE done
> DEC EAX
> done:
>
> Regards
>   Jacques Eloff
> --
> Oberon at inf.ethz.ch mailing list for ETH Oberon and related systems
> http://www.lists.inf.ethz.ch/mailman/listinfo/oberon
>
>

--
Oberon at inf.ethz.ch mailing list for ETH Oberon and related systems
http://www.lists.inf.ethz.ch/mailman/listinfo/oberon

```