Skip to content

Commit fdb13ee

Browse files
committed
Fixes #2. Add support for MUL instruction
1 parent eed842e commit fdb13ee

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

ALU/ALU.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ func Adder(val1, val2 int64) int64 {
55
return val1 + val2
66
}
77

8+
//Function to perform signed multiplication of two int64 numbers.
9+
func Multiplier(val1, val2 int64) int64 {
10+
return val1 * val2
11+
}
12+
813
//Function to perform logical AND operation of two int64 numbers.
914
func LogicalAND(val1, val2 int64) int64 {
1015
return val1 & val2

Memory/InstructionMemory.go

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

33
import (
4-
"fmt"
54
"errors"
65
ALU "github.com/coderick14/ARMed/ALU"
76
"regexp"
@@ -78,6 +77,11 @@ func (instructionMemory *InstructionMemory) ValidateAndExecuteInstruction() erro
7877
currentInstructionObject := SubInstruction{inst: currentInstruction}
7978
err = executeInstruction(&currentInstructionObject)
8079

80+
} else if strings.HasPrefix(currentInstruction, "MUL ") {
81+
82+
currentInstructionObject := MulInstruction{inst: currentInstruction}
83+
err = executeInstruction(&currentInstructionObject)
84+
8185
} else if strings.HasPrefix(currentInstruction, "ADDI ") {
8286

8387
currentInstructionObject := AddImmediateInstruction{inst: currentInstruction}
@@ -360,6 +364,59 @@ func (instruction *SubInstruction) execute() {
360364
InstructionMem.updatePC()
361365
}
362366

367+
/*
368+
INSTRUCTION : MULTIPLICATION
369+
370+
Example : MUL X1, X2, X3
371+
Meaning : X1 = X2 * X3
372+
*/
373+
type MulInstruction struct {
374+
inst string
375+
reg1 uint
376+
reg2 uint
377+
reg3 uint
378+
}
379+
380+
func (instruction *MulInstruction) checkSyntax() error {
381+
r, _ := regexp.Compile("^MUL X([0-9]|1[0-9]|2[0-7]), X(ZR|[0-9]|1[0-9]|2[0-7]), X(ZR|[0-9]|1[0-9]|2[0-7])$")
382+
if r.MatchString(instruction.inst) == false {
383+
return errors.New("Syntax error occurred in " + instruction.inst)
384+
}
385+
return nil
386+
}
387+
388+
func (instruction *MulInstruction) parse() error {
389+
statement := instruction.inst
390+
var registers [3]int
391+
var i, indexX, indexComma int
392+
for i = 0; i < 3; i++ {
393+
indexX = strings.Index(statement, "X")
394+
indexComma = strings.Index(statement, ",")
395+
if indexComma == -1 {
396+
indexComma = len(statement)
397+
}
398+
if statement[indexX+1:indexComma] == "ZR" {
399+
registers[i] = XZR
400+
} else {
401+
registers[i], _ = strconv.Atoi(statement[indexX+1 : indexComma])
402+
}
403+
if indexComma < len(statement) {
404+
statement = statement[indexComma+1:]
405+
}
406+
}
407+
instruction.reg1 = uint(registers[0])
408+
instruction.reg2 = uint(registers[1])
409+
instruction.reg3 = uint(registers[2])
410+
411+
return nil
412+
}
413+
414+
func (instruction *MulInstruction) execute() {
415+
result := ALU.Multiplier(getRegisterValue(instruction.reg2), getRegisterValue(instruction.reg3))
416+
setRegisterValue(instruction.reg1, result)
417+
InstructionMem.updatePC()
418+
}
419+
363420
/*
364421
INSTRUCTION : ADD IMMEDIATE
365422

0 commit comments

Comments
 (0)