954,490 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java to Assembly

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

malugirl4
Junior Poster in Training
84 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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.

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

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

malugirl4
Junior Poster in Training
84 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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.

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

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:

malugirl4
Junior Poster in Training
84 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

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

malugirl4
Junior Poster in Training
84 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

What I have up there is java..he wants us to put it into assembly using MIPS by downloading mars. I have no clue about assembly it wasn't even a prereq. So i'm trying to stay calm hoping someone knows whats going on.

Mars Download:
http://courses.missouristate.edu/KenVollmar/MARS/download.htm

malugirl4
Junior Poster in Training
84 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

Which java link? Apparently MARS needs Java installed too!

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

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

malugirl4
Junior Poster in Training
84 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

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

malugirl4
Junior Poster in Training
84 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

malugirl4
Junior Poster in Training
84 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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.

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

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?

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

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.

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

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

malugirl4
Junior Poster in Training
84 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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.

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You