SCC.150 Assembler Emulator
A compiler for a simple assembler language
 All Files Functions
Typedefs | Functions | Variables
emulator.c File Reference
#include "emulator.h"
#include "mystring.h"

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 ()
 

Variables

char prog [MAX_PROG_LEN][MAX_LINE_LEN]
 
int progLen = 0
 
unsigned int programRuns = 0
 
unsigned int REGA = 0
 
unsigned int REGB = 0
 
unsigned int REGC = 0
 
unsigned int REGX = 0
 
unsigned int INSP = 0
 
const char * register_str [] = {"REGA", "REGB", "REGC", "REGX"}
 
const char * opcodeStr []
 
opcode_function opcodeFunc []
 

Detailed Description

Author
Ben Goldsworthy (rumps) bgold.nosp@m.swor.nosp@m.thy96.nosp@m.@gma.nosp@m.il.co.nosp@m.m
Version
1.0

LICENSE

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

DESCRIPTION

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 '#'.

Function Documentation

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.

Parameters
instructionthe instruction to test
Returns
0 on success, -1 on failure
int execProgram ( )

Executes an the program.

Runs execInstruction on each line of the program in turn.

Returns
0 on success, -1 on failure
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.

Parameters
arg2the value to parse
Returns
an integer
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.

Parameters
opcodethe opcode
arg1the first argument
arg2the second argument (not applicable for PRT)
Returns
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.

Returns
0 on success, -1 on failure
int main ( )

Runs the program.

Returns
0 on success, -1 on failure
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.

Parameters
opcodethe opcode
arg1the first argument
arg2the second argument
Returns
0 on success
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.

Parameters
opcodethe opcode
arg1the first argument
arg2the second argument
Returns
0 on success
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.

Parameters
opcodethe opcode
arg1the first argument
arg2the second argument (not applicable for JMP)
Returns
0 on success
int opcodeNOP ( char *  opcode,
char *  arg1,
char *  arg2 
)

A function to handle the NOP opcode

Increments the instruction pointer INSP.

Parameters
opcodethe opcode
arg1the first argument (not applicable for NOP)
arg2the second argument (not applicable for NOP)
Returns
0 on success
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.

Parameters
opcodethe opcode
arg1the first argument
arg2the second argument
Returns
0 on success
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.

Parameters
opcodethe opcode
arg1the first argument
arg2the second argument (not applicable for PRT)
Returns
0 on success
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.

Parameters
opcodethe opcode
arg1the first argument
arg2the second argument
Returns
0 on success
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.

Parameters
opcodethe opcode
arg1the first argument
arg2the second argument
Returns
0 on success
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.

Parameters
opcodethe opcode
arg1the first argument
arg2the second argument
Returns
0 on success
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.

Parameters
opcodethe opcode
arg1the first argument
arg2the second argument
Returns
0 on success

Variable Documentation

opcode_function opcodeFunc[]
Initial value:
int opcodeJMP(char *opcode, char *arg1, char *arg2)
Definition: emulator.c:291
int opcodeOR(char *opcode, char *arg1, char *arg2)
Definition: emulator.c:172
int opcodeSHR(char *opcode, char *arg1, char *arg2)
Definition: emulator.c:268
int opcodeSUB(char *opcode, char *arg1, char *arg2)
Definition: emulator.c:220
int opcodePRT(char *opcode, char *arg1, char *arg2)
Definition: emulator.c:311
int opcodeAND(char *opcode, char *arg1, char *arg2)
Definition: emulator.c:148
int opcodeADD(char *opcode, char *arg1, char *arg2)
Definition: emulator.c:196
int opcodeSET(char *opcode, char *arg1, char *arg2)
Definition: emulator.c:124
int opcodeNOP(char *opcode, char *arg1, char *arg2)
Definition: emulator.c:107
int opcodeSHL(char *opcode, char *arg1, char *arg2)
Definition: emulator.c:244
const char* opcodeStr[]
Initial value:
= {"NOP", "SET", "AND", "OR", "ADD",
"SUB", "SHL", "SHR", "JMP", "PRT"}