0

Okay, I'm not familiar with MARS so this is an experiment
Stick the following at the top of your asm file and comment out where they exist elsewhere in your code!

A # in MARS
is the same as // in Java

.data

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

robot_instructions: .space 244


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
#main:

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


jr $ra # return

Get it to compile then run. Should get a printed line! Robot is...

0

It says robot is at a safe place.. I compiled it one step at a time

0

but I'm getting one error
Error in : invalid program counter value: 0

Step: execution terminated with errors.

0

Deal with that later. (Does it give a line number?)

below syscall # print number the code I had you paste!

# Get a Random #

	li $a0, 0		# Rng#
	li $a1, 4		# rand() %4   {0...3}
	li $v0, 41
	syscall			# Get RNG
		# $a0 = {0...3}

li $v0, 1 # specify Print Integer service
syscall # print number

above jr $ra #return

0

Sorry, make that 41 a 42

li $v0, 42

That code calls the system to generate a random range of numbers from 0 to a1. Since it is set to 4, it will generate a random number from 0...3 0<= x < N

So that code generates a number from 0 to 3 and then prints it. The print part is a test of printing an integer!

0

no its not giving me a line number.. I don't see the error anymore..

.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


    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
#main:

    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

# Get a Random #

    li $a0, 0       # Rng#
    li $a1, 4       # rand() %4   {0...3}
    li $v0, 41
    syscall         # Get RNG
        # $a0 = {0...3}

    li $v0, 1 # specify Print Integer service
    syscall # print number

      jr   $ra              # return

This is working so far..

Edited by mike_2000_17: Fixed formatting

0

I meant for you to insert the random code right after that first message print. We're going to temporarily use the system-print calls like breadcrumbs.

Did it print a number? Run it again, did the number change? Every time you run it it should be a number between 0 and 3.

0

results:
Robot is at a safe place2Robot falls into ChasmRobot falls into the trapRobot reaches DestinationRobot fails to reach DestinationRobot fails to reach Destination

0

Yep! See the 2 hiding in there! We're not printing \n Carriage Return, Line Feed.


Okay, I'm not going to write this whole thing for you but here's a breakdown of what you should now know.

How to create 16-bit data. You've declared your robot variables.
And ASCIIz text strings.
You know how to print ASCIIz string buffers.
You know how to print a 16-bit integer.
You know how to randomize a number of a range of 0...N-1

Now we're going to seed that RNG.
Right after #main insert the following at the start of your code!
#main

li $v0,30		# Get time
	syscall			# CMD: Get Time
		# $a0 = lower 32-bits,  $a1=upper 32-bits

		# Seed RNG (Random Number Generator)
	mov $a1,$a0		# get lower 32-bit time as seed
	li $a0,0		# Rng#0
	syscall			# CMD: Seed the RNG
0

Error in assembly line 25 position 2: "mov" directive cannot appear in text segment
Assemble: operation completed with errors.

0
.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


    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
#main:

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


# Get a Random #

    li $a0, 0               # Rng#
    li $a1, 4               # rand() %4   {0...3}
    li $v0, 42
    syscall                 # Get RNG
                            # $a0 = {0...3}

    li $v0, 1               # specify Print Integer service
    syscall                 # print number

    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
#main
li $v0,30       # Get time
    syscall         # CMD: Get Time
        # $a0 = lower 32-bits,  $a1=upper 32-bits

        # Seed RNG (Random Number Generator)
    move $a1,$a0        # get lower 32-bit time as seed
    li $a0,0        # Rng#0
    syscall         # CMD: Seed the RNG

      jr   $ra              # return

it was successful..is it supposed to print something else?

Edited by mike_2000_17: Fixed formatting

0

No, the seed code goes at the top of your code! Near the top of the file is
.text
#main:
<--- Put Time to seed here
<---- Put RNG SEED CODE HERE

print MsgRS1

0
.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


    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
    #main:
    li $v0,30       # Get time
    syscall         # CMD: Get Time
        # $a0 = lower 32-bits,  $a1=upper 32-bits

        # Seed RNG (Random Number Generator)
    move $a1,$a0        # get lower 32-bit time as seed
    li $a0,0        # Rng#0
    syscall         # CMD: Seed the RNG

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


# Get a Random #

    li $a0, 0               # Rng#
    li $a1, 4               # rand() %4   {0...3}
    li $v0, 42
    syscall                 # Get RNG
                            # $a0 = {0...3}

    li $v0, 1               # specify Print Integer service
    syscall                 # print number

    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

li $v0,30       # Get time
    syscall         # CMD: Get Time
        # $a0 = lower 32-bits,  $a1=upper 32-bits

        # Seed RNG (Random Number Generator)
    move $a1,$a0        # get lower 32-bit time as seed
    li $a0,0        # Rng#0
    syscall         # CMD: Seed the RNG

      jr   $ra              # return

I just placed it under the #main I had on top

Edited by mike_2000_17: Fixed formatting

0

So now you know

move $a1,$a0 copies from $a0 and stores to $a1

Now you need your maze buffer! You haven't created it yet!
Stick this in the .data section at the top of the file!

maze  .space  144     # 9x9 stored in a 16x9 buffer

You will need to clear this array. You don't do it in your Java code but it needs to be done!

This goes right after the SEED initialization before all that text printing
And before the GET Random number!!!

# Clear Maze Array

	la $a0, Maze		# load address of maze
	li $t3, 0
	li $t1, 144
clr:	
	sb 	$t3, 0($t0)		# Set value from array
	addi $t0, $t0, 1	# increment Maze address
	addi $t1, $t1, -1	# decrement loop counter
	bgtz $t1, clr		# repeat if not finished yet.

Notice, this is mostly code from the original fibonacci code, only reworked for your needs!

Remember what I said about commenting between code snippets!
Review each code snippet thus far and make sure their functionality is labeled!

TABS
Labels should be left justified. ALL code should be one TAB in,operands another tab

LABEL:
<TAB> bgtz <TAB> $t1,clr <TAB> <TAB> #comment

0

Error in assembly line 3 position 11: ".space" is not a valid integer constant or label
Error in assembly line 3 position 5: Symbol "maze" not found in symbol table.
Assemble: operation completed with errors.

0

la $a0, Maze # load address of maze


Error in assembly line 32 position 10: Symbol "Maze" not found in symbol table.
Assemble: operation completed with errors.

<:> ?

0

I just realized I made a mistake.

maze is all lower case.

AND

The clear maze $a0 is suppose to be $t0

la $t0, maze		# load address of maze
	li $t3, 0
	li $t1, 144
clr:		# repeat if not finished yet.

Okay, right after Maze is cleared it needs to have your values set

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

maze [Y][X]
       offset =  (Y * 16) + x
la $t0, maze        # base address

     li  $t3, 2                # 	maze[1][2] = 2;
     sb  $t3, 18($t0)    #        1 x16 + 2

Now work out our other pre-set maze initializers.

0

See if MARS accepts the following.

sb $t3, 18($t0) # 1 x16 + 2
sb $t3, 0x12($t0) # maze[1][2]

If it takes the hex code 0x12 that is 0x10 + 0x02 , 16 + 2
Matches Y, X nicely!

0

So initialize all your variables like in Java.

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;

0

a: .word 2
b: .word 3
c: .word 4
d: .word 5
e: .word 1

Does it do anything like this somewhere?

0

You have a loop of 64 iterations.

Use temporary register $t7

li $t7,0 # index=0

BigLoop:
# robot_instructions=rand.nextInt(5);

So in Java are you randomizing 1...4 or 0...4 ?
You only have case 1 .... 4

0

Those assignments are immediate values. Not values stored in memory. You can create a label, and assign those values to those locations. But then you'll have to get an address, get the value, the get the destination address and set that value.

How about using an equate?

iSAFE = 1
iCHASM = 2
iTRAP = 3
etc.

Then all you need to do is

la $t0, maze        # base address

     li  $t3, iCHASM       # 	maze[1][2] = 2;
     sb  $t3, 0x12($t0)    #        1 x 16 + 2
0

The ichasm would be much better I think. Its a simpler logic..Would it work? And does it need to be init. at the top?

0

So random 0...3
You have four cases
so increment the result.

# Get a Random #

	li $a0, 0		# Rng#0
	li $a1, 4		# rand() %4   {0...3}
	li $v0, 41
	syscall			# Get RNG
		# $a0 = {0...3}
                addi $a0, $a0, 1          # $a0 = {1...4}

Store the result in the instructions array
robot_instructions=rand.nextInt(5);
This is more like what you're doing in assembly
r = rand.nextInt(5) get 1...4
robot_instructions = r store word
You used a .word array so use a
sw store word,
like you previously use the
sb save byte
instruction!

0

Whatever equate you want. The simplest is using the equates defined at the top of the file (THey're like #define ichasm 1 in Java)

Once you do all that, you have to test for your four states, but using four comparison conditionals!

0
.data        # variable declarations follow this line
    maze:  .space  144     # 9x9 stored in a 16x9 buffer 
    robot_x: .word 0
    robot_y: .word 0
    robot_m_x: .word 0
    robot_m_y: .word 0

    robot_instructions: .space 244


    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
    #main:
    li $v0,30       # Get time
    syscall         # CMD: Get Time
        # $a0 = lower 32-bits,  $a1=upper 32-bits

        # Seed RNG (Random Number Generator)
    move $a1,$a0        # get lower 32-bit time as seed
    li $a0,0        # Rng#0
    syscall         # CMD: Seed the RNG


# Clear Maze Array

    la $t0, maze        # load address of maze
    li $t3, 0
    li $t1, 144
clr:        # repeat if not finished yet.

 la $t0, maze        # base address

     li  $t3, 2                #    maze[1][2] = 2;
     sb  $t3, 18($t0)    #        1 x16 + 2

    iSAFE = 1
    iCHASM = 2
    iTRAP = 3
    iDESTINATION = 4



    la $t0, maze        # base address

     li  $t3, iSAFE      #  maze[1][1] = 1;
     sb  $t3, 0x12($t0)    #        1 x 16 + 1


   la $t0, maze        # base address

     li  $t3, iCHASM       #    maze[1][2] = 2; #maze[2][4];
     sb  $t3, 0x12($t0)    #        1 x 16 + 2

 la $t0, maze        # base address

     li  $t3, iTRAP      #  maze[2][3] = 3; # maze[3][4];
     sb  $t3, 0x12($t0)    #        1 x 16 + 3
   la $t0, maze        # base address

     li  $t3, iDESTINATION      #   maze[3][2] = 4;
     sb  $t3, 0x12($t0)    #        1 x 16 + 4





    sb  $t3, 0($t0)     # Set value from array
    addi $t0, $t0, 1    # increment Maze address
    addi $t1, $t1, -1   # decrement loop counter
    bgtz $t1, clr       # repeat if not finished yet.


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


# Get a Random #

    li $a0, 0               # Rng#
    li $a1, 4               # rand() %4   {0...3}
    li $v0, 42
    syscall                 # Get RNG
                            # $a0 = {0...3}

    li $v0, 1               # specify Print Integer service
    syscall                 # print number

    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

li $v0,30       # Get time
    syscall         # CMD: Get Time
        # $a0 = lower 32-bits,  $a1=upper 32-bits

        # Seed RNG (Random Number Generator)
    move $a1,$a0        # get lower 32-bit time as seed
    li $a0,0        # Rng#0
    syscall         # CMD: Seed the RNG

      jr   $ra              # return

Edited by mike_2000_17: Fixed formatting

0

I'd normaly use a vector table but its getting late, so just using conditionals...

r = (rand() % 3) + 1;     1...4

// $a0 has your random number 1...4
If you subtract 1, then it will set your zero flag
a0 = a0 - 1
if (a0 is not zero)

addi $a0, $a0, -1              // was (1)?
bnez  Try2
#      if (r == 1) then                 // move
          robot_x = robot_m_x;
            robot_y = robot_m_y + 1;
        endif
  jr  nxt

Try2:
addi $a0, $a0, -1              // was (2)?
#           if (r == 2)      // jump
   robot_x = robot_m_x;
   robot_y = robot_m_y + 2 ; 
#     endif
   jr  nxt


etc.


else if (r == 3)                       // left
   robot_x = robot_m_x - 1;
else    //if (r == 4)                  // Right
   robot_x = robot_m_x + 1; 
endif

nxt:
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.