Assembler written in bash?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Assembler written in bash?

aetesaki
When I first did this project I hand assembled the project files. Easy enough.
But now I looked at it again, and started writing, in bash.

I have a parser script that removes all formatting and comment, using a lot of sed. This script also replace the predefined symbols with their numeric values
And I have a linker that reads each line of the parsed file, extracting all labels and getting all variables, before linking each to their numeric values (first pass)

The output of these two scripts are almost identical to the ProgL.asm files provided in the project. The difference being that the linker links variables to different locations compared to the provided assembler.

Next will be to make the assembler to produce hack files. (second pass)

Aetesaki
Reply | Threaded
Open this post in threaded view
|

Re: Assembler written in bash?

WBahn
Administrator
You really need to get your assembler to assign variables to sequential locations starting at Address 16. Or, at the very least, ensure that the first 240 variables are assigned unique locations in the range 16 to 255 (inclusive). This is important because the VM Translator relies on this behavior in order to create it's Static memory segments for each class such that they don't conflict with each other, and that they don't conflict with the pre-assigned segments, the stack, or the heap.
Reply | Threaded
Open this post in threaded view
|

Re: Assembler written in bash?

aetesaki
It does, as per contract. The difference in assignment lies in how the variables are handled.

Here's the code:
# strip @ from variable file
sed -i "s/^@//" $1.v
# remove duplicated variables
sort $1.v | uniq > $1.u
rm -f $1.v
# set lowest variable space
declare var=16
# for each variable
while IFS="" read -r p || [ -n "$p" ]
do
  # link variables to addresses
  sed -i "s/$p/$var/" $1.l
  # increase var
  var=$((var+1))
done < $1.u
# remove variable file
rm -f $1.u

During the pass through the parser file, each instance of a variable is dumped to a variable file, which is sorted and truncated to remove all duplicated variables. Then each instance of each variable are replaced with it's assigned address, starting sequentially from M[16].
Reply | Threaded
Open this post in threaded view
|

Re: Assembler written in bash?

WBahn
Administrator
That should work -- though the test scripts which assume that the variables are assigned sequentially as they are first encountered, so unless they happen to appear in the code in alphabetical order, the resulting machine code translations won't match. But, as you say, it does meet the letter of the contract.