Skip to content

Commit

Permalink
Adicionado pacote constants, alterada segunda passage, corrigido bug …
Browse files Browse the repository at this point in the history
…na primeira passagem, subtituidos textos fixos pelas constantes
  • Loading branch information
luizgallas authored and adriancerbaro-tlf committed Jun 9, 2020
1 parent caad956 commit a4107ea
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 32 deletions.
18 changes: 18 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package constants

const (
//TextSection texto para seção text
TextSection = "text"

//DataSection texto para seção data
DataSection = "data"

//LabelNotFound erro de label não encontrada
LabelNotFound = "Label not found"

//NoneInstructionFound erro de nome de instrução não encontrado
NoneInstructionFound = "None instruction found for this name: %s"

//InvalidOperandCount erro de quantidade de operadores invalidas
InvalidOperandCount = "Invalid operand count"
)
35 changes: 18 additions & 17 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package decoder

import (
"fmt"
"ph1-assembly/constants"
)

type metaInstruction struct {
Expand All @@ -11,22 +12,22 @@ type metaInstruction struct {

// Mapeia todos os mnemônicos disponíveis para seus opCodes e número de endereços
var operations = map[string]*metaInstruction{
"NOP": &metaInstruction{opCode: "00", size: 0},
"LDR": &metaInstruction{opCode: "01", size: 1},
"STR": &metaInstruction{opCode: "02", size: 1},
"ADD": &metaInstruction{opCode: "03", size: 1},
"SUB": &metaInstruction{opCode: "04", size: 1},
"MUL": &metaInstruction{opCode: "05", size: 1},
"DIV": &metaInstruction{opCode: "06", size: 1},
"NOT": &metaInstruction{opCode: "07", size: 1},
"AND": &metaInstruction{opCode: "08", size: 0},
"OR": &metaInstruction{opCode: "09", size: 1},
"XOR": &metaInstruction{opCode: "A0", size: 1},
"JMP": &metaInstruction{opCode: "B0", size: 1},
"JEQ": &metaInstruction{opCode: "C0", size: 1},
"JG": &metaInstruction{opCode: "D0", size: 1},
"JL": &metaInstruction{opCode: "E0", size: 1},
"HLT": &metaInstruction{opCode: "F0", size: 0},
"NOP": &metaInstruction{opCode: "00", size: 1},
"LDR": &metaInstruction{opCode: "10", size: 2},
"STR": &metaInstruction{opCode: "20", size: 2},
"ADD": &metaInstruction{opCode: "30", size: 2},
"SUB": &metaInstruction{opCode: "40", size: 2},
"MUL": &metaInstruction{opCode: "50", size: 2},
"DIV": &metaInstruction{opCode: "60", size: 2},
"NOT": &metaInstruction{opCode: "70", size: 2},
"AND": &metaInstruction{opCode: "80", size: 1},
"OR": &metaInstruction{opCode: "90", size: 2},
"XOR": &metaInstruction{opCode: "A0", size: 2},
"JMP": &metaInstruction{opCode: "B0", size: 2},
"JEQ": &metaInstruction{opCode: "C0", size: 2},
"JG": &metaInstruction{opCode: "D0", size: 2},
"JL": &metaInstruction{opCode: "E0", size: 2},
"HLT": &metaInstruction{opCode: "F0", size: 1},
}

// Decode traduz o mnemônico de uma instrução e retorna
Expand All @@ -35,7 +36,7 @@ func Decode(name string) (string, int, error) {
instruction := operations[name]

if instruction == nil {
return "", 0, fmt.Errorf("None instruction found for this name: %s", name)
return "", 0, fmt.Errorf(constants.NoneInstructionFound, name)
}

return instruction.opCode, instruction.size, nil
Expand Down
37 changes: 25 additions & 12 deletions extractor/instructions.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package extractor

import (
"ph1-assembly/constants"
"ph1-assembly/decoder"
"ph1-assembly/input"
"strconv"
)

//Instruction representa uma instrução
Expand All @@ -21,10 +23,15 @@ type Data struct {

//ExtractInstructions efetua a segunda passagem no código, guardando as
// instrucoes em uma lista de struct
func ExtractInstructions(contents []*input.SourceLine, labelMap map[string]int) (instructions []Instruction) {
// Contador de endereço
var address int = 0
func ExtractInstructions(contents []*input.SourceLine, labelMap map[string]int) []Instruction {
var instructions = make([]Instruction, 0)

for _, srcLine := range contents {

if srcLine.Name == constants.TextSection || srcLine.Name == constants.DataSection {
break
}

opCode, size, err := decoder.Decode(srcLine.Name)

if err != nil {
Expand All @@ -33,27 +40,33 @@ func ExtractInstructions(contents []*input.SourceLine, labelMap map[string]int)

// Cria instrução sem operando
instruction := &Instruction{
Address: address,
Address: srcLine.Address,
OpCode: opCode,
}

// Verifica se o valor retornado da decodificação para aquela instrução é 0 ou 1
if size == 1 {
// Verifica se o valor retornado da decodificação para aquela instrução é 1 ou 2
if size == 2 {
instruction.HasOperand = true
// Busca nos labels o valor do operando
operandValue := labelMap[srcLine.Operand]
operandValue, found := labelMap[srcLine.Operand]

if found == false {
operandValue, err = strconv.Atoi(srcLine.Operand)

if err != nil {
panic(constants.LabelNotFound)
}
}

instruction.Data.Value = operandValue

// O endereço do operando é sempre um após sua instrução, para manter a contuinidade da contagem
// é preciso incrementar um
instruction.Data.Address = address + 1
address++
instruction.Data.Address = srcLine.Address + 1
}

// Adiciona a instrução na lista e executa o laço novamente
instructions = append(instructions, *instruction)

}
return

return instructions
}
6 changes: 4 additions & 2 deletions extractor/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (

// ExtractLabels efetua a primeira passagem no código, guardando os rótulos
// e seus endereços em um map que irá retornar
func ExtractLabels(contents []*input.SourceLine) (labelMap map[string]int) {
func ExtractLabels(contents []*input.SourceLine) map[string]int {

var labelMap = make(map[string]int)

sections := map[string]int{
"text": 0,
Expand Down Expand Up @@ -36,5 +38,5 @@ func ExtractLabels(contents []*input.SourceLine) (labelMap map[string]int) {
}
}

return
return labelMap
}
3 changes: 2 additions & 1 deletion input/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package input
import (
"bufio"
"os"
"ph1-assembly/constants"
"regexp"
"strings"
)
Expand Down Expand Up @@ -62,7 +63,7 @@ func parseSourceLine(line string) (srcLine *SourceLine) {
match := lineMatchRegex.FindStringSubmatch(line)

if len(match) == 0 {
panic("Invalid operand count")
panic(constants.InvalidOperandCount)
}

// Instantcia um novo SourceLine
Expand Down

0 comments on commit a4107ea

Please sign in to comment.