Getting Started: Coding Assembly
How to Program a Robot
Robot Programmer Tutorial
Introduction
Writing code is extremely easy. There are a few commands for you to understand that you can use to control a robot. This manual will provide help on how to actually program a robot and is intended for beginners. Additional examples are available on the Internet.
As we progress through this guide we will build a list of key words and concepts used.
Each Robot consists of hardware and software. The software controls what the hardware does. To act as a "middleman" between the software and the hardware is the Operating System. Your cellphone most likely uses an IOS or Android Operating System; your computer most likely use Windows or Linux as an Operating System.
Here, we use an operating system named "BothOS".
BothOs
BothOS, or BOS, like any operating system contain what is known as "Registers" to store data, manipulate data and transfer data. Your job as a programmer is to write the code to set the registers at the right times to the "right" values. Since it is up to YOU to decide what a good strategy is for a robot it is therefor also up to YOU to decide what these "right values" are depending on your strategy. For example, to tell a gun to shoot in a direction you would need to tell this to BOS; then after this you may want to turn your robot around, which you also need to tell BOS; then maybe you want to speed up as fast as you can. Every action you want your robot to take during a match you need to tell BOS through code.
How does Assembly code look?
There are a few simple rules when it comes to telling BOS what to do:
Rule 1: One line represents one command.
For BOS to understand you you must sepereate each command by writing then below each other. For example sake, let say our command that we want to execute is "TEST" then we can ask BOS to execute it say 3 times by writing it 3 times in separate rows as
TEST
TEST
TEST
BOS will NOT accept multiple commands per line.
Rule 2: Commands can contain parameters.
A parameter is along with a command. A parameter tells BOS on what DATA you want it to execute the command on. For example, in the above ADD command example, which adds 2 numbers together, the command therefore accepts 2 parameters for it to make sense.
Thus, to add numbers together with the ADD command it would be used as such:
ADD 5 3 ;This would ADD 5 and 3 together, resulting in 8 (but the 8 is not stored anywhere)
ADD ax 5 ;This would ADD the AX register with 3, resulting in AX being 3 more than whatever it was before this command
ADD ax bx ;This would ADD the AX register with the BX register.
Some commands require no parameters, some require 1 parameter and some require 2 parameters. No commands require more than 2 parameters.
Rule 3: Commands can affect registers.
Once a command has executed, BOS needs to store the result in a register. If you had a value stored in the specific register used by a command then the value will be overwritten.
For example, the ADD ax 5 command would add 5 to the value of ax then STORE it in the ax register, while the ADD bx 5 would add 5 to the value of bx and store it in bx (and so on).
There are 19 assembly commands in total.
There are commands to help with mathematics, there are commands that help with communicating with devices and there are commands to do logical steps.
Your entire program can consist of a single command but your robot would not be very smart. Therefor you are free to use any set of commands to accomplish your strategy. BOS will accept your command, execute it and move to the next command. Once all the code in the Main program has executed past the last line of code then it will start again from the top. This loop will continue until the match is over or your bot has been destroyed.
Before understanding the code, lets take a look at the available "registers".
Registers
Registers are memory locations that your code will modify in order to tell BothOs what to do. Your program will use registers to store information. These registers can also be used for your own strategy purposes.
The registers available for you to use are named as follow:
Ax
Bx
Cx
Dx
Ex
Fx
Gx
There are also 3 registers that BothOs use and you should not use.
IP
CMPL
CMPR
Assembly Commands
There are 19 commands for you to use.
NOP
Parameters: 0
Description: Waste Time
Example Code:
NOP
NOP
NOP ;Wasted 3 clock cycles
MOV param1 param2
Parameters: 2
Description: Moves register or value parem2 into register parem1
Example Code:
MOV ax 3 ; moves the value of 3 into ax
MOV bx 5 ; moves the value of 5 into bx
MOV bx ax; moves the value of AX (which is 3 in this example) into bx and overwrite bx
INT param1
Parameters: 2
Description: Sends all the registers to an interrupt device. An "interrupt" device is a built-in device that you can access regardless of what components your robot are made out of. Things such as the match time, number of enemies etc. can be obtained via interrupt calls. There is an extra section dedicated to the INT command.
Example Code:
INT 1 ; This would cause interrupt device number 1 to execute.
INT 5 ; This would cause interrupt device number 5 to execute.
IO param1
Parameters: 2
Description: Sends all the registers to a device to use. The IO command is at the heart of every program you will use it to interact with the devices attached to your robot such as guns, legs, sensors and any other external device. There is an extra section dedicated to the IO command.
Example Code:
IO 0 ; This would interact with the device or weapon mapped at port 5,
IO 0 ; This would interact with the device or weapon mapped at port 1
ADD param1 param2
Parameters: 2
Description: Adds parem2 to parem1 and store it in parem1
Example Code:
ADD ax 5 ; adds 5 to the value of ax and store it in ax
SUB param1 param2
Parameters: 2
Description: Subracts parem2 from parem1 and store it in parem1
Example Code:
MOV ax 10 ; moves 10 into ax register
SUB ax 5 ; subtracts 5 from the value of ax (which is 10 from the line above) and store it in ax (thus ax is 5)
SUB ax 2 ; subtracts 5 from the value of ax (which is 5 from the line above) and store it in ax (thus ax is now 3)
MPY param1 param2
Parameters: 2
Description: Multiply parem2 by parem1 and store it in parem1
Example Code:
MOV ax 25 ; moves 25 into ax register
MOV bx 2 ; moves 2 into bx register
MPY ax 2 ; multiply the value of ax (which is 25 from the line above) with 2 and store it in ax (thus ax is 50)
MPY ax bx ; multiply the value of ax (which is 50 from the line above) with bx (which is 2) and store it in ax (thus ax is 100)
DIV param1 param2
Parameters: 2
Description: Divides parem1 by parem2 and store it in parem1
Example Code:
MOV ax 100 ; moves 100 into ax register
MOV bx 5 ; moves 5 into bx register
DIV ax 2 ; divides the value of ax (which is 100 from the line above) with 2 and store it in ax (thus ax is 50)
DIV ax bx ; divides the value of ax (which is 50 from the line above) with bx (which is 5) and store it in ax (thus ax is 10)
MOD param1 param2
Parameters: 2
Description: Mods parem1 by parem2 and store it in parem1
Example Code:
MOV ax 9; moves 9 into ax register
MOD ax 4 ; mods the value of ax (which is 9 from the line above) with 4 and store it in ax (thus ax is 1)
CALL param1
Parameters:1
Description: Calls/Executes a function stored outside of your code.
Example Code:
Call SecondFunction; will execute the code stored in the function named "SecondFunction"
CMP param1 param2
Parameters: 2
Description: Compared param1 with param2 and stores the result
Example Code:
CMP ax, 5; compare the value stored in the ax register with a value of "5"
CMO bx ax ; compare the value stored in the bx register with the value stored in the ax register.
JMP param1
Parameters: 1
Description: Jumps code execution to start at the label defined by param1
Example Code:
MOV AX 1 ; moves the value of 1 into the ax register
LBL ExampleJump ; define a label named "ExampleJump"
MOV AX 2 ; moves the value of 1 into the ax register
JMP ExampleJump ; jumps code execution to the label address of "ExampleJump"
MOV AX 4 ; this line of code will not execute because the jump does not allow code execution to ever actually reach here.
JLS param1 param2
Parameters: 2
Description: Jumps to a defined label IF param1 is LESS than param2
Example Code:
MOV ax 2
MOD bx 3
CMP ax bx ; compares ax to bx
JLS "ShootEnemy" ; if ax is LESS than bx then code execution will continue at the label defined as "ShootEnemy"
JGR param1 param2
Parameters: 2
Description: Jumps to a defined label IF param1 is GREATER than param2
Example Code:
MOV ax 2
MOD bx 1
CMP ax bx ; compares ax to bx
JGR "ShootEnemy" ; if ax is GREATER than bx then code execution will continue at the label defined as "ShootEnemy"
JNE param1 param2
Parameters: 2
Description: Jumps to a defined label IF param1 is NOT EQUAL TO param2
Example Code:
MOV ax 2
MOD bx 3
CMP ax bx ; compares ax to bx
JNE "ShootEnemy" ; if ax is NOT EQUAL TO bx then code execution will continue at the label defined as "ShootEnemy"
JEQ param1 param2
Parameters: 2
Description: Jumps to a defined label IF param1 is EQUAL TO param2
Example Code:
MOV ax 2
MOD bx 3
CMP ax bx ; compares ax to bx
JEQ "ShootEnemy" ; if ax is EQUAL to bx then code execution will continue at the label defined as "ShootEnemy"
JGE param1 param2
Parameters: 2
Description: Jumps to a defined label IF param1 is GREATER AND EQUAL TO param2
Example Code:
MOV ax 2;
MOD bx 3 ;
CMP ax bx ; compares ax to bx
JLS "ShootEnemy" ; if ax is GREATER AND EQUAL to bx then code execution will continue at the label defined as "ShootEnemy"
JLE param1 param2
Parameters: 2
Description: Jumps to a defined label IF param1 is LESS THAN AND EQUAL To param2
Example Code:
MOV ax 2; moves 9 into ax register
MOD bx 3 ; mods the value of ax (which is 9 from the line above) with 4 and store it in ax (thus ax is 1)
CMP ax bx ; compares ax to bx
JLS "ShootEnemy" ; if ax is LESS THAN AND EQUAL TO bx then code execution will continue at the label defined as "ShootEnemy"
Interrupts
INT 0 - Self Destruct
Input: NONE
Output: NONE
Description: Destroy self and take as many enemies with me as possible.
INT 1 - Reset
Input: NONE
Output: NONE
Description: Reset program to start over.
INT 2 - Locate
Input: NONE
Output: NONE
Description: Gets the robot's own location.
INT 3 - Overburn
Input: NONE
Output: NONE
Description: Sets the overburn & overclocking rate.
INT 4 - ID
Input: NONE
Output: NONE
Description: Gets the robot's pown ID for the match.
INT 5 - Timer
Input: NONE
Output: NONE
Description: Get the current match time.
INT 6 - FindAngle
Input: NONE
Output: NONE
Description: Finds an angle between two points.
INT 7 - TargetID
Input: NONE
Output: NONE
Description: NONE
INT 8 - TargetInfo
Input:
AX - The ID of the robot whose location you want.
Output:
EX - Found robot's X position.
FX - Found robot's Y position (height above ground).
GX - Found robot's Z position.
Description: Gets an other robot's location
INT 9 - GameInfo
Input: NONE
Output: NONE
Description: NONE
INT 10 - RobotInfo
Input: NONE
Output: NONE
Description: NONE
INT 11 - Collisions
Input: NONE
Output: NONE
Description: NONE
INT 12 - ResetCollisionCount
Input: NONE
Output: NONE
Description: NONE
INT 13 - Transmit
Input: NONE
Output: NONE
Description: NONE
INT 14 - Receive
Input: NONE
Output: NONE
Description: NONE
INT 15 - DataReady
Input: NONE
Output: NONE
Description: NONE
INT 16 - ClearCom
Input: NONE
Output: NONE
Description: NONE
INT 17 - KillsDeaths
Input: NONE
Output: NONE
Description: NONE
INT 18 - ScanClosestTarget
Input: NONE
Output:
EX - Closest found robot's ID.
Description: Returns the ID of the robot with the closest distance from self.
IO Devices
Every device attached to your robot has an IO (Input/Output) number that you can change to any number you wish. The default IO number for all new devices attached to a robot is IO 0. This means, by default, every device attached to your robot will respond to IO 0 being called. Generally, you would distinguish your devices with different IO numbers to allow you to interact with them independently (such as you robot legs needing to move forward) while also allowing you to group devices to use the same IO number (such as having all your weapons in one IO group to allow you to fire them all off simultaneously.
IO device has requires information to be passed to it in the memory registers. For more information on different IO devices please see the IO Devices manual
Assembly Examples
Strategy: Stand still, target enemies and shoot.
int 18
mov ax ex
int 8
mov ax 1
mov bx ex
mov cx fx
mov dx gx
io 0