SCC.150 Assembler Emulator
A compiler for a simple assembler language
 All Files Functions
Macros | Functions
emulator.h File Reference
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

Go to the source code of this file.

Macros

#define MAX_PROG_LEN   99
 
#define MAX_LINE_LEN   80
 
#define MAX_OPCODE   10
 
#define MAX_REGISTER   4
 
#define OPCODE_LENGTH   3
 
#define ARG_LENGTH   4
 
#define OPCODE   0
 
#define ARG1   1
 
#define ARG2   2
 

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

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

Header file for emulator.c.

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