[A89] Re: floting point numbers, arguments, etc...


[Prev][Next][Index][Thread]

[A89] Re: floting point numbers, arguments, etc...




In the TIGCC documentation, check the page "Important Information for Assembly Programmers". It talks a bit about floats. Specifically, you need to use the BCD structure (except using the offsets manually b/c it's ASM and not C):

 

All TIOS functions which return a floating point value (like most functions from timath.h header file) expect that before calling A6 points to the byte after the end byte of 10-byte long buffer where the result will be stored (usually this is a preallocated space on the stack frame). Note that this is not the same as GCC convention for returning floating point values: GCC returns floats in a triplet [D0.l;D1.l;D2.w]. That's why implementing floating point routines was not so easy, especially because A6 is a very important register (used in GCC as the frame pointer).
and

Suppose that you want to calculate the integer part of 2.34*log(342.1178). First, you need to know that hexadecimal representations for 2.34 and 342.1178 are $40002340000000000000 and $40023421178000000000 (see bcd if you don't know why). Also, note that bcdmul and bcdlong are original TIOS names for functions aliased as fmul and trunc (don't be misleaded by the fact that the library defines fmul & trunc to work with native float type and bcdmul & bcdlong to work with bcd structures; at the fundamental ASM level they are exactly the same routines). Then, this calculation may be performed using the following ASM program: 

       lea    after(pc),a6         ; Prepare a6 for storing results       move.l #$40023421,-(sp)     ; Push 342.1178       move.l #$17800000,-(sp)       clr.w  -(sp)       jsr    tios::log            ; The result is now in "temp"       move.l (a6,-4),-(sp)        ; Push the result       move.l (a6,-8),-(sp)       move.w (a6,-10),-(sp)       move.l #$40002340           ; Push 2.34       clr.l  -(sp)       clr.w  -(sp)       jsr    tios::bcdmul         ; The result is again in "temp"       move.l (a6,-4),-(sp)        ; Push the result again       move.l (a6,-8),-(sp)       move.w (a6,-10),-(sp)       jsr    tios::bcdlong        ; The final result is now in d0       lea    (sp,40),sp           ; Clean up the stack       ...temp:  ds.b   10                   ; Ten-byte bufferafter: ...

This program may be much more optimized if you know how to use stack frames properly (this technic is so popular in high-level language compilers, but quite unpopular in ASM programs; this example shows that stack frames may be very useful). The optimized version of the same program follows: 

       link   a6,#-10              ; Create 10-bytes long space on the stack       move.l #$40023421,-(sp)     ; Push 342.1178       move.l #$17800000,-(sp)       clr.w  -(sp)       jsr    tios::log            ; The result is on the stack frame       lea    (sp,10),sp           ; Adjust the stack pointer       move.l #$40002340           ; Push 2.34       clr.l  -(sp)       clr.w  -(sp)       jsr    tios::bcdmul         ; The result is again on the stack frame       lea    (sp,10),sp           ; Adjust the stack pointer again       jsr    tios:bcdlong         ; The final result is now in d0       unlk   a6                   ; Remove the stack frame



---------------------------------
Do You Yahoo!?
HotJobs, a Yahoo! service - Search Thousands of New Jobs




Follow-Ups: References: