Skip to content

Commit eed842e

Browse files
committed
Fixes #3. Stack underflow error
1 parent 60bfd86 commit eed842e

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Memory/Constants.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const MEMORY_SIZE = 4096
99
// Stack size in number of words
1010
const STACK_SIZE = 256
1111

12+
// Number of bytes in a word
13+
const WORD_SIZE = 4
14+
1215
// XZR register number
1316
const XZR = 31
1417

Memory/InstructionMemory.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package memory
22

33
import (
4+
"fmt"
45
"errors"
56
ALU "github.com/coderick14/ARMed/ALU"
67
"regexp"
@@ -392,7 +393,7 @@ func (instruction *AddImmediateInstruction) parse() error {
392393
instruction.constant = uint(constant)
393394

394395
address := getRegisterValue(instruction.reg2) + int64(instruction.constant)
395-
if address >= MEMORY_SIZE {
396+
if address > MEMORY_SIZE * WORD_SIZE {
396397
return errors.New("Stack underflow error in : " + instruction.inst)
397398
}
398399

@@ -460,7 +461,7 @@ func (instruction *SubImmediateInstruction) parse() error {
460461
instruction.constant = uint(constant)
461462

462463
address := getRegisterValue(instruction.reg2) + int64(instruction.constant)
463-
if address < (MEMORY_SIZE - STACK_SIZE) {
464+
if address < (MEMORY_SIZE - STACK_SIZE) * WORD_SIZE {
464465
return errors.New("Stack overflow error in : " + instruction.inst)
465466
}
466467

0 commit comments

Comments
 (0)