Difference between revisions of "Jasmin/Example"

From Robowiki
Jump to navigation Jump to search
m
(a more complete example :))
Line 4: Line 4:
 
; File:      cs/Nano.j
 
; File:      cs/Nano.j
 
; Author:    Robert Maupin, 3 December 2012
 
; Author:    Robert Maupin, 3 December 2012
; License:  None (use as you like)
 
 
; -------------------------------------------------------------------------
 
; -------------------------------------------------------------------------
  
; WARNING: you might not ever be able to decompile this class
+
; WARNING: you will never ever be able to decompile this class
  
 
.class public cs/Nano
 
.class public cs/Nano
 
.super robocode/AdvancedRobot
 
.super robocode/AdvancedRobot
 +
 +
;specify a field
 +
.field private direction D
  
 
; initializer
 
; initializer
 
.method public <init>()V
 
.method public <init>()V
  aload_0
+
.limit stack 4
  invokenonvirtual robocode/AdvancedRobot/<init>()V
+
aload_0
  return
+
invokenonvirtual robocode/AdvancedRobot/<init>()V
 +
 +
aload_0
 +
dconst_1
 +
putfield cs/Nano/direction D
 +
 +
return
 
.end method
 
.end method
  
Line 23: Line 31:
 
;these don't have to be precise, just big enough to hold everything :)
 
;these don't have to be precise, just big enough to hold everything :)
 
.limit stack 8
 
.limit stack 8
.limit locals 8
+
.limit locals 4
 
aload_0 ; the 'this' statement...
 
aload_0 ; the 'this' statement...
 
 
Line 35: Line 43:
 
dconst_0
 
dconst_0
 
ddiv
 
ddiv
invokevirtual robocode/AdvancedRobot/turnRadarRightRadians(D)V
+
    invokevirtual robocode/AdvancedRobot/turnRadarRightRadians(D)V
 
      
 
      
 
return
 
return
Line 42: Line 50:
 
;turn onScannedRobot...
 
;turn onScannedRobot...
 
.method public onScannedRobot(Lrobocode.ScannedRobotEvent;)V
 
.method public onScannedRobot(Lrobocode.ScannedRobotEvent;)V
;these don't have to be precise, just big enough to hold everything
+
;these don't have to be precise, just big enough to hold everything :)
.limit stack 16
+
.limit stack 8
.limit locals 8
+
.limit locals 4
 
 
 
;if (1 < getOthers())
 
;if (1 < getOthers())
Line 62: Line 70:
 
 
 
skipInfinityLock:
 
skipInfinityLock:
 +
 +
;setTurnGunRightRadians(Utils.normalRelativeAngle(getHeadingRadians() +
 +
; e.getBearingRadians() - getGunHeadingRadians()))
 +
 +
;load this early for setTurnGunRightRadians
 +
;so we don't have to do stack manipulation later
 +
aload_0
 +
 +
;;;get the angle to the enemy
 +
;getHeadingRadians()
 +
aload_0
 +
invokevirtual robocode/AdvancedRobot/getHeadingRadians()D
 +
 +
;e.getBearingRadians()
 +
aload_1
 +
invokevirtual robocode/ScannedRobotEvent/getBearingRadians()D
 +
dadd ;add the two values
 +
 +
;getGunHeadingRadians()
 +
aload_0
 +
invokevirtual robocode/AdvancedRobot/getGunHeadingRadians()D
 +
dsub
 +
 +
;normalRelativeAngle(value)
 +
invokestatic robocode/util/Utils/normalRelativeAngle(D)D
 +
 +
;setTurnGunRightRadians(value)
 +
invokevirtual robocode/AdvancedRobot/setTurnGunRightRadians(D)V
 +
 +
;setTurnRightRadians(Math.cos(e.getBearingRadians()));
 +
aload_0
 +
aload_1
 +
invokevirtual robocode/ScannedRobotEvent/getBearingRadians()D
 +
invokestatic java/lang/Math/cos(D)D
 +
invokevirtual robocode/AdvancedRobot/setTurnRightRadians(D)V
 +
 +
;setAhead(5*direction)
 +
aload_0
 +
iconst_5
 +
i2d
 +
aload_0
 +
getfield cs/Nano/direction D
 +
dmul
 +
invokevirtual robocode/AdvancedRobot/setAhead(D)V
 +
 +
;setFire(3)
 +
aload_0
 +
iconst_3
 +
i2d
 +
invokevirtual robocode/AdvancedRobot/setFire(D)V
 +
 +
return
 +
.end method
 +
 +
 +
;turn onHitWall...
 +
.method public onHitWall(Lrobocode.HitWallEvent;)V
 +
;these don't have to be precise, just big enough to hold everything :)
 +
.limit stack 4
 +
.limit locals 4
 +
 +
;direction = -direction;
 +
 +
aload 0
 +
dup
 +
getfield cs/Nano/direction D
 +
dneg
 +
putfield cs/Nano/direction D
 
 
 
return
 
return
 
.end method
 
.end method
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 08:30, 14 December 2012

Example of a simple robot written in Jasmin Assembly code.

; --- Copyright Robert Maupin 2012. All rights reserved. -----------------
; File:      cs/Nano.j
; Author:    Robert Maupin, 3 December 2012
; -------------------------------------------------------------------------

; WARNING: you will never ever be able to decompile this class

.class public cs/Nano
.super robocode/AdvancedRobot

;specify a field
.field private direction D

; initializer
.method public <init>()V
	.limit stack 4
	aload_0
	invokenonvirtual robocode/AdvancedRobot/<init>()V
	
	aload_0
	dconst_1
	putfield cs/Nano/direction D
	
	return
.end method

;run method
.method public run()V
	;these don't have to be precise, just big enough to hold everything :)
	.limit stack 8
	.limit locals 4
	aload_0 ; the 'this' statement...
	
	;setAdjustGunForRobotTurn(true);
	iconst_1
	invokevirtual robocode/AdvancedRobot/setAdjustGunForRobotTurn(Z)V
	
	aload_0
	;turnRadarRightRadians(1.0/0.0);
	dconst_1
	dconst_0
	ddiv
    invokevirtual robocode/AdvancedRobot/turnRadarRightRadians(D)V
    
	return
.end method

;turn onScannedRobot...
.method public onScannedRobot(Lrobocode.ScannedRobotEvent;)V
	;these don't have to be precise, just big enough to hold everything :)
	.limit stack 8
	.limit locals 4
	
	;if (1 < getOthers())
	aload_0
	invokevirtual robocode/AdvancedRobot/getOthers()I
	iconst_1
	
	;this is 3 bytes.... all jumps are (cept jsr_w, which is 5)
	if_icmplt skipInfinityLock
	
	;Infinity Lock Radar
	;setTurnRadarLeftRadians(getRadarTurnRemainingRadians());
	aload_0
	dup
	invokevirtual robocode/AdvancedRobot/getRadarTurnRemainingRadians()D
	invokevirtual robocode/AdvancedRobot/setTurnRadarLeftRadians(D)V
	
	skipInfinityLock:
	
	;setTurnGunRightRadians(Utils.normalRelativeAngle(getHeadingRadians() +
	;	e.getBearingRadians() - getGunHeadingRadians()))
	
	;load this early for setTurnGunRightRadians
	;so we don't have to do stack manipulation later
	aload_0 
	
	;;;get the angle to the enemy
	;getHeadingRadians()
	aload_0
	invokevirtual robocode/AdvancedRobot/getHeadingRadians()D
	
	;e.getBearingRadians()
	aload_1
	invokevirtual robocode/ScannedRobotEvent/getBearingRadians()D
	dadd ;add the two values
	
	;getGunHeadingRadians()
	aload_0
	invokevirtual robocode/AdvancedRobot/getGunHeadingRadians()D
	dsub
	
	;normalRelativeAngle(value)
	invokestatic robocode/util/Utils/normalRelativeAngle(D)D
	
	;setTurnGunRightRadians(value)
	invokevirtual robocode/AdvancedRobot/setTurnGunRightRadians(D)V
	
	;setTurnRightRadians(Math.cos(e.getBearingRadians()));
	aload_0 
	aload_1
	invokevirtual robocode/ScannedRobotEvent/getBearingRadians()D
	invokestatic java/lang/Math/cos(D)D
	invokevirtual robocode/AdvancedRobot/setTurnRightRadians(D)V
	
	;setAhead(5*direction)
	aload_0
	iconst_5
	i2d
	aload_0
	getfield cs/Nano/direction D
	dmul
	invokevirtual robocode/AdvancedRobot/setAhead(D)V
	
	;setFire(3)
	aload_0
	iconst_3
	i2d
	invokevirtual robocode/AdvancedRobot/setFire(D)V
	
	return
.end method


;turn onHitWall...
.method public onHitWall(Lrobocode.HitWallEvent;)V
	;these don't have to be precise, just big enough to hold everything :)
	.limit stack 4
	.limit locals 4
	
	;direction = -direction;
	
	aload 0
	dup
	getfield cs/Nano/direction D
	dneg
	putfield cs/Nano/direction D
	
	return
.end method