Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

JAVA -> MIPS assembly

Name: Anonymous 2006-10-30 4:08

First post, curious as to what kind of response I'll get...

Basicaly, just wondering if anyone'd be willing to take a quick peek at this, and clue me in on a fix for the exception PCspim's throwing at me, or offer any usefull comments.

I realy can't see why that function is any different than what's in the higher-level language version. I'm still fairly new to the Computer Science scene (as you'll see below) so hopfully you'll be gentle...:

##################################################
#
#
    .text
    .globl main

main:

    li    $v0, 6        #
    syscall            # load float

    mov.s    $f12, $f0    #
    jal    sqroot        # test function

    mov.s    $f12, $f0    #
    li    $v0, 3        #
    syscall            # print results


    li    $v0, 10        #
    syscall            # exit program


# b in $f12
# ret in $f0 (x)
sqroot:
    li    $t0, 0        #
    mtc1    $t0, $f2    #
    cvt.s.w    $f16, $f2    # cast floating point zero

    mov.s    $f4, $f16    # old guess
    mov.s    $f0, $f16    # x
    mov.s    $f8, $f16    # difference

    c.eq.s    $f12, $f16    # if (b == 0)
    bc1t    return        #     return 0

    li    $t0, 2        #
    mtc1    $t0, $f2    #
    cvt.s.w    $f6, $f2    # cast floating point 2

    l.s    $f18, precision    # Guessing precision

loop:
    mov.s    $f4, $f0    # save old guess
    div.s    $f10, $f12, $f0    # temp = (b / x)
    add.s    $f10, $f10, $f0    # temp = (b / x) + x
    div.s    $f0, $f10, $f6    # x = ((b / x) + x) / 2
    sub.s    $f10, $f4, $f0    # temp = old - x
    abs.s    $f8, $f10    # difference = |temp|
    c.le.s    $f8, $f18    #
    bc1f    loop        #

return:
    jr    $ra
                            #
                            #
#########################################################

////////////////////////////////////////////////////////
/// What everything -should- be working like, in Java.

/**
 * @author *Omitted*
 * Driver program to test quadratic and square root functions.
 */
import java.util.Scanner;

public class quadratic
{
    /**
     * Prompts the user for values of a quadratic equation
     * to test functions, quad and squareRoot.
     * @param args
     * Command line arguments
     */
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        String again = "n";
        int inA, inB, inC;

        //root[0] = i
        //root[1] = root one
        //root[2] = root two
        float[] root = new float[3];

       
        do
        {
            System.out.println("Ax^2 + Bx + C = 0");
            System.out.println("input value for A:");
            inA = scan.nextInt();
            System.out.println("input value for B:");
            inB = scan.nextInt();
            System.out.println("input value for C:");
            inC = scan.nextInt();

            switch (quad(inA, inB, inC, root))
            {
                case 0:
                    System.out.println("Two real roots:");
                    System.out.println("root 1: " + root[1]);
                    System.out.println("root 2: " + root[2]);
                    break;
                case 1:
                    System.out.println("One real root: " + root[1]);
                    break;
                case 2:
                    System.out.println("Two complex roots:");
                    System.out.println(root[1] + " + " + root[0] + " i");
                    System.out.println(root[2] + " - " + root[0] + " i");
                    break;
                default:
                    System.out.println("Error!");
                    break;
            }

            System.out.println("\nAgain? (y/n)");
            again = scan.next();
        }
        while(again.equalsIgnoreCase("y") || again.equalsIgnoreCase("yes"));
        System.out.println("Bye!");
    }
   
    /**
     * Estimates the square root of a number.
     *
     * @param b
     * Number to estimate.
     * @return
     * Estimation of the passed in number's square root.
     */
    private static float squareRoot(float b)
    {
        if (b == 0)
            return 0;
       
        float x, old, diff;   
        diff = 0;
        x = b;
       
        do
        {
            //save old guess
            old = x;
           
            //calculate new
            x = (x + (b / x)) / 2;
           
            //compare guesses
            diff = Math.abs(old - x);
        }
        while(diff > 0.00001F);
       
        return x;
    }
   
    /**
     * Using the Quadratic formula, finds the roots of a
     * Quadratic equation.
     *
     * @param a
     * Number representing A in the eqation: AX2 + BX + C = 0
     * @param b
     * Number representing B in the eqation: AX2 + BX + C = 0
     * @param c
     * Number representing C in the eqation: AX2 + BX + C = 0
     * @return
     * Returns the type of roots in a status variable;
     * 0 for two real roots, 1 for a single real root,
     * and 2 for two complex roots.
     */
    private static int quad(int a, int b, int c, float[] root)
    {
        //root[0] = i
        //root[1] = root one
        //root[2] = root two
        int status = 0;

        if (a == 0)
        {   
            status = 3;
            return status;
        }

   
        float descrim;
        //determine equation used by the discriminant
        descrim = ((b*b) - (4 * (a * c)));
       
        if (descrim == 0)
            status = 1;
        else
            if (descrim < 0)
                status = 2;


        //calulate once
        int twoA = (a * 2);
        int negB = (-1 * b);
        switch(status)
        {
            case 0: // two real roots
                descrim = squareRoot(descrim);
                root[1] = (negB + descrim) / twoA;
                root[2] = (negB - descrim) / twoA;
                break;
               
            case 1: // one real root
                root[1] = (b / twoA) * -1;
                break;
       
            case 2: // two complex root
                root[0] = (4 * (a * c)) - (b * b);
                root[0] = squareRoot(root[0]);
                root[0] = root[0] / twoA;
                root[1] = negB / twoA;
                root[2] = negB / twoA;
                break;
           
            default:
                break;
        }
       
        return status;
    }
}
                            ////
///////////////////////////////////////////////////////////

Thanks!

Now let's hope that posting this doesn't completly butchure the whitespace.

Name: Anonymous 2006-10-30 4:09

Are you fucking retarded?

Name: Anonymous 2006-10-30 4:14

O.K, I'll bite. I'd like to think not; but why do you ask?

Name: Anonymous 2006-10-30 4:48

>>1

You can enclose code in [ [ tags for it to remain proportional.

Name: Anonymous 2006-10-30 4:48

>>4

[ code ] [ / code ]

Name: Anonymous 2006-10-30 4:50

Do you know at which point an exception is raised?

Name: Anonymous 2006-10-30 4:53

>>4
>>5

Ah, thanks. Sorry about not doing that, I wasn't aware; any way of editing it now?

Name: Anonymous 2006-10-30 4:59

>>6
Exeption occurred at PC=0x00400f4
  Floating point

is what PCspim is telling me, I truncated some of the code posted here off though.

http://d.turboupload.com/d/1148231/quad2.s.html for the whole thing.

Name: Anonymous 2006-10-30 5:31

>>7
You can't edit, but you can repost it around code tags.

Name: Anonymous 2006-10-30 7:15

ALEX BARKER

Name: Anonymous 2006-10-30 8:36

i think this is your problem:

    cvt.s.w    $f16, $f2    # cast floating point zero

Name: Anonymous 2006-10-30 8:47

Try single-stepping through your program 'til you come to the instruction causing your exception. You can tell it's an exception as it will unexpectedly jump to the kernel code.

Name: Anonymous 2006-10-30 8:47

>>11

Don't be retarded

Name: Anonymous 2006-10-30 8:51

YOUR PROBLEM IS THAT YOU DIVIDED BY ZERO

O SHI-

Name: Anonymous 2006-10-30 12:12

OP here.

>>14
As conveniently this works with 4chan and all, he's right!

In the MIPS assembly version of the function, the initial guess was never being set to b before starting the loop.

Thanks everyone, for your help.

Name: Anonymous 2006-10-30 18:22

meh, use mips64, much cleaner.

Name: Anonymous 2006-10-30 19:21

DO YOUR HOMEWORK YOURSELF YOU FAGGOT

MINIJAVA

SABLECC

FUCK YOU

Name: Anonymous 2009-01-14 14:35

Trolls

Name: Anonymous 2011-01-31 20:14

<-- check em dubz

Name: Anonymous 2011-02-02 22:40

Name: Sgt.Kabu�뷴kimanꚇ䎘 2012-05-28 20:40

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List