New to MIPS - Java to MIPS Translation Issues

Discussion in 'Assembly Language Programming (ALP) Forum' started by flyntcoal, Sep 27, 2015.

  1. flyntcoal

    flyntcoal New Member

    Joined:
    Sep 27, 2015
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    To be honest, this isn't my first attempt to post and get help, but the other site decided to close my post because it wasn't a good enough question and because I decided to bump it. Be awesome if you could help me out, been trying to figure this out.

    When I go to assemble and run the code, the program loops back up to the user prompt over and over again. I can only edit the Search section of the code. Basically the program should take the user input and tell where in the list the number that the is, if the number is in the list. When I ran it line by line, it went past the part it was stuck at, but it just printed the entire list. You will be considered awesome and get an internet high five from me. :cheers:

    Here is the MIPS Code:

    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	# [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	#
    # add the code that is missing from right here
    		
    search:		add $t4, $t1, $t1
    		addi $t5, $t1, 1
    		sgt $t1, $t4, $t5
    		addi $t4, $t1, 1		
    		add $t5, $t1, $t1
    		add $t1, $t1, 1
    		blt $t3, $t1, search
    		sub $t3, $t3, 1
    		add $t0, $t0, 1
    		blt $t2, $t0, search
    		
    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
    

    Here is the Java code that I am to translate from:

    Code:
    	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. flyntcoal

    flyntcoal New Member

    Joined:
    Sep 27, 2015
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Update to the search section. Still could use the help as the code won't go past the read section.

    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	# [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 the code that is missing from right here
    		add $t0, $zero, $zero	#
    		
    search:		add $t1, $zero, $zero
    
    search2:	lw $t4, 0($t9)
    		lw $t5, 1($t9)
    		sgt $t7, $t4, $t5
    		sw $t4, 0($t9)
    		sw $t5, 1($t9)
    		blt $t1, $t3, search2
    		sub $t3, $t3, 1
    		add $t0, $t0, 1
    		blt $t0, $t2, search
    		
    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. flyntcoal

    flyntcoal New Member

    Joined:
    Sep 27, 2015
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    5 forum posts and can't get any help. All I have gotten is snobby remarks. Should of gotten something by now.
     
  4. flyntcoal

    flyntcoal New Member

    Joined:
    Sep 27, 2015
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Narrowed down to this line

    Code:
        		ble $t0, $t2, read	#
     
  5. flyntcoal

    flyntcoal New Member

    Joined:
    Sep 27, 2015
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Never mind scratch that. Seriously just figured it out. The instructor made it so that the user has to enter 39 prompts.

    I have a new error though

    Here is the updated MIPS Code

    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	# [main]
        		add $t1, $zero, $zero	#
        		addi $t2, $zero, 1	#
        		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		#
        		blt $t0, $t2, read	#
        					
        		add $t0, $zero, $zero	#
        			
        search:		add $t1, $zero, $zero
        
        search2:	lw $t4, 0($t9)
        		lw $t5, 1($t7)
        		sgt $t7, $t4, $t5
        		sw $t4, 0($t9)
        		sw $t5, 1($t9)
        		add $t1, $t1, 1
        		blt $t1, $t3, search2
        		sub $t3, $t3, 1
        		add $t0, $t0, 1
        		blt $t0, $t2, search
        		
        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

    I am now getting an error message, so I have made a bit of progress.
    Not quote sure what to make of this message I am getting with the runtime error saying : fetch address not aligned on word boundary 0x00000001
     
  6. flyntcoal

    flyntcoal New Member

    Joined:
    Sep 27, 2015
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    I should probably say it is on line 139

    Code:
    lw $t5, 1($t7)
     
  7. flyntcoal

    flyntcoal New Member

    Joined:
    Sep 27, 2015
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    how do i say this in MIPS?

    Code:
    t5 = list [t1 + 1];
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice