Skip to content

Add NASM - x64 #307

Description

@DutchOrange-1

Language Name

Assembly - X64

Docker Image

Locally run with NASM

Sample Implementation

; V1 - NASM 
; Takes about 9.774s - Note, my machine takes 600s to run a python script of the same formula. 
; Very much un-optomised, will fix in the comming days
; Result is just printed to the output. 
; Ran on Linux mint

extern printf
global main

section .data
    itterations  dd     1000000000    ; Number of itterations
    denom   dd 0        ;Denominator temp  ;esi
    nume     dd 0       ;Numerator temp   ;edi
    temp    dd 0.0
    result  dq 0.0696969   ; Result 
    four    dd 4.0 ; Last caculation
    fmt db "Result = %.16f", 10, 0

section .text


main:
    mov rbp, rsp; for correct debugging


    mov rbp, rsp; for correct debugging
    mov rbp, rsp; for correct debugging
    mov rcx , [itterations] ; How many counts
    xor rax, rax            ; Just clearing it. 
    xor rdx, rdx
    xorps xmm0, xmm0
    xorps xmm1, xmm1
    xorps xmm2, xmm2
    
    
    loopy:
    ; First do numerator:
    mov  rax, 1
    CALL even_test ; Numerator updates - rdi
    
    ; Now Denominator: rsi
    mov  rsi, rdx   ; Move k into rax
    add  rsi, rsi   ; Multiply by 2
    add  rsi, 1     ; Add one
    
    ; Now Combine them esi/edi
    ;fld  dword [esi]  ; Move to ST0 
    cvtsi2sd xmm0, rdi     ; xmm0 = float(esi) ; rdi = numerator
    cvtsi2sd xmm1, rsi     ; xmm1 = float(edi) ; rsi = denominator 
    
    divsd xmm0, xmm1       ; xmm0 = xmm0 / xmm1 ; s-single, d-double
    addpd xmm2, xmm0       ; Sum them
    
    ; Logic code
    inc rdx  ; Used for caculations - k
    dec rcx  ; Used to exit
    jnz loopy
    jmp Print_and_exit
    
    
Print_and_exit:
    push rbp
    movsd xmm0, xmm2
    addpd xmm0, xmm0 
    addpd xmm0, xmm0  
    mov rdi, fmt
    mov rax, 1
    call printf      ; Call the C library function
    
    xor rax, rax     ; Return 0 from main
    pop rbp          ; Restore stack
    ret
    
power_loop:             ; Multiplies ST0 with ST1, and stores in ST0 
  FMUL ST0, ST1
  dec   rcx
  jnz   power_loop ; Jump back 
  ret
  
 even_test: ; Insted of (-1^)x, it can be simplified 
     AND rax, rdx ; 0 = even , 1 = odd
     jz  make_pos
     jmp make_neg
 
     make_neg:
     mov rdi, -1
     ret
     
     make_pos:
     mov rdi, 1
     ret

Why add this language?

Its the foundation of them all, and should be made #1

Implementation Rules

  • Single-threaded (no concurrency/parallelism)
  • Uses the Leibniz formula for calculating pi
  • Reads rounds from rounds.txt
  • Outputs pi with 16 decimal places

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions