Random Fun with NASM

Posted by hank, Wed Nov 21 22:45:00 UTC 2007

I was on IRC tonight and someone was having trouble with their NASM homework. I decided to help them by learning NASM and coding up their homework problem for fun. It was pretty cool. Here’s the resultant program:


; Goal: (a+b)*c+d*b
; PARI/GP says:
; ? (1.234+5.678)*9.012+3.456*5.678
; %1 = 81.91411200000000000000000000
;
;=== Output ===
;hank@rofl:/tmp$ nasm -felf floating_point_arith.asm && \
; gcc floating_point_arith.o && ./a.out
;Answer: 81.91411
;===
;QED

  extern printf

  section .data
  a: dq 1.234
  b: dq 5.678
  c: dq 9.012
  d: dq 3.456
  e: dq 7.890
  fmt: db "Answer: %.5f",10,0

  section .bss
  f: resq 1

  section .text

  global main
  main:
    fldz
    fld   qword [a]
    fadd  qword [b]
    fmul  qword [c]
    fstp  qword [f]

    fld   qword [d]
    fmul  qword [b]
    fadd  qword [f]
    fstp  qword [f]

    push  dword [f+4]
    push  dword [f]
    push  dword fmt
    call  printf
    add   esp, 12
    mov   eax, 0
    ret

Code is also available here:


http://modzer0.cs.uaf.edu/repos/hank/code/asm/floating_point_arith.asm

All this does is add some floats and call printf, but it was fun to monkey around low-level again.

Tags:

Comments

Have your say

A name is required. You may use Markdown in your comments.