I have just completed my java code for the game engine maze I have been working on. We are told to download MARS and from java code it in assembly. I have never learned assembly. So I am completely lost. I so far downloaded MARS and uploaded my java code. Where do I go from here. This is my first time even hearing of assembly. I'm completely lost. Please help if you can. Thanks.

import java.util.Scanner;
import java.util.Random;

public class robot
{
	public static void maX]
iin (String args [])
	{
	int robot_x = 0;//int starts at 0
	int robot_y = 0;
	int robot_m_x = 0;
	int robot_m_y = 0;

	int[] robot_instructions = new int[64];// maze
	int[][] maze = new int[9][9];//grid
	//int i = 0;
	int robot_state = 0;
	robot_instructions[0] = 1;
	maze[1][2] = 2;//chasm
	maze[2][3] = 3;//trap
	maze[1][1] = 1;//safe
	maze[3][2] = 4;//destination
	maze[3][4] = 3;//trap
	maze[2][4] = 2;//chasm
	robot_x = 1;
	robot_y = 2;

	robot_state = 5;
	Random rand = new Random();//no set numbers performs random
	for(int i = 0; i < 64; i++)
	{
	robot_instructions[i]=rand.nextInt(5);

	switch(robot_instructions[i])
	{
	case 1:robot_x = robot_m_x;
	robot_y = robot_m_y + 1; //move
	break;
	case 2:robot_x = robot_m_x;
	robot_y = robot_m_y + 2 ; //jump
	break;
	case 3:
	robot_x = robot_m_x - 1; //Left
	break;
	case 4:
	robot_x = robot_m_x + 1; //Right
	break;
	}
	//}

	System.out.println("Robot final location X: " + robot_x + " Y: " + robot_y);
	System.out.println ("Robot State: ");
	if (( (robot_x == 1) && (robot_y == 2)) || ( (robot_x == 2) && (robot_y == 4)) )
	{
	System.out.println("Robot has fallen into a chasm");
	robot_state = 2;
	}
	else if(( (robot_x == 2) && (robot_y == 3)) || ((robot_x == 3) && (robot_y == 4)))
	{
	System.out.println("Robot has fallen into a trap");
	robot_state = 3;
	}
	else if((robot_x == 3) && (robot_y == 2))
	{
	System.out.println("Robot has reached Destination");
	robot_state = 4;
	}
	else
	{
	System.out.println("Robot is at a safe place");
	robot_state = 1;

	}
	switch(robot_state)
	{
	case 1:robot_state = 1; //safe
	System.out.println ( "Robot is at a safe place");
	break;
	case 2:robot_state = 2; //chasm
	System.out.println ( "Robot falls into Chasm");
	break;
	case 3:robot_state = 3; //trap
	System.out.println ("Robot falls into the trap");
	break;
	case 4:robot_state = 4; //destination
	System.out.println ( "Robot reaches Destination");
	break;
	case 5:robot_state = 5; //Failed
	System.out.println ( "Robot fails to reach Destination");
	break;
	}
	}
	}
}

That doesn't make sense. You have a Java class and the Java instructor is telling you to code in Assembly? Or is this an Assembly class but where does Java come in?

The first thing you have to do is un-Java it. That is un-object oriented it.

What assembler are you suppose to use? With masm32 there are libraries you can use for input/output.

So simplify your Java into simple line by line items. Pick the general flow and slowly build that into your assembly application. Take a first attempt and repost, and we'll help nudge you in the correct direction!

Some of this isn't too bad. But you have a mistake in Java. you were reseting robot_state again needlessly.

TxtAry1   db  "Robot is at a safe place"
TxtAry2   db "Robot falls into Chasm"
TxtAry3   db  "Robot falls into the trap"
TxtAry4   db  "Robot reaches Destination"
TxtAry5   db  "Robot fails to reach Destination"

TxtAry    dd    offset TxtAry1, TxtAry2, TxtAry3, TxtAry4, TxtAry5


and in your code

             eax is robot_state
              mov eax,TxtAry[ eax * 4 ]

The idea is to take your code in byte sized pieces then try to reassemble and link. Then occasionally run but single-step the code.

Its actually a computer organization course. First we had to create it in c++, or java whichever our preference is, so I chose java.
"Which assembler are you supposed to use?"
This is my first time doing assembly, I know nothing about it. Prof did tell us to download MARS and run it through that. I'm clueless in what I need to do

I'm not familiar with MARS. Does it come with some examples? If so, look at the small ones and figure out how they work.

That's kind of a big step to be converting a high level language into assembly language without any training steps inbetween!
Makes me wonder if both Java/C++ and Assembly were pre-requistes.

Anyway use one of their examples build it, single step it, add comments in the code, and learn. Then look at your program and try to correlate your Java to Assembly. Assembly has many instructions that aren't used by the higher level languages.

j++ inc eax
j+=k add eax,ebx eax=eax+ebx
But an example of what isn't covered
adc eax,ebx eax=eax+ebx+carry
Anyway, look for correlations. Assembly is simple instructions, Java is a higher level and actually mixes instructions.
For example the j+=k
is more like...

mov  esi,k
   mov eax,[esi]
   mov esi,j
   mov  ebx,[esi]
   add    ebx,eax
   mov  [esi],ebx

There is a way to optimize this further, but I'm trying to make a point of multiple register instructions versus a single Highlevel function.

Its my first time doing assembly..Is this right so far? What do I do at main?

.data # variable declarations follow this line

robot_x: .word 0
robot_y: .word 0
robot_m_x: .word 0
robot_m_y: .word 0

robot_instructions: .space 244
robot_m_y: .word 5

main:

Get some beefy code in place first. You've only done some declarations.

THats the thing. I dont know assembly..I have only learned java..and this proj is due by tonight. So i'm just trying to get all the help I can cause I have no idea how to get this done

Tonight? I don't think there's enough time...


So how do you call rand() from Assembly?

Is this pure Assembly, or a mixed language?

Your robot_instructions array is fixed [64] so you can allocate it!

maze is a [9][9]  So the easiest to over set its size, and save a divide by 9!
maze db    16*9       So your math is    
x, y       (y * 16) + x   Only use the 1st 9 of each row!

     maze[1][2] = 2;//chasm
	maze[2][3] = 3;//trap
	maze[1][1] = 1;//safe
	maze[3][2] = 4;//destination
	maze[3][4] = 3;//trap
	maze[2][4] = 2;//chasm

Insert proc near
      shl edx,4            ; Y x 16
      add edx,ecx       ; y x 16 + x
      mov byte ptr maze[edx],al
     ret
Insert endp

OOPS, then the assembly I've been inserting won't help! It's 80x86.

you're attending Missouri State? You won't make this in time for an evening class! Or did you mean you need to finish it tonight for a Friday class?

But the basics are the same.
You don't need dynamic allocation, used predefined memory.
64 byte array for robot instructions.

16 x 9 = 144 byte buffer for your maze array

Which java link? Apparently MARS needs Java installed too!

no i'm not attending MSU. My prof got it from their website. I need it for tomorrow morning. Do you have any clue what I need to do..I haven't' a clue

Have you downloaded the Fibonacci.asm sample they show?
You can use it as the template and strip/modify it for your needs!

I already have a java compiler I use putty and I use vim (editor)

I have executed the Fibonacci..But I dont know assembly enough to know how to do my java to asembly

Okay, download the file and assemble and run it. See how it works.

The important thing is that it should compile clean and run. It's there sample for MARS.

Then start adding your data fields. Note that
.byte
is for bytes. Most of what you do will fit in a byte.
Then modify the print to match your prints. Add extra lines after the syscall's and keep those code bits blocked as they're one piece. Replicate for your needs!

Note that you don't read values directly from memory. You load the address of the value, then read the contents referenced by that value!

signed short fibs[ 12 ]

la $t0, fibs # load address of array into tmp register t0
lw $a0, 0($t0) # load 16-bit word into register a0l
,0($t0) is load a0 = fibs[0]
,2($t0) is a0 = fibs[1]
,4($t0) is a0=fibs[2]

the ,# is the byte offset from the memory address in register t0.

That code calculates the first 12 fibonacci numbers and stores them in the fibs[] array. Then it looops one at a time to print out all 12.
If you can single-step the code, do so watching the registers!

Does it assemble and run, and display 12 sets of numbers?

The easiest method is to declare your byte arrays (which aren't really two dimensional arrays, only one dimension and you'll be doing the simple math). extract your print code out of Java first and start printing the same stuff.

You'll also need to look at their manuals (or google syscall for MIPS or MARS and find the command to input numbers!

li $v0,4
syscall Print String

li $v0,1
syscall Print integer

$v0 is the function number! Other registers are specific to what tha function requires!

Don't worry about data entry yet! Just dump those arrays like you do now! but keep in mind you'll need data entry so keep your code clean. LOTS of comments on each little code snippet! Whitespace makes assembly easier to read.

I honestly dont think you know how lost I am..

I can't do this for you.

I'll be going off-line for a couple hours but just re-read what I talked about making space for your arrays. And doing print outs.

You don't do print()
You have to set the registers to do the print. Keep things simple.

so dont i have to get rid of the fibs? and do i just start declaring after the .data

also 12..dont i change that? i just wanna know how i start this of..
You've honestly been great help, thank you

In your Java
System.out.println ( "Robot fails to reach Destination");
In MIPS Assembly
.data # DATA SECTION OF ASM FILE
MsgRS5 .asciiz "Robot fails to reach Destination");


.text # CODE SECTION OF ASM FILE
la $a0, MsgRS5 # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading

DO NOT REMOVE ANY FIBS CODE for now.
You will clean up later, and it will give you nice examples to copy from.

Insert your labels next to it!

Start copying in all your pre-canned text strings and give them a unique label. Once that's done, re-assemble to make sure it reassembles. If not look for something missing. A quote or <.> or something!

Then add the matrix and array buffers!

Then try to set a register to an immediate value, and try to print a string with that value! For right now do compare and branches, but will try table arrays for strings later!

.data

MsgRS3 .asciiz  "Robot falls into the trap"
MsgRS4 .asciiz  "Robot reaches Destination"
MsgRS5 .asciiz "Robot fails to reach Destination"



#Just push their stuff out of the way in the file!
space:.asciiz  " "          # space to insert between numbers
head: .asciiz  "The Fibonacci numbers are:\n"

so just ignore the fib..but still keep in on there?

Yes, for now. I'm off line in 5 minutes. So just follow my instructions for now. Move all your text strings over. Experiment with printing your arrays. Remember that integer print function? Look for the system manual and see if there's an 8-bit print. If not then your arrays will be set to .word thus 16-bit instead of 8-bit but still not a problem.

The idea is over the next two hours get as much as you can into your asm file that will assemble and link!

.data
MsgRS1 .asciiz "Robot is at a safe place"
MsgRS2 .asciiz "Robot falls into Chasm"
MsgRS3 .asciiz "Robot falls into the trap"
MsgRS4 .asciiz "Robot reaches Destination"
MsgRS5 .asciiz "Robot fails to reach Destination"

.text # CODE SECTION OF ASM FILE

la $a0, MsgRS1 # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading

la $a0, MsgRS2 # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading

la $a0, MsgRS3 # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading

la $a0, MsgRS4 # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading

la $a0, MsgRS5 # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading

Am I doing this right?

Error in assembly line 2 position 9: ".asciiz" is not a valid integer constant or label
Error in assembly line 2 position 17: ""Robot is at a safe place"" is not a valid integer constant or label
Error in assembly line 3 position 9: ".asciiz" is not a valid integer constant or label
Error in assembly line 3 position 17: ""Robot falls into Chasm"" is not a valid integer constant or label
Error in assembly line 4 position 9: ".asciiz" is not a valid integer constant or label
Error in assembly line 4 position 18: ""Robot falls into the trap"" is not a valid integer constant or label
Error in assembly line 5 position 9: ".asciiz" is not a valid integer constant or label
Error in assembly line 5 position 18: ""Robot reaches Destination"" is not a valid integer constant or label
Error in assembly line 6 position 9: ".asciiz" is not a valid integer constant or label
Error in assembly line 6 position 17: ""Robot fails to reach Destination"" is not a valid integer constant or label
Error in assembly line 36 position 7: ".word" directive cannot appear in text segment
Error in assembly line 37 position 7: ".word" directive cannot appear in text segment
Error in assembly line 2 position 2: Symbol "MsgRS1" not found in symbol table.
Error in assembly line 3 position 2: Symbol "MsgRS2" not found in symbol table.
Error in assembly line 4 position 2: Symbol "MsgRS3" not found in symbol table.
Error in assembly line 5 position 2: Symbol "MsgRS4" not found in symbol table.
Error in assembly line 6 position 2: Symbol "MsgRS5" not found in symbol table.
Assemble: operation completed with errors.

Sorry, they're labels. Forgot the colon.

MsgRS1: .asciiz "Robot is at a safe place"
MsgRS2: .asciiz "Robot falls into Chasm"
MsgRS3: .asciiz "Robot falls into the trap"
MsgRS4: .asciiz "Robot reaches Destination"
MsgRS5: .asciiz "Robot fails to reach Destination"

Don't worry so much about the system printing code. Worry about data and clean assembles!

.data # variable declarations follow this line

robot_x: .word 0
robot_y: .word 0
robot_m_x: .word 0
robot_m_y: .word 0

robot_instructions: .space 244


main:

# Assume that this leaves the value in $t0.

beq $t0, const1, switch_label_1
beq $t0, const2, switch_label_2
beq $t0, const1, switch_label_3
beq $t0, const2, switch_label_4
beq $t0, const1, switch_label_5
switch_label_1:
# MIPS code to compute statement1.

switch_label_2:
# MIPS code to compute statement2.


.data
MsgRS1: .asciiz "Robot is at a safe place"
MsgRS2: .asciiz "Robot falls into Chasm"
MsgRS3: .asciiz "Robot falls into the trap"
MsgRS4: .asciiz "Robot reaches Destination"
MsgRS5: .asciiz "Robot fails to reach Destination"

.text # CODE SECTION OF ASM FILE

la $a0, MsgRS1 # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading

la $a0, MsgRS2 # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading

la $a0, MsgRS3 # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading

la $a0, MsgRS4 # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading

la $a0, MsgRS5 # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading

.data

fibs: .word 0 : 12 # "array" of 12 words to contain fib values
size: .word 12 # size of "array"
.text
la $t0, fibs # load address of array
la $t5, size # load address of size variable
lw $t5, 0($t5) # load array size
li $t2, 1 # 1 is first and second Fib. number
add.d $f0, $f2, $f4
sw $t2, 0($t0) # F[0] = 1
sw $t2, 4($t0) # F[1] = F[0] = 1
addi $t1, $t5, -2 # Counter for loop, will execute (size-2) times
loop: lw $t3, 0($t0) # Get value from array F[n]
lw $t4, 4($t0) # Get value from array F[n+1]
add $t2, $t3, $t4 # $t2 = F[n] + F[n+1]
sw $t2, 8($t0) # Store F[n+2] = F[n] + F[n+1] in array
addi $t0, $t0, 4 # increment address of Fib. number source
addi $t1, $t1, -1 # decrement loop counter
bgtz $t1, loop # repeat if not finished yet.
la $a0, fibs # first argument for print (array)
add $a1, $zero, $t5 # second argument for print (size)
jal print # call print routine.
li $v0, 10 # system call for exit
syscall # we are out of here.

.data
space:.asciiz " " # space to insert between numbers
head: .asciiz "The Fibonacci numbers are:\n"
.text
print:add $t0, $zero, $a0 # starting address of array
add $t1, $zero, $a1 # initialize loop counter to array size
la $a0, head # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading
out: lw $a0, 0($t0) # load fibonacci number for syscall
li $v0, 1 # specify Print Integer service
syscall # print fibonacci number
la $a0, space # load address of spacer for syscall
li $v0, 4 # specify Print String service
syscall # output string
addi $t0, $t0, 4 # increment address
addi $t1, $t1, -1 # decrement loop counter
bgtz $t1, out # repeat if not finished
jr $ra # return

This had no errors and it executed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.