Hone logo
Hone
Problems

Go Assembly Generator

This challenge focuses on building a foundational tool for understanding and manipulating low-level code. You will implement a Go program that generates simple assembly instructions for a hypothetical RISC-like architecture. This skill is crucial for compiler development, performance optimization, and embedded systems programming.

Problem Description

Your task is to create a Go program that can generate sequences of assembly instructions. The program should take a structured representation of desired operations and translate them into a specific assembly language format.

Key Requirements:

  • Instruction Set: Support a small, predefined set of assembly instructions. For this challenge, we'll define the following:
    • MOV reg, value: Move an immediate value into register reg.
    • ADD reg1, reg2: Add the value in reg2 to the value in reg1, storing the result in reg1.
    • SUB reg1, reg2: Subtract the value in reg2 from the value in reg1, storing the result in reg1.
    • LOAD reg, address: Load a value from address into reg.
    • STORE reg, address: Store the value from reg into address.
    • JMP address: Unconditional jump to address.
    • LABEL name: Define a label named name at the current instruction address.
  • Input Representation: The input will be a slice of Go structs, where each struct represents a single assembly instruction with its operands.
  • Output Format: The generated assembly code should be a string, with each instruction on a new line. Labels should be followed by a colon.
  • Register and Address Naming: Registers will be named R0, R1, R2, etc. Addresses will be represented as unsigned integers.

Expected Behavior:

The program should iterate through the input instruction representations and convert each one into its corresponding string representation in the defined assembly language.

Edge Cases:

  • Empty Input: The program should handle an empty slice of instructions gracefully, returning an empty string.
  • Invalid Operands: While not strictly required for this initial challenge, consider how invalid operand types or values could be handled in a more robust implementation (e.g., returning an error). For this challenge, assume valid inputs.

Examples

Example 1:

Input:
[]Instruction{
    {Op: "MOV", Operands: []string{"R1", "10"}},
    {Op: "ADD", Operands: []string{"R1", "R2"}},
    {Op: "STORE", Operands: []string{"R1", "0x100"}},
}

Output:
MOV R1, 10
ADD R1, R2
STORE R1, 256

Explanation: Each instruction struct is converted to its string representation. The hexadecimal address 0x100 is converted to its decimal equivalent.

Example 2:

Input:
[]Instruction{
    {Op: "LABEL", Operands: []string{"start"}},
    {Op: "MOV", Operands: []string{"R0", "5"}},
    {Op: "JMP", Operands: []string{"loop_end"}},
    {Op: "LABEL", Operands: []string{"loop_end"}},
    {Op: "ADD", Operands: []string{"R0", "R0"}},
}

Output:
start:
MOV R0, 5
JMP loop_end
loop_end:
ADD R0, R0

Explanation: The LABEL instructions are formatted with a trailing colon. The JMP instruction correctly references a label.

Example 3:

Input:
[]Instruction{}

Output:
""

Explanation: An empty input slice results in an empty output string.

Constraints

  • The input Instruction struct will have Op (string) and Operands (slice of strings).
  • The number of instructions in the input slice will be between 0 and 1000.
  • Register names will always be in the format R<digit>, e.g., R0, R15.
  • Values can be integers (decimal) or hexadecimal strings (prefixed with 0x).
  • Addresses can be integers (decimal) or hexadecimal strings (prefixed with 0x).
  • Label names will be valid Go identifiers.

Notes

  • You'll need to define the Instruction struct yourself.
  • Consider how to parse and format numeric values (decimal and hexadecimal). The strconv package in Go will be very useful here.
  • Focus on correctly mapping the operation name and operands to the desired output string format.
  • The goal is to produce syntactically correct assembly for the defined instruction set. Error handling for malformed inputs is not the primary focus of this challenge, but it's good to think about for future improvements.
Loading editor...
go