My Project
assembler.h
Go to the documentation of this file.
1 #ifndef __ASSEMBLER_H__
2 #define __ASSEMBLER_H__
3 
4 /*
5  * "Copyright (c) 2014 by Fritz Sieker."
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation for any purpose, without fee, and without written
9  * agreement is hereby granted, provided that the above copyright notice
10  * and the following two paragraphs appear in all copies of this software.
11  *
12  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
13  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
14  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR
15  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16  *
17  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
20  * BASIS, AND THE AUTHOR NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
21  * UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
22  */
23 
57 #include "lc3.h"
58 
59 #include "symbol.h"
60 
61 #ifndef LC3AS_VAR
62 #define LC3AS_VAR extern
63 #endif
64 
66 #define ERR_OPEN_READ "could not open '%s' for reading."
67 #define ERR_OPEN_WRITE "could not open '%s' for writing."
68 #define ERR_LINE_TOO_LONG "source line too long (max is %d)"
69 #define ERR_NO_ORIG "no .ORIG directive found"
70 #define ERR_NO_END "no .END directive found"
71 #define ERR_ORIG_NOT_1ST "instruction(s) appear before .ORIG"
72 #define ERR_END_NOT_LAST "instruction(s) appear after .END"
73 #define ERR_MISSING_ORIG ".ORIG not found"
74 #define ERR_MISSING_END ".END not found"
75 #define ERR_EXPECTED_COMMA "expected comma, got '%s'"
76 #define ERR_EXPECTED_REG "expected register (R0-R7), got '%s'"
77 #define ERR_EXPECT_REG_IMM "expected register or immediate, got '%s'"
78 #define ERR_BAD_LABEL "label '%s' contains illegal characters"
79 #define ERR_MISSING_OP "expected LC3 op, got '%s'"
80 #define ERR_MISSING_OPERAND "too few operand(s)"
81 #define ERR_EXTRA_OPERAND "extra operand(s) '%s'"
82 #define ERR_DUPLICATE_LABEL "label '%s' previosly defined"
83 #define ERR_MISSING_LABEL "label '%s' never defined"
84 #define ERR_BAD_PCOFFSET "PCoffset to '%s' out of range"
85 #define ERR_BAD_IMM "immediate '%s' (bad format)"
86 #define ERR_IMM_TOO_BIG "immediate '%s' out of range"
87 #define ERR_EXPECTED_STR "expected quoted string, got '%s'"
88 #define ERR_BAD_STR "unterminated string '%s'"
89 
91 LC3AS_VAR int srcLineNum;
92 
94 LC3AS_VAR int currAddr;
95 
97 LC3AS_VAR int numErrors;
98 
100 typedef struct line_info line_info_t;
101 
106 struct line_info {
108  int lineNum;
109  int address;
112  int form;
113  int reg1;
114  int reg2;
115  int reg3;
116  int immediate;
117  char* reference;
118 };
119 
120 
128 void asm_error (char* msg, ...);
129 
131 void asm_init (void);
132 
139 void asm_init_line_info (line_info_t* info);
140 
171 void asm_pass_one (char* asm_file_name, char* sym_file_name);
172 
180 void asm_pass_two(char* obj_file_name);
181 
187 void asm_print_line_info (line_info_t* info);
188 
190 void asm_term (void);
191 
201 char* check_for_label (char* token);
202 
225 void check_line_syntax (char *token);
226 
236 
244 void encode_PCoffset_or_error (int width);
245 
249 void get_comma_or_error (void);
250 
261 void get_immediate_or_error (char* token, int width, int isSigned);
262 
272 void get_PCoffset_or_error (char* token);
273 
281 void get_operand (operand_t operand, char* token);
282 
289 int get_reg_or_error (char* token);
290 
296 FILE *open_read_or_error (char* file_name);
297 
303 FILE *open_write_or_error (char* file_name);
304 
328 
345 void update_address (void);
346 
347 #endif
void asm_init(void)
Definition: assembler.c:59
void scan_operands(operands_t operands)
Definition: assembler.c:141
Defines the interface to symbol.c functions (do not modify)
void encode_operand(operand_t operand)
Definition: assembler.c:88
void asm_print_line_info(line_info_t *info)
Definition: assembler.c:39
int lineNum
Definition: assembler.h:108
void get_comma_or_error(void)
Definition: assembler.c:96
enum operand operand_t
int address
Definition: assembler.h:109
void get_PCoffset_or_error(char *token)
Definition: assembler.c:103
opcode_t opcode
Definition: assembler.h:111
void asm_pass_one(char *asm_file_name, char *sym_file_name)
Definition: assembler.c:66
operand
Definition: lc3.h:115
FILE * open_read_or_error(char *file_name)
Definition: assembler.c:112
void asm_error(char *msg,...)
Definition: assembler.c:49
int form
Definition: assembler.h:112
enum opcode opcode_t
char * reference
Definition: assembler.h:117
void asm_term(void)
Definition: assembler.c:74
LC3AS_VAR int numErrors
Definition: assembler.h:97
void check_line_syntax(char *token)
Definition: assembler.c:83
void update_address(void)
Definition: assembler.c:156
LC3AS_VAR int currAddr
Definition: assembler.h:94
line_info_t * next
Definition: assembler.h:107
LC3AS_VAR int srcLineNum
Definition: assembler.h:91
int reg2
Definition: assembler.h:114
void asm_init_line_info(line_info_t *info)
Definition: assembler.c:23
int reg3
Definition: assembler.h:115
Definition: assembler.h:106
int get_reg_or_error(char *token)
Definition: assembler.c:107
int reg1
Definition: assembler.h:113
int machineCode
Definition: assembler.h:110
enum operands operands_t
char * check_for_label(char *token)
Definition: assembler.c:78
void encode_PCoffset_or_error(int width)
FILE * open_write_or_error(char *file_name)
Definition: assembler.c:117
int immediate
Definition: assembler.h:116
void asm_pass_two(char *obj_file_name)
Definition: assembler.c:70
operands
Definition: lc3.h:142
definitions of the LC3 instruction set architecture (ISA) (do not modify)
void get_immediate_or_error(char *token, int width, int isSigned)
Definition: assembler.c:100
void get_operand(operand_t operand, char *token)
Definition: assembler.c:122