Skip to content

Commit 9e70630

Browse files
committed
Add check for label instructions
1 parent 7f90707 commit 9e70630

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Memory/InstructionMemory.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import (
1111
type InstructionMemory struct {
1212
PC int64
1313
Instructions []string
14+
Labels map[string]int64
1415
}
1516

1617
var InstructionMem = InstructionMemory{
1718
PC: 0,
1819
Instructions: []string{},
20+
Labels: make(map[string]int64),
1921
}
2022

2123
var dataMemory = DataMemory{
@@ -46,7 +48,7 @@ func (instructionMemory *InstructionMemory) isValidPC() bool {
4648
}
4749

4850
/*
49-
* Function : validateAndExecuteInstruction
51+
* Function : ValidateAndExecuteInstruction
5052
* Details : checks instruction type, performs syntax analysis, parses the statement and executes it
5153
*/
5254

@@ -57,6 +59,17 @@ func (instructionMemory *InstructionMemory) ValidateAndExecuteInstruction() erro
5759

5860
var err error
5961

62+
// Check for labels
63+
labelRegex, _ := regexp.Compile("^([a-zA-Z][[:alnum:]]*)[[:space:]]*:")
64+
if labelRegex.MatchString(currentInstruction) {
65+
66+
indexColon := strings.Index(currentInstruction, ":")
67+
labelName := strings.TrimSpace(currentInstruction[:indexColon])
68+
currentInstruction = strings.TrimSpace(currentInstruction[indexColon+1:])
69+
instructionMemory.Labels[labelName] = instructionMemory.PC
70+
71+
}
72+
6073
if strings.HasPrefix(currentInstruction, "ADD ") {
6174

6275
currentInstructionObject := AddInstruction{inst: currentInstruction}

0 commit comments

Comments
 (0)