flat assembler
Message board for the users of flat assembler.

 Index > Heap > Leibniz Pi program in Alpha Assembly
Author
mattst88

Joined: 12 May 2006
Posts: 260
Location: South Carolina
mattst88
The Leibniz Pi algorithm is an algorithm I've written in many different languages, and I have learned a quite a bit from it.

Wikipedia article: http://en.wikipedia.org/wiki/Leibniz_formula_for_pi

My leibniz code (C, x86 FPU, SSE3) is here: http://mattst88.com/programming/?page=leibniz

Since I've got an Alpha, I figured I'd study RISC programming and write the leibniz algorithm in alpha assembly. So here it is, if anyone is interested whatsoever.

By the way, here's a page about my Alphaserver DS20L. http://mattst88.com/ds20l/

(GAS Syntax)

Code:
```STACK = 1
FRAME = ((STACK*8+8)/16)*16

.data
F_ONE:    .t_floating     1.0
PRINT: .ascii          "1/%.0f - %19.17f\n"

.text
.align  4
.set    noreorder
.arch   ev6
.globl  main
.ent    main

main:   ldgp    \$gp,0(\$27)
lda     \$sp,-FRAME(\$sp)
stq     \$26,0(\$sp)
.frame  \$sp,FRAME,\$26,0
.prologue 1
.globl  first
first:     ldt     \$f2,F_ONE               # \$f2 num = 1.0
cpyse   \$f2,\$f2,\$f3             # \$f3 sum = 1.0
ldq     \$9,ONE                  # \$9 = 1
addt    \$f2,\$f2,\$f4             # \$f4 incrementor = 2.0
ldq     \$10,COUNT               # \$10 = counter
addt    \$f4,\$f3,\$f5             # \$f5 denom = 3.0
mov     \$10,\$2                  # load counter into \$2

.align  4
loop:  cpysn   \$f2,\$f2,\$f2             # num = -num
divt    \$f2,\$f5,\$f10            # temp = num / denom
addt    \$f3,\$f10,\$f3            # sum += temp
addt    \$f5,\$f4,\$f5             # denom += 2.0

subq    \$2,\$9,\$2                # counter -= 1

bne     \$2,loop                 # if counter == 0, loop

lda     \$16,PRINT               # load format string
cpyse   \$f5,\$f5,\$f17            # \$f17 = denom
cpyse   \$f3,\$f3,\$f18            # \$f18 = sum
jsr     \$26,printf              # call printf
ldgp    \$gp,0(\$26)              # reload global pointer

mov     \$10,\$2                  # reset counter

done:     mov     \$31,\$0                  # return val = 0
ldq     \$26,0(\$sp)
lda     \$sp,FRAME(\$sp)
ret     \$31,(\$26),1             # return
.end    main    ```

_________________
My x86 Instruction Reference -- includes SSE, SSE2, SSE3, SSSE3, SSE4 instructions.
Assembly Programmer's Journal
26 Apr 2008, 00:51
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 17278
revolution
When will we see fasmalpha?
26 Apr 2008, 01:12
mattst88

Joined: 12 May 2006
Posts: 260
Location: South Carolina
mattst88
revolution wrote:
When will we see fasmalpha?

I don't know the FASM internals that well, so there'd be a significant learning curve. I assume if I ever do attempt to do it, I could use your FASMARM as a base though, right?

_________________
My x86 Instruction Reference -- includes SSE, SSE2, SSE3, SSSE3, SSE4 instructions.
Assembly Programmer's Journal
26 Apr 2008, 01:15
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 17278
revolution
mattst88 wrote:
I assume if I ever do attempt to do it, I could use your FASMARM as a base though, right?
Sure, that would be good. It would be nice not to have to see the ugly GAS syntax.
26 Apr 2008, 01:46
AlexP

Joined: 14 Nov 2007
Posts: 561
Location: Out the window. Yes, that one.
AlexP
matts88: You actually posted your site in the Wikipedia article. Wow. Nice code though.
26 Apr 2008, 02:49
mattst88

Joined: 12 May 2006
Posts: 260
Location: South Carolina
mattst88
AlexP wrote:
matts88: You actually posted your site in the Wikipedia article.

Very true. I figure it's quite relevant though.

_________________
My x86 Instruction Reference -- includes SSE, SSE2, SSE3, SSSE3, SSE4 instructions.
Assembly Programmer's Journal
26 Apr 2008, 03:35
bitRAKE

Joined: 21 Jul 2003
Posts: 2914
Location: [RSP+8*5]
bitRAKE
Code:
```mov ebx,0       ; sum
mov ebp,\$170000 ; itterations/2
mov ecx,3       ; denominator

.0: mov edx,1
mov eax,0
div ecx
sub ebx,eax

mov edx,1
mov eax,0
div ecx

dec ebp
jne .0    ```
...fixed point version. EBX ---> pi/4 * 2^32

There is a lot of rounding error, but lots of good bits too.

_________________