-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecondpass.c
79 lines (61 loc) · 1.87 KB
/
secondpass.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "parser.h"
#include "assembler.h"
#include "symbols.h"
#include "validator.h"
#include "commands.h"
#include "registers.h"
#include "secondpass.h"
#include "firstpass.h"
char * word;
/*Executes the second pass - replacing all the symbols with their correct values */
int executeSecondPass()
{
int i = 0, lineNum = 0, noErrorsFound = TRUE;
unsigned int instructionMemoryLength;
char *currInstructionWord ;
Symbol *currSymbol;
instructionMemoryLength = getInstructionCounter();
while(i < instructionMemoryLength)
{
currInstructionWord = getInstructionMemoryWord(i);
/*If it's a symbol that needs to be replaced with the actual symbol value*/
if (isalpha(currInstructionWord[0]))
{
if(isSymbolExists(currInstructionWord))
{
currSymbol = getSymbol(currInstructionWord);
setInstructionMemoryWord(i, convertBase10toBase2(currSymbol->value), LINKER_ABSOLUTE);
} else if(isExternSymbolExists(currInstructionWord)) /*If it's an external symbol set zero word*/
{
setInstructionMemoryWord(i, convertBase10toBase2(0), LINKER_EXTERNAL);
insertExternSymbolValue(currInstructionWord , i + IC_STARTUP_VALUE);
} else /*Report symbol not found error */
{
addAssemblerError(SYMBOL_NOT_VALID, lineNum);
noErrorsFound = FALSE;
}
}
else if (currInstructionWord[0] == OFFSET_MARK_LABEL)
{
word = strtok(currInstructionWord, OFFSET_MARK_STRING);
if(isSymbolExists(word))
{
currSymbol = getSymbol(word);
setInstructionMemoryWord(i, convertBase10toBase2(currSymbol->value- getInstructionCounter() + IC_STARTUP_VALUE), LINKER_RELOCATABLE);
} else
{
addAssemblerError(SYMBOL_NOT_VALID, lineNum);
noErrorsFound = FALSE;
}
} else /*It's an instruction so increase the line counter */
{
lineNum++;
}
i++;
}
return noErrorsFound;
}