Math Is Fun Forum
  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2007-03-07 04:39:08

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,562

Machine Code; Assembly Language

Anyone try programming assembly language
for the math processor in the Pentium?? (x87 part I guess)
I'm starting to learn about it.
The purpose of this is to be able to compute trig in the
fastest possible language known to the PC.
I'm using FASM, FASMW (windows).
I modified the MINIPAD example using settextwindow or
something to display my results.
So far I think I have access to at least 9MB, have to
look more into it.
Bye.


igloo myrtilles fourmis

Offline

#2 2007-03-07 04:46:44

luca-deltodesco
Member
Registered: 2006-05-05
Posts: 1,470

Re: Machine Code; Assembly Language

all processors nowadays have hardware level trig functions tongue even programming at the machine level wouldnt give a faster trig function


The Beginning Of All Things To End.
The End Of All Things To Come.

Offline

#3 2007-03-07 04:59:21

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: Machine Code; Assembly Language

and good thing too....


A logarithm is just a misspelled algorithm.

Offline

#4 2007-03-07 05:11:44

Ricky
Moderator
Registered: 2005-12-04
Posts: 3,791

Re: Machine Code; Assembly Language

I myself have programmed in MASM (Microsoft Assembly) and MIPS (used for various things such as gaming consoles, although not in computers).

The purpose of this is to be able to compute trig in the
fastest possible language known to the PC
.

That's not exactly correct.  Every thing that runs on the computer, whether it be programmed in Java, Lisp, C++, or Whitespace, runs with machine code.

However, the translation from something such as C++ code to machine code may be inefficient, and by programming directly in assembly (which is machine code, for all practical purposes) you may be able to write the code more efficiently.

Typically the most you can do is reduce the number of times variables need to be loaded for flushed out of the registers.  This is one of the longest operations of the CPU, so it can speed up a program a great bit.

As for trig functions, you have the potential to speed them up in assembly.  I've known others who have somehow gotten access to the graphics card through assembly, and were able to use it as a second processor.  For those who are not aware, the graphics card is like its own specialized processor.  It has it's out PU (processing unit) and memory.

Perhaps the trig functions on a GPU are much more specialized than those on the CPU.  After all, the amount of trig functions required in graphics is amazing.


"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

Offline

#5 2007-03-07 05:30:32

Dross
Member
Registered: 2006-08-24
Posts: 325

Re: Machine Code; Assembly Language

I learnt a bit of assembly - if you're learning just to do things quickly though, it'll be a struggle. Now-a-days you'll have to be a very good assembly programmer if you want to beat a modern C-compiler that optimises well. Also, the maths coprocessor is out-dated, going back to the times when it was a kind of "add-on" that allowed programmers to make faster programs, but only if you had the coprocessor. Today all the functions of a coprocessor are built into the CPU.


Bad speling makes me [sic]

Offline

#6 2007-03-07 13:45:40

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,562

Re: Machine Code; Assembly Language

Neat comments!!
Here's my first tiny program and output.
fldpi
fstp tword[jf_print_area]
fwait

Here's the output:
5Âh!¢ÚÉ

Now I have to do some more learning...


igloo myrtilles fourmis

Offline

#7 2007-03-09 03:10:42

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,562

Re: Machine Code; Assembly Language

Even though I am trying to do 80-bit floats,
I found this awesome applet at the bottom of this web page: http://www.randelshofer.ch/fhw/gri/float.html#chapterfloatingpointformat
This java applet exposes the intricate workings of the bits and lets you type in different
values for numbers and see the corresponding bit layout.
So I'm getting somewhere...


igloo myrtilles fourmis

Offline

#8 2007-03-09 09:52:37

MathsIsFun
Administrator
Registered: 2005-01-21
Posts: 7,535

Re: Machine Code; Assembly Language

I played around with assembly some years ago. Speed is insane, but so is the programming! Took me ages to get a simple thing working. I went back to "C".


"The physicists defer only to mathematicians, and the mathematicians defer only to God ..."  - Leon M. Lederman

Offline

#9 2007-03-10 05:19:36

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,562

Re: Machine Code; Assembly Language

If you get sick of these postings, just tell me and I won't do it.
Here's the next step, to break the ASCII cryptic codes into HEX for examination.
Here's the HEX result for the ASCII pi code of 5Âh!¢ÚÉ

;;output was 35C26821A2DA0FC900405Âh!¢ÚÉ
;;The twenty Hex digits represent the 80 bit code for pi, though it might be
;;backwards, or mixed up a little still.

fldz
fstp tword[jf_print_area + 30]

fldpi
fstp tword[jf_print_area + 20]
fwait

mov ebx,0
@@:
mov al,[jf_print_area + 20 + ebx]
mov cl,al
and al,0x0F
or al,0x30
cmp al,0x39
jbe jf_skip_09hex_notAtoF_AAAD
add al,7 ;;because of ASCII chart 0x3A --> 0x41, letter A (numbers and letters are not contiguous like HEX)
jf_skip_09hex_notAtoF_AAAD:
shr cl,4
and cl,0x0F
or cl,0x30
cmp cl,0x39
jbe jf_skip_hex_again_9A_border_AAAD
add cl,7
jf_skip_hex_again_9A_border_AAAD:
mov [jf_print_area + ebx + ebx],cl
mov [jf_print_area + ebx + ebx + 1],al
inc ebx
cmp ebx, 9
jbe @B

;;output was 35C26821A2DA0FC90040   5Âh!¢ÚÉ

Last edited by John E. Franklin (2007-03-10 05:21:59)


igloo myrtilles fourmis

Offline

#10 2007-03-13 12:23:58

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,562

Re: Machine Code; Assembly Language

I found a way to use the BCD integer
conversion from floating point
commands to display the pi number.
I can get at 18 digits this way.
The actual number of digits in
the constant may be perhaps 19 or 20,

I'm not sure, but this is the
best way I have found so far to
get the number in human readable
form.

Here is pi, shown backwards as
it came into memory on my PC:

24 93 97 58 53 26 59 41 31

Here it turned around.
314159265358979324

I multiplied pi by
one-thousand, five times and
then multiplied in by ten,
twice.  I did that because
the "fbstp" command for
conversion to BCD (binary
coded decimal) rounds the
number to a whole number, so
I would have lost the fractional
part if I didn't move the
decimal point over first.
Also, the "fbld" command I learned
to use to load in an integral
number of 18 digits in length,
which I use for the constants
of value 1000, and 10.

Here is some of the code to enjoy reading
that displayed the 18 digits of pi
backwards like this:
24 93 97 58 53 26 59 41 31

 jf_thousand db 0x00,0x10,0,  0,0,0,  

0,0,0,  0,0,0,  0,0,0,  0,0,0,   0,0,0   

;;bcd is 18 bytes long; I have 21 bytes 

here to make sure.
       ;;                                

                                    

;;This is low-nibble first, hence 3 

zeros and a 1 for 1000.

 jf_ten db 0x10,0,0,  0,0,0,   0,0,0,  

0,0,0,  0,0,0,  0,0,0,  0,0,0  ;21 

bytes, only need 18 according to 

documentation.


section '.code' code readable executable

  start:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

fwait
fninit
fwait


fldpi
fbld tword[jf_thousand]
fmulp
fbld tword[jf_thousand]
fmulp
fbld tword[jf_thousand]
fmulp
fbld tword[jf_thousand]
fmulp
fbld tword[jf_thousand]
fmulp
fbld tword[jf_ten]
fmulp
fbld tword[jf_ten]
fmulp


fbstp tword[jf_print_area + 20]
fwait

;;;;Below loop makes the number human 

readable
;;;;by enlarging each nibble or half-

byte into
;;;;a whole byte character '0'..'9' and 

'A'..'F'
;;;;Note for BCD, only '0' to '9' 

appear.
mov ebx,0
@@:  ;;beginLOOPHEXupNUMat20
mov al,[jf_print_area + 20 + ebx]
mov cl,al
and al,0x0F
or al,0x30
cmp al,0x39
jbe jf_skip_09hex_notAtoF_AAAD
add al,7 ;;because of ASCII chart 0x3A 

--> 0x41, letter A (numbers and letters 

are not contiguous like HEX)
jf_skip_09hex_notAtoF_AAAD:
shr cl,4
and cl,0x0F
or cl,0x30
cmp cl,0x39
jbe jf_skip_hex_again_9A_border_AAAD
add cl,7
jf_skip_hex_again_9A_border_AAAD:
mov [jf_print_area + ebx + ebx],cl
mov [jf_print_area + ebx + ebx + 1],al
inc ebx
cmp ebx, 9
jbe @B ;;endLOOPHEXupNUMat20

igloo myrtilles fourmis

Offline

Board footer

Powered by FluxBB