Typedefs | |
typedef int(* | opcode_function )(char *, char *, char *) |
Functions | |
int | opcodeNOP (char *opcode, char *arg1, char *arg2) |
int | opcodeSET (char *opcode, char *arg1, char *arg2) |
int | opcodeAND (char *opcode, char *arg1, char *arg2) |
int | opcodeOR (char *opcode, char *arg1, char *arg2) |
int | opcodeADD (char *opcode, char *arg1, char *arg2) |
int | opcodeSUB (char *opcode, char *arg1, char *arg2) |
int | opcodeSHL (char *opcode, char *arg1, char *arg2) |
int | opcodeSHR (char *opcode, char *arg1, char *arg2) |
int | opcodeJMP (char *opcode, char *arg1, char *arg2) |
int | opcodePRT (char *opcode, char *arg1, char *arg2) |
int | getArg2 (char *arg2) |
bool | isValid (char *opcode, char *arg1, char *arg2) |
int | execInstruction (char *instruction) |
int | execProgram () |
int | loadProgram () |
int | main () |
This file is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
This program emulates programs written in the SCC.150 Assembler language. The language is a very simple assembly language that comprises of only ten opcodes:
NOP
or 'No OPeration', which performs no task
SET ARG1 ARG2
, which puts the value in ARG2
into the register indicated in ARG1
AND ARG1 ARG2
, which performs a bitwise AND operation on the values in ARG1
and ARG2
OR ARG1 ARG2
, which performs a bitwise OR operation on the values in ARG1
and ARG2
ADD ARG1 ARG2
, which performs an addition operation on the values in ARG1
and ARG2
SUB ARG1 ARG2
, which performs a subtraction operation on the values in ARG1
and ARG2
SHL ARG1 ARG2
, which performs a bitwise shift left on the value in ARG1
, to the number indicated in ARG2
SHR ARG1 ARG2
, which performs a bitwise shift right on the value in ARG1
, to the number indicated in ARG2
JMP ARG1
, which jumps to the line indicated by ARG1
if the value in REGX
is 0
PRT ARG1
, which prints the value in ARG1
Five registers are used: REGA
, REGB
, REGC
, REGX
and INSP
, or 'INStruction Pointer'.
Comments in the language are indicated by the line beginning with a '#'.
int execInstruction | ( | char * | instruction | ) |
Executes an instruction.
Tests the validity of an instruction (and whether or not it's a comment), and if the line passes, calls the appropriate function.
instruction | the instruction to test |
int execProgram | ( | ) |
Executes an the program.
Runs execInstruction
on each line of the program in turn.
int getArg2 | ( | char * | arg2 | ) |
A function to return the value in the second argument of an instruction.
Returns either the value in the indicated register or, as the instruction is parsed as a string, the integer value given. mystrncmp()
is used over mystrcmp()
because there tends to be dodgy C string magic tacked on to the end of the args.
arg2 | the value to parse |
bool isValid | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
Tests if an instruction is valid.
Tests that the opcode is valid and that the registers are also.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument (not applicable for PRT ) |
true
if the string is valid, false
if it is not int loadProgram | ( | ) |
Loads a program.
Loads the program file and reads each line into an array.
int main | ( | ) |
Runs the program.
int opcodeADD | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the ADD
opcode
Performs addition on the values in arg1
and arg2
, storing the result in arg1
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument |
int opcodeAND | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the AND
opcode
Performs a bitwise AND on the values in arg1
and arg2
, storing the result in arg1
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument |
int opcodeJMP | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the JMP
opcode
If REGX
contains 0, jumps to the line indicated by arg1
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument (not applicable for JMP ) |
int opcodeNOP | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the NOP
opcode
Increments the instruction pointer INSP
.
opcode | the opcode |
arg1 | the first argument (not applicable for NOP ) |
arg2 | the second argument (not applicable for NOP ) |
int opcodeOR | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the OR
opcode
Performs a bitwise OR on the values in arg1
and arg2
, storing the result in arg1
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument |
int opcodePRT | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the PRT
opcode
Prints the value in the register indicated in arg1
, or the integer value specified.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument (not applicable for PRT ) |
int opcodeSET | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the SET
opcode
Gets the value of either the register or integer set in arg2
and places it in the register indicated by arg1
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument |
int opcodeSHL | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the SHL
opcode
Performs a bitwise shift left on the value in arg1
, storing the result in arg1
. The number of shifts is specified in arg2
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument |
int opcodeSHR | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the SHR
opcode
Performs a bitwise shift right on the value in arg1
, storing the result in arg1
. The number of shifts is specified in arg2
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument |
int opcodeSUB | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the SUB
opcode
Subtracts the value in arg2
from the value in arg1
, storing the result in arg1
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument |
opcode_function opcodeFunc[] |
const char* opcodeStr[] |