|
1 | 1 | package memory
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 | 4 | "errors"
|
6 | 5 | ALU "github.com/coderick14/ARMed/ALU"
|
7 | 6 | "regexp"
|
@@ -78,6 +77,11 @@ func (instructionMemory *InstructionMemory) ValidateAndExecuteInstruction() erro
|
78 | 77 | currentInstructionObject := SubInstruction{inst: currentInstruction}
|
79 | 78 | err = executeInstruction(¤tInstructionObject)
|
80 | 79 |
|
| 80 | + } else if strings.HasPrefix(currentInstruction, "MUL ") { |
| 81 | + |
| 82 | + currentInstructionObject := MulInstruction{inst: currentInstruction} |
| 83 | + err = executeInstruction(¤tInstructionObject) |
| 84 | + |
81 | 85 | } else if strings.HasPrefix(currentInstruction, "ADDI ") {
|
82 | 86 |
|
83 | 87 | currentInstructionObject := AddImmediateInstruction{inst: currentInstruction}
|
@@ -360,6 +364,59 @@ func (instruction *SubInstruction) execute() {
|
360 | 364 | InstructionMem.updatePC()
|
361 | 365 | }
|
362 | 366 |
|
| 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 | + |
363 | 420 | /*
|
364 | 421 | INSTRUCTION : ADD IMMEDIATE
|
365 | 422 |
|
|
0 commit comments