CS 3723
 Programming Languages 
   MIPS Example:  
  Euler Series  


Calculate Euler's Series

The following page contains code in C, Tiny®, and MIPS assembly code to calculate the sum of the series: Σ 1/(n^2). The great Swiss mathematician Leonhard Euler proved that this series converges (slowly) to π2/6 = 1.644934066848226

C, Tiny, Output MIPS Assembly Code
#include <stdio.h>
int main() {
   double n, s, m;
   n = 1; s = 0;
   scanf("%lf", &m);
   while (n - m != 0) {
      s = s + 1/(n*n*n*n);
      printf("%.0f\t%.17f\n", n, s);
      n = n + 1;
   }
}

# euler.t: Euler series n = 1; s = 0; > m; { n - m ? s = s + 1/(n*n); < n; < T; < s; < N; n = n + 1; } $
Locations of vars, consts: 1: M[1] 0: M[0] n: M[23] s: M[28] M[36], ... are temporaries
% which spim /usr/bin/spim % spim euler.s 50 1 1.00000000000000000 2 1.25000000000000000 3 1.36111111111111116 4 1.42361111111111116 5 1.46361111111111120 6 1.49138888888888888 7 1.51179705215419502 8 1.52742205215419502 9 1.53976773116654075 10 1.54976773116654076 11 1.55803219397645809 12 1.56497663842090251 13 1.57089379818421615 14 1.57599583900054263 15 1.58044028344498710 16 1.58434653344498710 17 1.58780674105744390 18 1.59089316081053034 19 1.59366324391302339 20 1.59616324391302333 21 1.59843081760916839 22 1.60049693331164766 23 1.60238729247988965 24 1.60412340359100081 25 1.60572340359100085 26 1.60720269353182932 27 1.60857443564431213 28 1.60984994584839369 29 1.61103900649048648 30 1.61215011760159754 31 1.61319070032792422 32 1.61416726282792422 33 1.61508553647347064 34 1.61595058837658478 35 1.61676691490719704 36 1.61753851984546859 37 1.61826898003538822 38 1.61896150081101142 39 1.61961896300693509 40 1.62024396300693518 41 1.62083884700455561 42 1.62140574042859198 43 1.62194657331123127 44 1.62246310223685120 45 1.62295692939734493 46 1.62342951918940548 47 1.62388221271588806 48 1.62431624049366574 49 1.62473273362152915
### Compiled on: Tue Sep 24, 2013
main:   addu    $s7, $ra, $zero
# addr of M: constants, vars, temps
        la      $s1, M
### Start of compiled code
# M[23] = M[1]
        l.d     $f2, 8($s1)
        s.d     $f2, 184($s1)
# M[28] = M[0]
        l.d     $f2, 0($s1)
        s.d     $f2, 224($s1)
# Read M[22] as double
        li      $v0, 7
        syscall
        s.d     $f0, 176($s1)
WhileStart0:
# M[36] = M[23] - M[22]
        l.d     $f2, 184($s1)
        l.d     $f4, 176($s1)
        sub.d   $f6, $f2, $f4
        s.d     $f6, 288($s1)
        l.d     $f2, 288($s1)
        l.d     $f4, 0($s1)
        c.eq.d  $f2, $f4
        bc1t    WhileEnd0
# M[37] = M[23] * M[23]
        l.d     $f2, 184($s1)
        l.d     $f4, 184($s1)
        mul.d   $f6, $f2, $f4
        s.d     $f6, 296($s1)
# M[38] = M[1] / M[37]
        l.d     $f2, 8($s1)
        l.d     $f4, 296($s1)
        div.d   $f6, $f2, $f4
        s.d     $f6, 304($s1)
# M[39] = M[28] + M[38]
        l.d     $f2, 224($s1)
        l.d     $f4, 304($s1)
        add.d   $f6, $f2, $f4
        s.d     $f6, 312($s1)
# M[28] = M[39]
        l.d     $f2, 312($s1)
        s.d     $f2, 224($s1)
# Print M[23]
        li      $v0, 3
        l.d     $f12, 184($s1)
        syscall
# Print Tab as ASCII char
        li      $v0, 4
        la      $a0, Tab
        syscall
# Print M[28]
        li      $v0, 3
        l.d     $f12, 224($s1)
        syscall
# Print NewL as ASCII char
        li      $v0, 4
        la      $a0, NewL
        syscall
# M[40] = M[23] + M[1]
        l.d     $f2, 184($s1)
        l.d     $f4, 8($s1)
        add.d   $f6, $f2, $f4
        s.d     $f6, 320($s1)
# M[23] = M[40]
        l.d     $f2, 320($s1)
        s.d     $f2, 184($s1)
        j       WhileStart0
WhileEnd0:
### End of complied code
        addu    $ra, $s7, $zero
        jr      $ra
        .data
        .align  3
M:      .double 0.,1.,2.,3.,4.,5.,6.
        .double 7.,8.,9. # constants
        .space  208  # variables a to z
        .space  1000 # 125 temporaries
Blank:  .asciiz " "
NewL:   .asciiz "\n"
Tab:    .asciiz "\t"
### End of MIPS source


Revision date: 2013-09-24. (Please use ISO 8601, the International Standard.)