Skip to content

Commit bd8b50a

Browse files
committed
fix decodeInstruction
1 parent de6cb29 commit bd8b50a

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

asmparser/mips/mips_parser.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,22 @@ func parseInstruction(line string) (*instruction, error) {
114114
if len(matches) <= 4 {
115115
return nil, fmt.Errorf("failed to parse instruction: %s", line)
116116
}
117-
instr, err := decodeInstruction(strings.ReplaceAll(matches[3], " ", ""))
118-
if err != nil {
119-
return nil, fmt.Errorf("invalid MIPS instruction format: %w", err)
120-
}
121117
pcAddress, err := strconv.ParseUint(matches[1], 16, 64)
122118
if err != nil {
123119
return nil, fmt.Errorf("invalid instruction address: %w", err)
124120
}
121+
instr, err := decodeInstruction(strings.ReplaceAll(matches[3], " ", ""), pcAddress)
122+
if err != nil {
123+
return nil, fmt.Errorf("invalid MIPS instruction format: %w", err)
124+
}
125125
instr.opcodeString = matches[4]
126126
instr.address = pcAddress
127127
return instr, nil
128128
}
129129

130130
// decodeInstruction decodes a hexadecimal MIPS instruction.
131131
// https://en.wikibooks.org/wiki/MIPS_Assembly/Instruction_Formats#FI_Instructions
132-
func decodeInstruction(str string) (*instruction, error) {
132+
func decodeInstruction(str string, pcAddress uint64) (*instruction, error) {
133133
_instruction, err := strconv.ParseUint(str, 16, 32)
134134
if err != nil {
135135
return nil, fmt.Errorf("failed to parse hex instruction: %w", err)
@@ -163,7 +163,7 @@ func decodeInstruction(str string) (*instruction, error) {
163163
int64(funcCode),
164164
)
165165
case 0x02, 0x03: // J-Type Instructions (Jump)
166-
targetAddress := (instr & 0x03FFFFFF) << 2
166+
targetAddress := (instr&0x03FFFFFF)<<2 + (uint32(pcAddress) & 0xF0000000)
167167
decodedInstruction.instType = asmparser.JType
168168
//nolint
169169
decodedInstruction.operands = append(decodedInstruction.operands, int64(targetAddress))

0 commit comments

Comments
 (0)