I making a assembler I have finish the label part but I’m stuck in the replace the label with the address
This is what I get:
<code>module assembler
pub fn assembler(source []string) []string {
mut label := map[string]int{}
mut counter := 0
mut code := []string{}
for i in source {
if ':' in i.split('') {
label[i.split(':')[0]] = counter
} else {
code << i
}
counter++
}
for i in code {
for part in i.split(' ') {
if part in label {
println(label[part]) // Here is the address come out
}
}
}
return ['']
}
</code>
<code>module assembler
pub fn assembler(source []string) []string {
mut label := map[string]int{}
mut counter := 0
mut code := []string{}
for i in source {
if ':' in i.split('') {
label[i.split(':')[0]] = counter
} else {
code << i
}
counter++
}
for i in code {
for part in i.split(' ') {
if part in label {
println(label[part]) // Here is the address come out
}
}
}
return ['']
}
</code>
module assembler
pub fn assembler(source []string) []string {
mut label := map[string]int{}
mut counter := 0
mut code := []string{}
for i in source {
if ':' in i.split('') {
label[i.split(':')[0]] = counter
} else {
code << i
}
counter++
}
for i in code {
for part in i.split(' ') {
if part in label {
println(label[part]) // Here is the address come out
}
}
}
return ['']
}
main:
<code>module main
import assembler
fn main() {
assembler.assembler(['.main:', 'JMP .hlt', '.hlt:', 'HLT']) // Simple example
}
</code>
<code>module main
import assembler
fn main() {
assembler.assembler(['.main:', 'JMP .hlt', '.hlt:', 'HLT']) // Simple example
}
</code>
module main
import assembler
fn main() {
assembler.assembler(['.main:', 'JMP .hlt', '.hlt:', 'HLT']) // Simple example
}
And I’m think about this:
<code>arr := []string{}
// In a loop
if part in label {
arr << label[part]
}
arr << part
</code>
<code>arr := []string{}
// In a loop
if part in label {
arr << label[part]
}
arr << part
</code>
arr := []string{}
// In a loop
if part in label {
arr << label[part]
}
arr << part
I expecting that the output should be [“JMP 1”, “HLT”]
I’m also thinking to rewrite it.