CS641 Floating Point Example: Convert Fahrenheit to Celcius,

using double, not float (vs. P&H, pg. 262)

sf06.cs.umb.edu$ more convert.c

double f2c(double fahr)

{

    return (5.0/9.0)* (fahr - 32.0);

}

sf06.cs.umb.edu$ mips-gcc -S convert.c

sf06.cs.umb.edu$ more convert.s

        .file   1 "convert.c"

        .section .mdebug.abi32

        .previous

        .abicalls

        .rdata

        .align  3

$LC0:                    <--- constant 32.0, in little-endian rep, so lower half first

        .word   0

        .word   1077936128

        .align  3

$LC1:                    <--- constant 5.0/9.0

        .word   1908874354  

        .word   1071761180

        .text

        .align  2

        .globl  f2c

        .ent    f2c

        .type   f2c, @function

f2c:

        .frame  $fp,8,$31               # vars= 0, regs= 1/0, args= 0, gp= 0

        .mask   0x40000000,-8

        .fmask  0x00000000,0

        .set    noreorder

        .cpload $25

        .set    reorder

        addiu   $sp,$sp,-8

        sw      $fp,0($sp)

        move    $fp,$sp

        swc1    $f12,8($fp)

        swc1    $f13,12($fp)

        lwc1    $f2,8($fp)

        lwc1    $f3,12($fp)

        lwc1    $f0,$LC0     <--no explicit use of $gp here (vs. optimized below)

                   but in fact this is a pseudo-instruction that expands to use $gp

                   entry value plus offset to set addr in at, then load using addr

        lwc1    $f1,$LC0+4

        sub.d   $f0,$f2,$f0

        lwc1    $f2,$LC1

        lwc1    $f3,$LC1+4

        mul.d   $f0,$f0,$f2

        move    $sp,$fp

        lw      $fp,0($sp)

        addiu   $sp,$sp,8

        j       $31

        .end    f2c

        .ident  "GCC: (GNU) 3.4.5"

 

How to see actual machine code:

sf06.cs.umb.edu$ mips-gcc -S convert.c

sf06.cs.umb.edu$ mips-as -o convert.o convert.s

sf06.cs.umb.edu$ mips-ld convert.o

mips-ld: warning: cannot find entry symbol __start; defaulting to 00000000004000b0

sf06.cs.umb.edu$ mips-objdump -D a.out        <---dumps machine code

 

sf06.cs.umb.edu$ mips-gcc-O2 -S convert.c

sf06.cs.umb.edu$ more convert.s

        .file   1 "convert.c"

        .section .mdebug.abi32

        .previous

        .abicalls

        .section        .rodata.cst8,"aM",@progbits,8

        .align  3

$LC0:

        .word   0

        .word   1077936128

        .align  3

$LC1:

        .word   1908874354

        .word   1071761180

        .text

        .align  2

        .globl  f2c

        .ent    f2c

        .type   f2c, @function

f2c:

        .frame  $sp,0,$31               # vars= 0, regs= 0/0, args= 0, gp= 0

        .mask   0x00000000,0

        .fmask  0x00000000,0

        .set    noreorder

        .cpload $25

        .set    nomacro

 

        lw      $2,%got($LC0)($28)    <--use of $gp sets up $2 for use in both loads

        nop                                (here $gp points to start of .rodata section)

        lwc1    $f0,%lo($LC0)($2)     <--load 1st half of 32.0

        nop

        lwc1    $f1,%lo($LC0+4)($2)   <--load 2nd half of 32.0

        lw      $2,%got($LC1)($28)

        sub.d   $f12,$f12,$f0

        lwc1    $f0,%lo($LC1)($2)     <--load 1st half of 5/9

        nop

        lwc1    $f1,%lo($LC1+4)($2)   <--load 2nd half of 5/9

        nop

        mul.d   $f0,$f12,$f0

        j       $31

        nop

 

        .set    macro

        .set    reorder

        .end    f2c

        .ident  "GCC: (GNU) 3.4.5"