Discussion:
assembler for proj3
(too old to reply)
Stephen Tu
2008-08-03 06:00:53 UTC
Permalink
Raw Message
So since hand-assembling mips -> hex isn't exactly that fun, i have
wrote a simple assembler script. you can run it by invoking the command:

~cs61c-aq/public/bin/assembler [path_to_mips_file]

it can also read from STDIN, so you can pipe input into it. say you want
to convert a single command but dont want to save to file:

echo "add $r0,$r1,$r2" | ~cs61c-aq/public/bin/assembler

the program returns hex instructions to STDOUT, so if you want to save
it to a file just redirect the output:

~cs61c-aq/public/bin/assembler [path_to_mips_file] > myfile.hex

as for the syntax of the MIPS that it can read, it is mostly like
regular mips (it's not whitespace sensitive, and you can put comments in
your code w/ hashes (#)). several differences to note:

1) the op for jump is "jump" not "j"
2) for immediates, you can only put in positive decimal values as of
now. you can't do negative values and you can't do hex values yet. if
somebody wants to add this feature, please do so, b/c i'm too lazy to :)
3) for lw/sw, the offset part is _not_ whitespace sensitive. meaning
lw $r0, 0( $r1 ) is not valid and neither is lw $r0, 0 ($r1)


NOTE: i tried to test it on every case, but i'm sure i missed one or
two. please let me know when the program is giving incorrect hex
translations. don't expect it to be 100% accurate yet. if you find any
bugs let me know or better yet, post a diff of a fix and i'll apply it
as a patch :). it's just a simple perl script so you can all see the
source code

finally, i tried to make sure everybody has permissions to access it,
but in case it doesn't work, let me know. you might have to cp the
script to your own directory if it has issues opening your saved text
files due to lack of permissions. hopefully this script is helpful to
you guys until albert posts his assembler.
Stephen Tu
2008-08-03 06:02:38 UTC
Permalink
Raw Message
Post by Stephen Tu
So since hand-assembling mips -> hex isn't exactly that fun, i have
~cs61c-aq/public/bin/assembler [path_to_mips_file]
it can also read from STDIN, so you can pipe input into it. say you want
echo "add $r0,$r1,$r2" | ~cs61c-aq/public/bin/assembler
the program returns hex instructions to STDOUT, so if you want to save
~cs61c-aq/public/bin/assembler [path_to_mips_file] > myfile.hex
as for the syntax of the MIPS that it can read, it is mostly like
regular mips (it's not whitespace sensitive, and you can put comments in
1) the op for jump is "jump" not "j"
2) for immediates, you can only put in positive decimal values as of
now. you can't do negative values and you can't do hex values yet. if
somebody wants to add this feature, please do so, b/c i'm too lazy to :)
3) for lw/sw, the offset part is _not_ whitespace sensitive. meaning
lw $r0, 0( $r1 ) is not valid and neither is lw $r0, 0 ($r1)
NOTE: i tried to test it on every case, but i'm sure i missed one or
two. please let me know when the program is giving incorrect hex
translations. don't expect it to be 100% accurate yet. if you find any
bugs let me know or better yet, post a diff of a fix and i'll apply it
as a patch :). it's just a simple perl script so you can all see the
source code
finally, i tried to make sure everybody has permissions to access it,
but in case it doesn't work, let me know. you might have to cp the
script to your own directory if it has issues opening your saved text
files due to lack of permissions. hopefully this script is helpful to
you guys until albert posts his assembler.
GAH. sorry i just realized that the example i posted is wrong:
echo "add $r0,$r1,$r2" | ~cs61c-aq/public/bin/assembler
will not work b/c your shell will interpolate $r0,$r1,and $r2 and (most
likely) fail unless you have them set

echo 'add $r0,$r1,$r2' | ~cs61c-aq/public/bin/assembler

is the correct way to invoke that
Jeremy Cowles
2008-08-03 06:07:29 UTC
Permalink
Raw Message
Nice! I'm going to check it out now. I saved all my code as assembly so I
will run it through and let you know how it goes.

Jeremy
Post by Stephen Tu
~cs61c-aq/public/bin/assembler [path_to_mips_file]
it can also read from STDIN, so you can pipe input into it. say you want
echo "add $r0,$r1,$r2" | ~cs61c-aq/public/bin/assembler
the program returns hex instructions to STDOUT, so if you want to save it
~cs61c-aq/public/bin/assembler [path_to_mips_file] > myfile.hex
as for the syntax of the MIPS that it can read, it is mostly like regular
mips (it's not whitespace sensitive, and you can put comments in your code
1) the op for jump is "jump" not "j"
2) for immediates, you can only put in positive decimal values as of now.
you can't do negative values and you can't do hex values yet. if somebody
wants to add this feature, please do so, b/c i'm too lazy to :)
3) for lw/sw, the offset part is _not_ whitespace sensitive. meaning
lw $r0, 0( $r1 ) is not valid and neither is lw $r0, 0 ($r1)
NOTE: i tried to test it on every case, but i'm sure i missed one or two.
please let me know when the program is giving incorrect hex translations.
don't expect it to be 100% accurate yet. if you find any bugs let me know
or better yet, post a diff of a fix and i'll apply it as a patch :). it's
just a simple perl script so you can all see the source code
finally, i tried to make sure everybody has permissions to access it, but
in case it doesn't work, let me know. you might have to cp the script to
your own directory if it has issues opening your saved text files due to
lack of permissions. hopefully this script is helpful to you guys until
albert posts his assembler.
Jeremy Cowles
2008-08-03 06:12:34 UTC
Permalink
Raw Message
I got permission denied - I think the bin folder is on lock down.
Post by Jeremy Cowles
Nice! I'm going to check it out now. I saved all my code as assembly so I
will run it through and let you know how it goes.
Jeremy
Post by Stephen Tu
~cs61c-aq/public/bin/assembler [path_to_mips_file]
it can also read from STDIN, so you can pipe input into it. say you want
echo "add $r0,$r1,$r2" | ~cs61c-aq/public/bin/assembler
the program returns hex instructions to STDOUT, so if you want to save it
~cs61c-aq/public/bin/assembler [path_to_mips_file] > myfile.hex
as for the syntax of the MIPS that it can read, it is mostly like regular
mips (it's not whitespace sensitive, and you can put comments in your
1) the op for jump is "jump" not "j"
2) for immediates, you can only put in positive decimal values as of now.
you can't do negative values and you can't do hex values yet. if somebody
wants to add this feature, please do so, b/c i'm too lazy to :)
3) for lw/sw, the offset part is _not_ whitespace sensitive. meaning
lw $r0, 0( $r1 ) is not valid and neither is lw $r0, 0 ($r1)
NOTE: i tried to test it on every case, but i'm sure i missed one or two.
please let me know when the program is giving incorrect hex translations.
don't expect it to be 100% accurate yet. if you find any bugs let me know
or better yet, post a diff of a fix and i'll apply it as a patch :). it's
just a simple perl script so you can all see the source code
finally, i tried to make sure everybody has permissions to access it, but
in case it doesn't work, let me know. you might have to cp the script to
your own directory if it has issues opening your saved text files due to
lack of permissions. hopefully this script is helpful to you guys until
albert posts his assembler.
Stephen Tu
2008-08-03 06:24:26 UTC
Permalink
Raw Message
try again now,

sometimes i dont get permissions, it never works like it should
Jeremy Cowles
2008-08-03 06:34:36 UTC
Permalink
Raw Message
cat oct.s | ~cs61c-aq/public/bin/assembler
The following error was encountered: parse_itype(): invalid i-type
specification
for line lui $r2, $r2, 1 at
/home/cc/cs61c/su08/class/cs61c-aq/public/bin/assembler line 155, <> line 5.

lui $r2, $r2, 1
Stephen Tu
2008-08-03 06:37:05 UTC
Permalink
Raw Message
Post by Jeremy Cowles
cat oct.s | ~cs61c-aq/public/bin/assembler
The following error was encountered: parse_itype(): invalid i-type
specification
for line lui $r2, $r2, 1 at
/home/cc/cs61c/su08/class/cs61c-aq/public/bin/assembler line 155, <> line 5.
lui $r2, $r2, 1
that's b/c your syntax for lui is non-conventional. lui only takes one
register, for example: lui $r0, 33
Jeremy Cowles
2008-08-03 06:41:29 UTC
Permalink
Raw Message
doh. ok, I got a little farther:

cat oct.s | ~cs61c-aq/public/bin/assembler
The following error was encountered: parse_itype(): invalid i-type
specification
for line beq $r2, $r2, -12 at
/home/cc/cs61c/su08/class/cs61c-aq/public/bin/assembler line 155, <> line
20.

Any thoughts on this one?
Post by Stephen Tu
Post by Jeremy Cowles
cat oct.s | ~cs61c-aq/public/bin/assembler
The following error was encountered: parse_itype(): invalid i-type
specification
for line lui $r2, $r2, 1 at
/home/cc/cs61c/su08/class/cs61c-aq/public/bin/assembler line 155, <> line 5.
lui $r2, $r2, 1
that's b/c your syntax for lui is non-conventional. lui only takes one
register, for example: lui $r0, 33
Stephen Tu
2008-08-03 06:45:28 UTC
Permalink
Raw Message
cat oct.s | ~cs61c-aq/public/bin/assembler
The following error was encountered: parse_itype(): invalid i-type
specification
for line beq $r2, $r2, -12 at
/home/cc/cs61c/su08/class/cs61c-aq/public/bin/assembler line 155, <>
line 20.
Any thoughts on this one?
Post by Stephen Tu
Post by Jeremy Cowles
cat oct.s | ~cs61c-aq/public/bin/assembler
The following error was encountered: parse_itype(): invalid i-type
specification
for line lui $r2, $r2, 1 at
/home/cc/cs61c/su08/class/cs61c-aq/public/bin/assembler line 155, <> line 5.
lui $r2, $r2, 1
that's b/c your syntax for lui is non-conventional. lui only takes one
register, for example: lui $r0, 33
"2) for immediates, you can only put in positive decimal values as of
now. you can't do negative values and you can't do hex values yet. if
somebody wants to add this feature, please do so, b/c i'm too lazy to :) "

can you fix this please ??? :) it's kind of a pain actually since ints
in perl can only be 32-bit AFAIK, so to handle negative numbers you have
to actually calculate the 16-bit 2's complement by hand. i'll fix it
eventually haha
Ari Kahn
2008-08-03 08:43:08 UTC
Permalink
Raw Message
Post by Stephen Tu
cat oct.s | ~cs61c-aq/public/bin/assembler
The following error was encountered: parse_itype(): invalid i-type
specification
for line beq $r2, $r2, -12 at
/home/cc/cs61c/su08/class/cs61c-aq/public/bin/assembler line 155, <>
line 20.
Any thoughts on this one?
Post by Stephen Tu
Post by Jeremy Cowles
cat oct.s | ~cs61c-aq/public/bin/assembler
The following error was encountered: parse_itype(): invalid i-type
specification
for line lui $r2, $r2, 1 at
/home/cc/cs61c/su08/class/cs61c-aq/public/bin/assembler line 155, <> line 5.
lui $r2, $r2, 1
that's b/c your syntax for lui is non-conventional. lui only takes
one register, for example: lui $r0, 33
"2) for immediates, you can only put in positive decimal values as of
now. you can't do negative values and you can't do hex values yet. if
somebody wants to add this feature, please do so, b/c i'm too lazy to :) "
can you fix this please ??? :) it's kind of a pain actually since ints
in perl can only be 32-bit AFAIK, so to handle negative numbers you have
to actually calculate the 16-bit 2's complement by hand. i'll fix it
eventually haha
Well, I haven't messed with perl in ages, but poking around, the 16-bit
representation seems pretty straightforward, but I'm having one hell of
a time trying to use the regexps to take in a negative number for some
reason.
Stephen Tu
2008-08-03 08:53:04 UTC
Permalink
Raw Message
Post by Ari Kahn
Well, I haven't messed with perl in ages, but poking around, the 16-bit
representation seems pretty straightforward, but I'm having one hell of
a time trying to use the regexps to take in a negative number for some
reason.
i was actually wrong, it should be 8-bit 2's complement since immediates
in our project are 8-bits. actually i realized it _is_ really simple,
since all you do is negate the positive number and then mask off the
last 8 bits.

as for the regex, shouldn't this work?:

if ( $line =~ m/(-?\d+)/ ) {
$num = $1;
}

$num will then contain the number. the question is whether perl will
interpret this as a string or as a negative integer. the better question
is what will happen when you try to do arithmetic (or even bitwise)
operations on this negative number.

having said that, i think the beauty of perl is that it will behave
nicely in this situation. in theory at least ;)
Jeremy Cowles
2008-08-03 18:51:14 UTC
Permalink
Raw Message
Oh, sorry, I was getting tired. I haven't touched perl in a long time, but
it seems like you don't need a 16 bit number, like you just need to take the
negative number and represent it in 8 bits unsigned. I tried this in Python,
and it seemed to do the job:

def unsign(value):
return 0xff - (-value - 1)

So the function expects a negative number. It inverts the value so it is
positive, subtracts 1, and then subtracts the rest of the number from 0xFF,
which is the unsigned value of -1 in 8 bits. The resulting number is the
unsigned value of the original negative number, represented in 8 bits.

I don't know if that is applicable to your problem, I'm not sure I entirely
understand what is going wrong, but this did produce the correct immediate
hex value for the lui example.

Jeremy
cat oct.s | ~cs61c-aq/public/bin/assembler
The following error was encountered: parse_itype(): invalid i-type
specification
for line beq $r2, $r2, -12 at
/home/cc/cs61c/su08/class/cs61c-aq/public/bin/assembler line 155, <> line
20.
Any thoughts on this one?
Post by Stephen Tu
Post by Jeremy Cowles
cat oct.s | ~cs61c-aq/public/bin/assembler
The following error was encountered: parse_itype(): invalid i-type
specification
for line lui $r2, $r2, 1 at
/home/cc/cs61c/su08/class/cs61c-aq/public/bin/assembler line 155, <> line 5.
lui $r2, $r2, 1
that's b/c your syntax for lui is non-conventional. lui only takes one
register, for example: lui $r0, 33
"2) for immediates, you can only put in positive decimal values as of now.
you can't do negative values and you can't do hex values yet. if somebody
wants to add this feature, please do so, b/c i'm too lazy to :) "
can you fix this please ??? :) it's kind of a pain actually since ints in
perl can only be 32-bit AFAIK, so to handle negative numbers you have to
actually calculate the 16-bit 2's complement by hand. i'll fix it
eventually haha
Stephen Tu
2008-08-03 19:52:11 UTC
Permalink
Raw Message
Post by Jeremy Cowles
Oh, sorry, I was getting tired. I haven't touched perl in a long time,
but it seems like you don't need a 16 bit number, like you just need to
take the negative number and represent it in 8 bits unsigned. I tried
return 0xff - (-value - 1)
So the function expects a negative number. It inverts the value so it is
positive, subtracts 1, and then subtracts the rest of the number from
0xFF, which is the unsigned value of -1 in 8 bits. The resulting number
is the unsigned value of the original negative number, represented in 8
bits.
I don't know if that is applicable to your problem, I'm not sure I
entirely understand what is going wrong, but this did produce the
correct immediate hex value for the lui example.
Jeremy
thanks for your help, but it turned out to be even easier than i
thought- perl is nice enough to treat (-?\d+) [regexp] as an integer, so
all i had to do was to mask that int with 0xFF and it was good :)

i have updated my assembler to be almost on par w/ willem's. it now
understands negative and hex numbers, and it will error check your
immediates to make sure they do not exceed 8 bits.

it'd be really cool if somebody could implement label functionality!
Stephen Tu
2008-08-03 07:20:51 UTC
Permalink
Raw Message
This is just a guessing thought.. I googled the error, and tried
maning parse_itype on nova.. but is it talking about inode type?
In case you aren't aware, inodes are the data structure used by
unix for file entries in the filesystem. I think one of the entries
of an inode would be the string of the filename (not sure check me on
this). Unix links are like a second pointer to the same structure, and
directories are just a special type of file that happens to contain a list
of files.
I hope I added something that sheds some light onto what is going
on with your bug.
cat oct.s | ~cs61c-aq/public/bin/assembler
The following error was encountered: parse_itype(): invalid i-type
specification
for line beq $r2, $r2, -12 at
/home/cc/cs61c/su08/class/cs61c-aq/public/bin/assembler line 155, <> line
20.
Any thoughts on this one?
Post by Stephen Tu
Post by Jeremy Cowles
cat oct.s | ~cs61c-aq/public/bin/assembler
The following error was encountered: parse_itype(): invalid i-type
specification
for line lui $r2, $r2, 1 at
/home/cc/cs61c/su08/class/cs61c-aq/public/bin/assembler line 155, <> line 5.
lui $r2, $r2, 1
that's b/c your syntax for lui is non-conventional. lui only takes one
register, for example: lui $r0, 33
parse_itype() is a subroutine i wrote in the assembler script that is
supposed to parse i-type MIPS instructions. it is supposed to throw an
error when the initial line parser recognizes that the line is an i-type
instruction, but the actual line doesn't match a set of regular
expressions that certain i-type instructions must have. the reason the
above case failed was because
beq $r2, $r2, -12
has a (-12) in the expression, which doesn't match the regular
expression since i haven't had it handle negative immediates yet. i'm
not at all aware of what inodes are, but the error is a custom error
Loading...