hlorenzi / customasm

💻 An assembler for custom, user-defined instruction sets! https://hlorenzi.github.io/customasm/web/
Apache License 2.0
704 stars 55 forks source link

Is it possible to parse statements like .data or .asciz? #201

Open gustavogutierrezutp opened 6 months ago

gustavogutierrezutp commented 6 months ago

Hi,

I have all the instructions of my architecture already parsed and the produced binary looks fine. How ever I was wonderin if it is possible to also parse the section code like

.global symbol
.asciz "hello"

and to produce the corresponding binary output.

MineRobber9000 commented 6 months ago
#d "hello", 0

Not sure what .global symbol does but .asciz can be done with this. You could even use a function:

#fn asciz(s) => ascii(s) @ 0`8
#d asciz("hello")
ggutierrez commented 6 months ago

Here is an example of what I'm trying to achieve. When .data is parsed then I know that whatever is declared after it goes in the memory as static values. That is why I want to make a rule for it.

#subruledef registerName {
  x0 => 0`5
  x1 => 1`5
  x2 => 2`5
  x3 => 3`5
  x4 => 4`5
}

#ruledef {
  add {rdest: registerName},{rs1: registerName},{rs2: registerName} =>  0x00`7 @ rs2`5 @ rs1`5 @ 0`3 @ rdest`5 @ 0b0110011
}

#ruledef {
  .data => 0`4
}

.data
argument: .word   7
str1:     .string "something1"
str2:     .string " something2 "

.text
main:
add x3, x3, x3
MineRobber9000 commented 6 months ago

I'm not sure what "static values" means in this context-- all values outputted to the ROM are static, it's just a matter of how those values are encoded. Your .data section here can be written as:

argument:
#d32 7 ; or d16 for that size
str1:
#d "something1" ; assuming string vs asciz means it's not null terminated, add ", 0" if I'm wrong
str2:
#d " something2 " ; again, add ", 0" for null termination

Of course, if you wanted to codify these as instructions you could easily add them (though you'd need to get rid of the leading dot, since customasm assumes something with a leading dot is a local label):

#ruledef {
    word {n: u32} => n
    string {s} => s
}