| | |
Java to Assembly
Thread Solved |
Done: Should be near bottom of file just above the robot_state 4 case code that doesn't exist yet!
# Process new robot positions
j PrtOut
MoveHop: j MoveLoop
Done:
# Next conditional line
# Ref: else if(( (robot_x == 2) && (robot_y == 3)) || ((robot_x == 3) && (robot_y == 4)))
!!!! Remember $t0 is robot_x $t5 is robot_y
!!!! $t1=(1) $t2=(2) $t3=(3) $t4=(4)
!!! this line comment is correct, code isn't! You have wrong immedate value, and you aren't jumping to 2nd half of this line! Remember you had ( A & B ) || (C & D) So only 1/2 has to be true to be successful! So if first half fails, then check the second half.
Re-examine conditionals and your branching. I'm shutting down in a couple minutes to head for home, etc. But...
1st Conditional Line
LineA:
if (A <> #) then go to 2nd half Line A3
if (B == #) Then go to succss for LineA LineAOK
LineA3:
if (C <> #) then go to Line B we totally lose
if (D <> #) then go to Line B we also lose
LineAOK: WINNER
Do our stuff
then jump past everything to DONE which is robot_state case
--------
Line B
For Line B we do same as Line A with new sets of #'s.
------
Line C
Same again
------
Line D
Same again
Done: We are now at robot_state printing!
----------------
Okay review my comments as to your code...
for Line B
bne $t0,$t1,LineB # jump if robot_x <> 2
!!!Comment is correct Code is not!
beq $t5,$t2,LineBOK # jump if robot_y == 3
LineB: # || ( (robot_x == 2) && (robot_y == 4)) )
bne $t0,$t2,LineC # <--- Good
bne $t5,$t4,LineC # <--- Good
LineBOK: #SUCCESS
la $a0, MsgFallenTrap
li $vo, 4
syscal
la $a0, robot_state
sw $t2, 0 ($a0) #robot_state=2
j prtOut ????
!!! remember we keep flowing dowards. In Java it was if else if elseif else
So Need to jump past the other else's to the robot_state case statement code!
j Done
# Process new robot positions
j PrtOut
MoveHop: j MoveLoop
Done:
# Next conditional line
# Ref: else if(( (robot_x == 2) && (robot_y == 3)) || ((robot_x == 3) && (robot_y == 4)))
!!!! Remember $t0 is robot_x $t5 is robot_y
!!!! $t1=(1) $t2=(2) $t3=(3) $t4=(4)
!!! this line comment is correct, code isn't! You have wrong immedate value, and you aren't jumping to 2nd half of this line! Remember you had ( A & B ) || (C & D) So only 1/2 has to be true to be successful! So if first half fails, then check the second half.
Re-examine conditionals and your branching. I'm shutting down in a couple minutes to head for home, etc. But...
1st Conditional Line
LineA:
if (A <> #) then go to 2nd half Line A3
if (B == #) Then go to succss for LineA LineAOK
LineA3:
if (C <> #) then go to Line B we totally lose
if (D <> #) then go to Line B we also lose
LineAOK: WINNER
Do our stuff
then jump past everything to DONE which is robot_state case
--------
Line B
For Line B we do same as Line A with new sets of #'s.
------
Line C
Same again
------
Line D
Same again
Done: We are now at robot_state printing!
----------------
Okay review my comments as to your code...
for Line B
bne $t0,$t1,LineB # jump if robot_x <> 2
!!!Comment is correct Code is not!
beq $t5,$t2,LineBOK # jump if robot_y == 3
LineB: # || ( (robot_x == 2) && (robot_y == 4)) )
bne $t0,$t2,LineC # <--- Good
bne $t5,$t4,LineC # <--- Good
LineBOK: #SUCCESS
la $a0, MsgFallenTrap
li $vo, 4
syscal
la $a0, robot_state
sw $t2, 0 ($a0) #robot_state=2
j prtOut ????
!!! remember we keep flowing dowards. In Java it was if else if elseif else
So Need to jump past the other else's to the robot_state case statement code!
j Done
Last edited by wildgoose; Aug 14th, 2009 at 11:59 pm.
•
•
Join Date: Jun 2009
Posts: 82
Reputation:
Solved Threads: 0
Assembly Syntax (Toggle Plain Text)
.data # variable declarations follow this line iSAFE: 1 iCHASM: 2 iTRAP: 3 iDESTINATION: 4 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_state: .word 0 robot_instructions: .space 256 # Robot State Message Table MsgRobotTbl: .word MsgRS1 .word MsgRS2 .word MsgRS3 .word MsgRS4 .word MsgRS5 MsgFailsDestination: .asciiz "Robot fails to reach Destination\n" MsgRS1: .asciiz "Robot is at a safe place\n" MsgRS2: .asciiz "Robot falls into Chasm\n" MsgRS3: .asciiz "Robot falls into the trap\n" MsgRS4: .asciiz "Robot reaches Destination\n" MsgRS5: .asciiz "Robot fails to reach Destination\n" MsgFinal: .asciiz "Robot final location X: " MsgY: .asciiz " Y: " MsgCR: .asciiz "\n" MsgRobotState: .asciiz "Robot State: \n" MsgFallenChasm: .asciiz "Robot has fallen into a chasm\n" MsgFallenTrap: .asciiz "Robot has fallen into a trap\n" MsgReachedDestination: .asciiz "Robot has reached Destination\n" MsgSafePlace: .asciiz "Robot is at a safe place\n" MsgFallsChasm: .asciiz "Robot falls into Chasm\n" MsgFallsTrap: .asciiz "Robot falls into the trap\n" MsgReachesDestination: .asciiz "Robot reaches Destination\n" .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 Entire Maze Array la $t0, maze # address of maze li $t3, 0 li $t1, 144 clr: # repeat if not finished yet. sb $t3, 0($t0) add $t0, $t0, 1 # address++ add $t1, $t1, -1 # count-- bgtz $t1, clr # Setup variables la $t0, maze # base address li $t1,1 # iSAFE: sb $t1,0x11($t0) # maze[1][1] = 1; li $t1,2 # iCHASM: sb $t1, 0x12($t0) # maze[1][2] = 2 sb $t1,0x24($t0) # maze[2][4] = 2 li $t1, 3 #iTRAP: sb $t1, 0x23($t0) # maze[2][3] sb $t1, 0x34($t0) # maze[3][4] li $t1, 4 #iDESTINATION: # maze[3][2] = 4; sb $t1, 0x32($t0) # 1 x 16 + 4 # robot_x = 1 la $t0,robot_x li $t1,1 sw $t1,0($t0) # robot_y = 2 la $t0,robot_y li $t1,2 sw $t1,0($t0) # init robot_state = 5 la $t0,robot_state li $t1,5 sw $t1,0($t0) la $t7,robot_instructions # index li $t6,64 MoveLoop: # Get a Random # li $a0, 0 # Rng# li $a1, 4 # rand() %4 {0...3} li $v0, 42 syscall # Get RNG # $a0 = {0...3} add $a0, $a0, 1 # {1...4} sw $a0,0($t7) # robot_instructions[i] = rnd{ 1...4 } add $a0, $a0, -1 bgtz $a0, RJump # case 1: MOVE la $t1,robot_m_x la $t0,robot_x lw $t2,0($t1) sw $t2,0($t0) # robot_x = robot_m_x la $t1,robot_m_y la $t0,robot_y lw $t2,0($t1) add $t2,$t2,1 sw $t2,0($t0) # robot_y = robot_m_y + 1 j PrtOut RJump: add $a0, $a0, -1 bgtz $a0, RLeft # case 2: Jump la $t1,robot_m_x la $t0,robot_x lw $t2,0($t1) sw $t2,0($t0) # robot_x = robot_m_x la $t1,robot_m_y la $t0,robot_y lw $t2, 0($t1) add $t2, $t2, 2 sw $t2, 0($t0) # robot_y = robot_m_y + 2 j PrtOut RLeft: add $a0, $a0, -1 bgtz $a0, RRight # case 3: Left la $t1,robot_m_x la $t0,robot_x lw $t2,0($t1) add $t2, $t2, -1 sw $t2,0($t0) # robot_x = robot_m_x - 1 j PrtOut RRight: la $t1,robot_m_x la $t0,robot_x lw $t2,0($t1) add $t2, $t2, 1 sw $t2,0($t0) # robot_x = robot_m_x + 1 # Process new robot posit5ions! PrtOut: # System.out.println("Robot final location X: " + robot_x + " Y: " + robot_y); la $a0, MsgFinal li $v0, 4 # specify Print String service syscall # print heading la $t0,robot_x lw $a0,0($t0) li $v0, 1 syscall # CMD: print robot x integer la $a0, MsgY li $v0, 4 # specify Print String service syscall # print heading la $t0,robot_y lw $a0,0($t0) li $v0, 1 syscall # CMD: print robot Y integer la $a0, MsgCR li $v0, 4 # specify Print String service syscall # print heading # System.out.println ("Robot State: "); la $t0,robot_x lw $t0,0($t0) # $t0 = robot_x la $t5,robot_y # lw $t5,0($t1) # $t5 = robot_y lw $t5,0($t5) # $t5 = robot_y # Using $t1 ... $t4 since they look similar to 1...4 li $t1,1 li $t2,2 li $t3,3 li $t4,4 # register to register compares # Ref: if (( (robot_x == 1) && (robot_y == 2)) || ( (robot_x == 2) && (robot_y == 4)) ) bne $t0,$t1,LineA3 # jump if robot_x <> 1 beq $t5,$t2,LineAOK # jump if robot_y == 2 LineA3: # || ( (robot_x == 2) && (robot_y == 4)) ) bne $t0,$t2, LineB #jump if robot_x <> 2 bne $t5,$t4, LineB #jump if robot_y <> 4 LineAOK: # SUCCESS la $a0, MsgFallenChasm li $v0, 4 # specify Print String service syscall # print heading la $a0, robot_state sw $t2,0($a0) # robot_state = 2 # Process new robot positions j PrtOut MoveHop: j MoveLoop # Next conditional line # Ref: else if(( (robot_x == 2) && (robot_y == 3)) || ((robot_x == 3) && (robot_y == 4))) bne $t0,$t2,LineB # jump if robot_x <> 2 beq $t5,$t3,LineBOK # jump if robot_y == 3 LineB: # || ( (robot_x == 2) && (robot_y == 4)) ) bne $t0,$t3,LineC #jump if robot_x <> 3 bne $t5,$t4, LineC #jump if robot_y <> 4 LineBOK: #SUCCESS la $a0, MsgFallenTrap li $vo, 4 syscal#print heading la $a0, robot_state sw $t2, 0 ($a0) #robot_state=3 j MoveLoop j Done # register to register compares # Ref: if ((robot_x == 3) && (robot_y == 2)) bne $t0,$t3,LineC # jump if robot_x <> 3 beq $t5,$t2,LineC # jump if robot_y == 2 LineCOK: # SUCCESS la $a0, MsgReachedDestination li $v0, 4 # specify Print String service syscall # print heading la $a0, robot_state sw $t2,0($a0) # robot_state = 4 # Process new robot positions j PrtOut j MoveLoop j Done LineDOK: # SUCCESS la $a0, MsgSafePlace li $v0, 4 # specify Print String service syscall # print heading la $a0, robot_state sw $t2,0($a0) # robot_state = 1 # Process new robot positions j PrtOut Done: jr $ra # return
Since this is no longer a school project I went in and cleaned top to bottom. You need to finish at bottom where indicated.
Review each section of code and understand how it works!
Review each section of code and understand how it works!
Assembly Syntax (Toggle Plain Text)
.data # variable declarations follow this line iSAFE: 1 iCHASM: 2 iTRAP: 3 iDESTINATION: 4 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_state: .word 0 robot_instructions: .space 64 # Robot State Message Table MsgRobotTbl: .word MsgRS1 .word MsgRS2 .word MsgRS3 .word MsgRS4 .word MsgRS5 MsgFailsDestination: .asciiz "Robot fails to reach Destination\n" MsgRS1: .asciiz "Robot is at a safe place\n" MsgRS2: .asciiz "Robot falls into Chasm\n" MsgRS3: .asciiz "Robot falls into the trap\n" MsgRS4: .asciiz "Robot reaches Destination\n" MsgRS5: .asciiz "Robot fails to reach Destination\n" MsgFinal: .asciiz "Robot final location X: " MsgY: .asciiz " Y: " MsgCR: .asciiz "\n" MsgRobotState: .asciiz "Robot State: \n" MsgFallenChasm: .asciiz "Robot has fallen into a chasm\n" MsgFallenTrap: .asciiz "Robot has fallen into a trap\n" MsgReachedDestination: .asciiz "Robot has reached Destination\n" MsgSafePlace: .asciiz "Robot is at a safe place\n" MsgFallsChasm: .asciiz "Robot falls into Chasm\n" MsgFallsTrap: .asciiz "Robot falls into the trap\n" MsgReachesDestination: .asciiz "Robot reaches Destination\n" .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 Entire Maze Array la $t0, maze # address of maze li $t3, 0 li $t1, 144 clr: # repeat if not finished yet. sb $t3, 0($t0) add $t0, $t0, 1 # address++ add $t1, $t1, -1 # count-- bgtz $t1, clr # _______________ # Setup variables la $t0, maze # base address li $t1,1 # iSAFE: sb $t1, 0x11($t0) # maze[1][1] = 1; li $t1,2 # iCHASM: sb $t1, 0x12($t0) # maze[1][2] = 2 sb $t1, 0x24($t0) # maze[2][4] = 2 li $t1, 3 #iTRAP: sb $t1, 0x23($t0) # maze[2][3] = 3 sb $t1, 0x34($t0) # maze[3][4] = 3 li $t1, 4 #iDESTINATION: sb $t1, 0x32($t0) # maze[3][2] = 4; # robot_x = 1 la $t0,robot_x li $t1,1 sw $t1,0($t0) # robot_y = 2 la $t0,robot_y li $t1,2 sw $t1,0($t0) # init robot_state = 5 la $t0,robot_state li $t1,5 sw $t1,0($t0) la $t7,robot_instructions # index li $t6,64 # Num of loops # _______________ # TOP of BIG Loop MoveLoop: # Get a Random # li $a0, 0 # Rng# li $a1, 4 # rand() %4 {0...3} li $v0, 42 syscall # Get RNG # $a0 = {0...3} add $a0, $a0, 1 # {1...4} # Store Robot instruction sb $a0,0($t7) # robot_instructions[i] = rnd{ 1...4 } add $t7, $t7, 1 # increment address add $a0, $a0, -1 bgtz $a0, RJump # ____________ # case 1: MOVE la $t1,robot_m_x la $t0,robot_x lw $t2,0($t1) sw $t2,0($t0) # robot_x = robot_m_x la $t1,robot_m_y la $t0,robot_y lw $t2,0($t1) add $t2,$t2,1 sw $t2,0($t0) # robot_y = robot_m_y + 1 j Processing # Goto Processing RJump: add $a0, $a0, -1 bgtz $a0, RLeft # ____________ # case 2: Jump la $t1,robot_m_x la $t0,robot_x lw $t2,0($t1) sw $t2,0($t0) # robot_x = robot_m_x la $t1,robot_m_y la $t0,robot_y lw $t2, 0($t1) add $t2, $t2, 2 sw $t2, 0($t0) # robot_y = robot_m_y + 2 j Processing # Goto Processing RLeft: add $a0, $a0, -1 bgtz $a0, RRight # ____________ # case 3: Left la $t1,robot_m_x la $t0,robot_x lw $t2,0($t1) add $t2, $t2, -1 sw $t2,0($t0) # robot_x = robot_m_x - 1 j Processing # Goto Processing # _____________ # case 4: Right RRight: la $t1,robot_m_x la $t0,robot_x lw $t2,0($t1) add $t2, $t2, 1 sw $t2,0($t0) # robot_x = robot_m_x + 1 # ______________________________ # Processing new robot positions! Processing: # System.out.println("Robot final location X: " + robot_x + " Y: " + robot_y); la $a0, MsgFinal li $v0, 4 # specify Print String service syscall # print ASCIIz la $t0,robot_x lw $a0,0($t0) li $v0, 1 syscall # CMD: print robot_x integer la $a0, MsgY li $v0, 4 # specify Print String service syscall # print ASCIIz la $t0,robot_y lw $a0,0($t0) li $v0, 1 syscall # CMD: print robot_Y integer la $a0, MsgCR li $v0, 4 # specify Print String service syscall # print CRLF # System.out.println ("Robot State: "); la $t0,robot_x lw $t0,0($t0) # $t0 = robot_x la $t5,robot_y lw $t5,0($t5) # $t5 = robot_y # Using $t1 ... $t4 since they look similar to 1...4 li $t1,1 li $t2,2 li $t3,3 li $t4,4 # _____________________________________________________________________________________ # Ref: if (( (robot_x == 1) && (robot_y == 2)) || ( (robot_x == 2) && (robot_y == 4)) ) bne $t0,$t1,LineA3 # jump if robot_x <> 1 beq $t5,$t2,LineAOK # jump if robot_y == 2 * Success * LineA3: # || ( (robot_x == 2) && (robot_y == 4)) ) bne $t0,$t2, LineB #jump if robot_x <> 2 bne $t5,$t4, LineB #jump if robot_y <> 4 LineAOK: # SUCCESS la $a0, MsgFallenChasm li $v0, 4 # specify Print String service syscall # print ASCIIz la $a0, robot_state sw $t2,0($a0) # robot_state = 2 # Process new robot positions j Done MoveHop: j MoveLoop # HOP to top of loop # ____________________________________________________________________________________ # Ref: else if(( (robot_x == 2) && (robot_y == 3)) || ((robot_x == 3) && (robot_y == 4))) LineB: bne $t0,$t2,LineB3 # jump if robot_x <> 2 beq $t5,$t3,LineBOK # jump if robot_y == 3 * Success * LineB3: # || ( (robot_x == 2) && (robot_y == 4)) ) bne $t0,$t3, LineC #jump if robot_x <> 3 bne $t5,$t4, LineC #jump if robot_y <> 4 LineBOK: # SUCCESS la $a0, MsgFallenTrap li $vo, 4 syscal #print heading la $a0, robot_state sw $t3, 0 ($a0) #robot_state=3 j Done # ___________________________________________ # Ref: if ((robot_x == 3) && (robot_y == 2)) LineC: bne $t0,$t3, LineD # jump if robot_x <> 3 bne $t5,$t2, LineD # jump if robot_y <> 2 # SUCCESS la $a0, MsgReachedDestination li $vo, 4 syscal #print ASCIIz la $a0, robot_state sw $t4, 0 ($a0) #robot_state=4 j Done # _________________________________________ LineD: la $a0, MsgSafePlace li $v0, 4 # specify Print String service syscall # print ASCIIz la $a0, robot_state sw $t4,0($a0) # robot_state = 1 # ___________________ # Print Robot State Done: la $a0, robot_state lw $t0,0($a0) # to is robot_state ### <--- Insert 4 case robot_state printing! # ________ # End Loop add $t6, $t6, -1 # count-- bgtz $t1, MoveHop # Hop back to top of big loop jr $ra # return
•
•
Join Date: Jun 2009
Posts: 82
Reputation:
Solved Threads: 0
Error in Assembly2 line 260 position 5: "$vo": operand is of incorrect type
Error in Assembly2 line 261 position 2: "syscal" directive cannot appear in text segment
Error in Assembly2 line 277 position 5: "$vo": operand is of incorrect type
Error in Assembly2 line 278 position 2: "syscal" directive cannot appear in text segment
Assemble: operation completed with errors.
Error in Assembly2 line 261 position 2: "syscal" directive cannot appear in text segment
Error in Assembly2 line 277 position 5: "$vo": operand is of incorrect type
Error in Assembly2 line 278 position 2: "syscal" directive cannot appear in text segment
Assemble: operation completed with errors.
•
•
Join Date: Jun 2009
Posts: 82
Reputation:
Solved Threads: 0
#robot_state print
Assembly Syntax (Toggle Plain Text)
Done: la $a0, robot_state lw $t0,0($a0) # to is robot_state la $a0, robot_state sw $t4,0($a0) # robot_state = 1 syscall la $a0, robot_state sw $t2,0($a0) # robot_state = 2 syscall la $a0, robot_state sw $t3, 0 ($a0) #robot_state=3 syscall la $a0, robot_state sw $t4, 0 ($a0) #robot_state=4 syscall
•
•
Join Date: Jun 2009
Posts: 82
Reputation:
Solved Threads: 0
Error in Assembly2 line 260 position 5: "$vo": operand is of incorrect type
Error in Assembly2 line 261 position 2: "syscal" directive cannot appear in text segment
Error in Assembly2 line 277 position 5: "$vo": operand is of incorrect type
Error in Assembly2 line 278 position 2: "syscal" directive cannot appear in text segment
Assemble: operation completed with errors.
Error in Assembly2 line 261 position 2: "syscal" directive cannot appear in text segment
Error in Assembly2 line 277 position 5: "$vo": operand is of incorrect type
Error in Assembly2 line 278 position 2: "syscal" directive cannot appear in text segment
Assemble: operation completed with errors.
You print the four robot_state lines but you aren't printing based upon if 1 or 2 or 3 or 4.
So without using conditionals, try this instead!
It uses the robot_state as an index into a string table and prints the appropriate string for states 1...5
There's probaby bugs but you should try to find them yourself by single steping the code and watching the values in the registers. If you have compile errors, look for typos, like those last errors you posted!
When you get curious enough, find the line count for your Java program and find the line count for this assembly file. Notice the difference! That's why these days higher level languages are used for application building and not assembly language. Assembly is left for specialty applications like drivers, and math libraries, etc.
So without using conditionals, try this instead!
It uses the robot_state as an index into a string table and prints the appropriate string for states 1...5
Assembly Syntax (Toggle Plain Text)
Done: la $a0, robot_state lw $t0,0($a0) # is state 1...5 add $t0,$t0,-1 # state is 0...4 sll $t0,$t0,2 # x4 0,4,8,12,16 Address offset # ASCIIz table lookup la $a0, MsgRobotTbl # table base add $a0, $a0, $t0 lw $a0,0($a0) li $vo, 4 syscal #print ASCIIz MsgRobotTbl[ robot_state ]
There's probaby bugs but you should try to find them yourself by single steping the code and watching the values in the registers. If you have compile errors, look for typos, like those last errors you posted!
When you get curious enough, find the line count for your Java program and find the line count for this assembly file. Notice the difference! That's why these days higher level languages are used for application building and not assembly language. Assembly is left for specialty applications like drivers, and math libraries, etc.
Last edited by wildgoose; Aug 15th, 2009 at 4:12 pm.
![]() |
Similar Threads
- Java system info (Java)
- PCSpim/Assembly HELP (Assembly)
- Sr. Core JAVA Technologist / Software Tool Engineer in Replay Solutions (Software Development Job Offers)
- Using java as an assembly language (Java)
- How much VB6 is still out there?!? (Visual Basic 4 / 5 / 6)
Other Threads in the Assembly Forum
- Previous Thread: First Assembly file problem
- Next Thread: Where To Start
| Thread Tools | Search this Thread |





