11 Replies - 8010 Views - Last Post: 29 September 2015 - 05:10 PM

#1 flyntcoal   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 22-February 15

Converting Java to MIPS

Posted 26 September 2015 - 07:50 PM

Here we go again, cuz last was closed because i decided to bump it in order get help faster. (This will probably get closed again, but who cares).

Just start recently working with MIPS and could really use the help. When I run the code all I get is the user prompt over and over again. When I ran it step by step it did finally go past, but ended up just printing the entire list, but I just need to print to the user where in the list the number is or if it isn't in it. Can only edit the search part of the code. I was given the Java part of the code just for the fact of converting the Java to MIPS.

JAVA 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);
        }
        }


Here is the MIPS 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



If I can get a bit of help before he closes this thread that would be awesome and you would be awesome for helping out. :bananaman:/>

This post has been edited by macosxnerd101: 26 September 2015 - 07:52 PM
Reason for edit:: Renamed title to be more descriptive


Is This A Good Question/Topic? 0
  • +

Replies To: Converting Java to MIPS

#2 flyntcoal   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 22-February 15

Re: Converting Java to MIPS

Posted 27 September 2015 - 12:12 PM

Rewrote the Search section, but can't test it until it actually goes past the user prompt. All it does is loop back up to the top of the read section. :helpsmilie:

	.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


Was This Post Helpful? 0
  • +
  • -

#3 flyntcoal   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 22-February 15

Re: Converting Java to MIPS

Posted 27 September 2015 - 03:04 PM

5 forum posts and can't get any help. All I have gotten is snobby remarks. Should of gotten something by now.
Was This Post Helpful? 0
  • +
  • -

#4 flyntcoal   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 22-February 15

Re: Converting Java to MIPS

Posted 27 September 2015 - 03:09 PM

Narrowed down to this line

		ble $t0, $t2, read	#

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15114
  • View blog
  • Posts: 60,481
  • Joined: 12-June 08

Re: Converting Java to MIPS

Posted 27 September 2015 - 03:14 PM

This is a volunteer forum, chief. Folks will answer on their knowledge and schedule, and not yours

Chillax, and maybe curb the 'tude, d00d.
Was This Post Helpful? 0
  • +
  • -

#6 flyntcoal   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 22-February 15

Re: Converting Java to MIPS

Posted 27 September 2015 - 03:20 PM

View Postmodi123_1, on 27 September 2015 - 03:14 PM, said:

This is a volunteer forum, chief. Folks will answer on their knowledge and schedule, and not yours

Chillax, and maybe curb the 'tude, d00d.



Chillax this is the 2nd time posting it here and been waiting for responses on other forums for several days. How can I chill when I am trying to figure out what the hell is wrong with the code that I didn't even write? And I know it is just a change or two away from working, but for the life of me i can't figure it out and it is frustrating when you post to forums to get help and all I have gotten is remarks about how i shouldn't bump my post, about i am not asking a specific enough question, or how i am just asking a terrible question. From everything it just is telling me i am either crap at asking questions (which no such thing as a bad question), i'm too stupid to know how to ask a question, or people don't have time to give a second to help me out. Kinda see where the frustration is?
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15114
  • View blog
  • Posts: 60,481
  • Joined: 12-June 08

Re: Converting Java to MIPS

Posted 27 September 2015 - 03:24 PM

Ah, that makes sense. You are asking for folks to help you fool around with code you did not create. I guess I would be a little confused too.
Was This Post Helpful? 0
  • +
  • -

#8 flyntcoal   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 22-February 15

Re: Converting Java to MIPS

Posted 27 September 2015 - 03:29 PM

View Postmodi123_1, on 27 September 2015 - 03:24 PM, said:

Ah, that makes sense. You are asking for folks to help you fool around with code you did not create. I guess I would be a little confused too.



Basically i have one section of code that is what I created, the rest was given to me by the instructor in order to add code to it, but his section is giving me the trouble and i haven't been able to get a hold of him, so tried next best thing, but so far that hasn't worked. the search and search2 sections are both ones that I wrote, but I can't test them, because the read section loops over and over again. Saying that the register is over 39, so naturally I tried to make it a greater than to test and I got an error. So trying to get help on figuring out why the read section (he wrote) isn't working. FYI: Does the same in the Java code so technically it is the same, but that isn't good because then his code is all jacked up.
Was This Post Helpful? 0
  • +
  • -

#9 flyntcoal   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 22-February 15

Re: Converting Java to MIPS

Posted 27 September 2015 - 03:53 PM

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

    		.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

I should probably say it is on line 139
Was This Post Helpful? 0
  • +
  • -

#10 flyntcoal   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 22-February 15

Re: Converting Java to MIPS

Posted 27 September 2015 - 03:58 PM

how do i say this in MIPS?

t5 = list [t1 + 1];

Was This Post Helpful? 0
  • +
  • -

#11 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,110
  • Joined: 03-April 12

Re: Converting Java to MIPS

Posted 28 September 2015 - 07:56 PM

The error says (most probably) that you are trying to rwite or read a word from an odd address (0x00000001). Words (4 bytes) need to be on 4-byte boundary.

if t3 is not already in use
"t5 = list [t1 + 1];"
add t3, t1, #1
lw t5, list(t3)


if it is, find some other free register

I tried to figure out what the code should do, but I didn't quite get it. Comments would have been nice...

This post has been edited by turboscrew: 28 September 2015 - 07:59 PM

Was This Post Helpful? 0
  • +
  • -

#12 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,110
  • Joined: 03-April 12

Re: Converting Java to MIPS

Posted 29 September 2015 - 05:10 PM

In your earlier post, you showed this:

Quote

68 sw $v0, 0($t9)
69 addi $t0, $t0, 1 #
70 ble $t0, $t2, read #
71 add $t0, $zero, $zero #
72 # add the code that is missing from right here
73
74 search: add $t4, $t1, $t1
75 addi $t5, $t1, 1
76 sgt $t1, $t4, $t5
77 addi $t4, $t1, 1


So what you are supposed to do is write this in MIPS code?

33	    t0 = 0;
34	    do {
35	        t1 = 0;
36	        do {
37	            t4 = list [t1];
38	            t5 = list [t1 + 1];
39	            if (t4 > t5) {
40	                list [t1 + 1] = t4;
41	                list [t1] = t5;
42	            }
43	            t1 ++;
44	        } while (t1 < t3);
45	        t3 --;
46	        t0 ++;
47	    } while (t0 < t2);



t0 = 0
do {
...
} while (t0 < t2)


translates, for example , to
add $t0, $zero, $zero # t0 = 0
loop:           # do {
...
sltu s1, t0, t2 # (t0 < t2)
bgtz s1, loop   # } while



t4 = list [t1];


translates to:
lw t4, list(t1)



t5 = list [t1 + 1];


translates to:
add s1, t1, 1 
lw t5, list(s1)



list [t1 + 1] = t4;


translates to:
add s1, t1, 1
sw t4, list(s1)


This post has been edited by turboscrew: 29 September 2015 - 05:13 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1