dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Converting Java to MIPS - Not getting the right output

  1. #1
    Join Date
    Sep 2015
    Posts
    13

    Exclamation Converting Java to MIPS - Not getting the right output

    Okay so now all I get is all the numbers I enter (because it makes me enter them 39 times, if someone can fix that I would appreciate that), plus I also get 780. I should be getting the user input and then where the number is stored.

    Code:
    .data
        list:		.word 5
        		.word 7
        		.word 4
        		.word 6
        		.word 3
        		.word 8
        		.word 2
        		.word 9
        		.word 0
        		.word 30
        		.word 31
        		.word 39
        		.word 32
        		.word 38
        		.word 33
        		.word 37
        		.word 34
        		.word 36
        		.word 35
        		.word 20
        		.word 21
        		.word 29
        		.word 22
        		.word 28
        		.word 23
        		.word 27
        		.word 24
        		.word 26
        		.word 25
        		.word 10
        		.word 11
        		.word 19
        		.word 12
        		.word 18
        		.word 13
        		.word 17
        		.word 14
        		.word 16
        		.word 15
        		.word 1			# last array element
        		.word -1			# not part of array
        prompt1:	.asciiz "\nPlease enter an array element: "
        prompt2:	.asciiz "\nPlease enter a search target: "
        space:	.asciiz " "
        nfound:	.asciiz "\nThe target was not found."
        found:	.asciiz "\nThe target was found at array location "
        
        		.text
        		.globl main
        main:		add $t0, $zero, $zero	#
        		add $t1, $zero, $zero	#
        		addi $t2, $zero, 39	#
        		addi $t3, $zero, 39	#
        		add $t4, $zero, $zero	#
        		add $t5, $zero, $zero	#
        		add $t6, $zero, $zero	#
        #		add $t7, $zero, $zero	#
        		la $t8, list
        
        read:		addi $v0, $zero, 4	#
        		la $a0, prompt1		#
        		syscall
        		addi $v0, $zero, 5	#
        		syscall
        		sll $t7, $t0, 2		#
        		add $t9, $t8, $t7
        		sw $v0, 0($t9)
        		addi $t0, $t0, 1		#
        		ble $t0, $t2, read	#
        					
        		add $t0, $zero, $zero	#
        			
        search:		add $t1, $zero, $zero
        
        search2:	sw $t4, 0($t9)
        		add $t5, $t4, 1
        		sw $t5, 0($t9)
        		sgt $t6, $t4, $t5
        		lw $t4, 0($t9)
        		add $t5, $t4, 1
        		lw $t5, 0($t9)
        		add $t1, $t1, 1
        		blt $t1, $t3, search2
        		sub $t3, $t3, 1
        		add $t0, $t0, 1
        		blt $t0, $t2, search
        		add $t0, $zero, $zero
        		
        print:		addi $v0, $zero, 1	# 
        		sll $t7, $t0, 2		#
        		add $t9, $t8, $t7
        		lw $a0, 0($t9)
        		syscall
        		addi $v0, $zero, 4	#
        		la $a0, space		#
        		syscall
        		addi $t0, $t0, 1		#
        		ble $t0, $t2, print	#
        
        end:		addi $v0, $zero, 10	# 
        		syscall
    Java Code:

    Code:
        package javaapplication15;
        	import java.io.*;
        	
        	public class CIS2233StarterCode2Java {
        	
        	public static void main (String [] args) throws IOException {
        	
        	BufferedReader kbd = new BufferedReader (new InputStreamReader (System.in));
        	int [] list = {5,7,4,6,3,8,2,9,0,20,21,29,22,28,23,27,24,26,25,30,31,39,32,38,33,37,34,36,35,10,11,19,12,18,13,17,14,16,15,1};
        	String prompt1 = "\nPlease enter an array element: ";
        	String prompt2 = "\nPlease enter a search target: ";
        	String space = " ";
        	String nfound = "\nThe target was not found.";
        	String found = "\nThe target was fount at array location ";
        	int t0 = 0;
        	int t1 = 0;
        	int t2 = 39;
        	int t3 = 39;
        	int t4;
        	int t5;
        	int t6;
        	int a0;
        	int v0;
        	// address calculation register t7
        	// base address of array register t8
        	// address calculation register t9
        	do {
        		System.out.print (prompt1);
        		v0 = Integer.parseInt(kbd.readLine());
        		list [t0] = v0;
        		t0 ++;
        	} while (t0 < t2);
        	t0 = 0;
        	do {
        		t1 = 0;
        		do {
        			t4 = list [t1];
        			t5 = list [t1 + 1];
        			if (t4 > t5) {
        				list [t1 + 1] = t4;
        				list [t1] = t5;
        			}
        			t1 ++;
        		} while (t1 < t3);
        		t3 --;
        		t0 ++;
        	} while (t0 < t2);
        	t0 = 0;
        	do {
        		a0 = list [t0];
        		System.out.print (a0);
        		System.out.print (space);
        		t0 ++;
        	} while (t0 <= t2);
                }
                }

  2. #2
    Join Date
    Sep 2015
    Posts
    13

    Re: Converting Java to MIPS - Not getting the right output

    I am now getting the entire list followed by the number the user input

    Example: 5 7 4 6 3 8 2 9 0 30 31 39 32 38 33 37 34 36 35 20 21 29 22 28 23 27 24 26 25 10 11 19 12 18 13 17 14 16 15 5

    Code:
    		.data
    list:		.word 5
    		.word 7
    		.word 4
    		.word 6
    		.word 3
    		.word 8
    		.word 2
    		.word 9
    		.word 0
    		.word 30
    		.word 31
    		.word 39
    		.word 32
    		.word 38
    		.word 33
    		.word 37
    		.word 34
    		.word 36
    		.word 35
    		.word 20
    		.word 21
    		.word 29
    		.word 22
    		.word 28
    		.word 23
    		.word 27
    		.word 24
    		.word 26
    		.word 25
    		.word 10
    		.word 11
    		.word 19
    		.word 12
    		.word 18
    		.word 13
    		.word 17
    		.word 14
    		.word 16
    		.word 15
    		.word 1			# last array element
    		.word -1			# not part of array
    prompt1:	.asciiz "\nPlease enter an array element: "
    prompt2:	.asciiz "\nPlease enter a search target: "
    space:	.asciiz " "
    nfound:	.asciiz "\nThe target was not found."
    found:	.asciiz "\nThe target was found at array location "
    
    		.text
    		.globl main
    main:		add $t0, $zero, 39	# [main]
    		add $t1, $zero, $zero	#
    		addi $t2, $zero, 39	#
    		addi $t3, $zero, 39	#
    		add $t4, $zero, $zero	#
    		add $t5, $zero, $zero	#
    		add $t6, $zero, $zero	#
    #		add $t7, $zero, $zero	#
    		la $t8, list
    
    read:		addi $v0, $zero, 4	# [read]
    		la $a0, prompt1		#
    		syscall
    		addi $v0, $zero, 5	#
    		syscall
    		sll $t7, $t0, 2		#
    		add $t9, $t8, $t7
    		sw $v0, 0($t9)
    		addi $t0, $t0, 1		#
    		ble $t0, $t2, read	#
    					
    		add $t0, $zero, $zero	#
    			
    search:		add $t1, $zero, $zero
    search2:	lw $t4, 0($t8)
    		add $t5, $t4, 1
    		lw $t5, 0($t8)
    		sgt $t6, $t4, $t5
    		sw $t5, 0($t8)
    		add $t5, $t4, 1
    		sw $t4, 0($t8)
    		add $t1, $t1, 1
    		blt $t1, $t3, search2
    		sub $t3, $t3, 1
    		add $t0, $t0, 1
    		blt $t0, $t2, search
    		
    		add $t0, $zero, $zero
    		
    print:		addi $v0, $zero, 1	# [print]
    		sll $t7, $t0, 2		#
    		add $t9, $t8, $t7
    		lw $a0, 0($t9)
    		syscall
    		addi $v0, $zero, 4	#
    		la $a0, space		#
    		syscall
    		addi $t0, $t0, 1		#
    		ble $t0, $t2, print	#
    
    end:		addi $v0, $zero, 10	# [end]
    		syscall

  3. #3
    Join Date
    Sep 2015
    Posts
    13

    Re: Converting Java to MIPS - Not getting the right output

    well? i am so far hitting a brick wall and it seems the one tool that could be useful (this site) isn't. i know it is volunteer, but to allow someone to just be hung out to dry with 0 help just don't seem right.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,924

    Re: Converting Java to MIPS - Not getting the right output

    Quote Originally Posted by flyntcoal View Post
    well? i am so far hitting a brick wall and it seems the one tool that could be useful (this site) isn't. i know it is volunteer, but to allow someone to just be hung out to dry with 0 help just don't seem right.
    Patience is a virtue. Some one with the necessary assembler skills will come along. I've just now read your posts but unfortunately I can't help. Some members only check these posts daily and for me this is first thing Monday morning after a weekend!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Converting Java to MIPS - Not getting the right output

    What you're describing is a logic error. So get your debugger and step through the code monitoring the loop variables and figure out what's wrong.
    It's unrealistic to expect someone else to do your debugging for you.


    This isn't a question about how to interact with the OS, or how to deal with the system nor is it even specific about assembly. you could just as well have had a problem in C++, and we would have told you the same. "use your debugger".

    With a C++ problem you just might have had a bit more luck that someone with enough skill happens to spot the logic error right away and decides to post about it.

    But this is Assembly, a subforum that doesn't get a lot of attention as it is, and you're asking this for MIPS, a processor not a lot of people are familiar with in this world vastly dominated by x86 and ARM. (I wouldn't place mips even in the top 10 of most popular chip architectures).

  6. #6
    Join Date
    Sep 2015
    Posts
    13

    Re: Converting Java to MIPS - Not getting the right output

    Quote Originally Posted by OReubens View Post
    What you're describing is a logic error. So get your debugger and step through the code monitoring the loop variables and figure out what's wrong.
    It's unrealistic to expect someone else to do your debugging for you.


    This isn't a question about how to interact with the OS, or how to deal with the system nor is it even specific about assembly. you could just as well have had a problem in C++, and we would have told you the same. "use your debugger".

    With a C++ problem you just might have had a bit more luck that someone with enough skill happens to spot the logic error right away and decides to post about it.

    But this is Assembly, a subforum that doesn't get a lot of attention as it is, and you're asking this for MIPS, a processor not a lot of people are familiar with in this world vastly dominated by x86 and ARM. (I wouldn't place mips even in the top 10 of most popular chip architectures).
    Right, I know from going through it step by step (prob well over a 100 times) that it is a logic error, but anytime I try to edit something with in the only sections of the code I can change it doesn't quite work. The problem has to be within the bit of code I was told not to edit and it was written by the instructor. It is a pain to try and figure out someone else's logic error over my own.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Converting Java to MIPS - Not getting the right output

    If you were told not to edit a part of the code, than that may have been exactly the kind of problem you were really asked to solve.

    When calling other functions in assembly, you Always have to deal with something called "calling convention".

    This is a set of rules that describe
    - how parameters are transfered from the caller to the called function
    - how value(s) are returned
    - which registers you should not expect to still hold the same value after the call.
    - and the other way around, which registers you can assume to remain unchanged over the call.

    If you're sure your code has no inherent logic error, and you are to assume the called code doesn't either, then the most likely cause if that you have violated one of the calling convention rules.

    If you assume some register remains the same after the call, but the function changes it (or "could" change it according to the calling convention), then you should preserve this register value before the call, and restore it afterwards. This is typically done with a push and pop on the stack, but there are other ways to do it as well.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)