Created
July 15, 2011 01:45
-
-
Save cotto/1083875 to your computer and use it in GitHub Desktop.
example of using M0's get/set_byte ops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.version 0 | |
.chunk "byte_ops_test" | |
.constants | |
0 "string thing?" | |
1 "\n" | |
.metadata | |
.bytecode | |
# print byte value of first byte of string 0 | |
set_imm S0, 0, 0 | |
deref S0, CONSTS, S0 | |
# print first (non-header) byte of S0 | |
set_imm I0, 0, 1 | |
set_imm I1, 0, 8 | |
get_byte I1, S0, I1 | |
print_i I0, I1, x | |
# print newline | |
set_imm S1, 0, 1 | |
deref S1, CONSTS, S1 | |
print_s I0, S1, x | |
# print the length of the string and its encoding | |
# print character count | |
set_imm I1, 0, 0 | |
get_word I1, S0, I1 | |
print_i I0, I1, x | |
print_s I0, S1, x | |
# print encoding | |
set_imm I1, 0, 1 | |
get_word I1, S0, I1 | |
print_i I0, I1, x | |
print_s I0, S1, x | |
# calculate the sum of the byte value of string 0 | |
# I1 = number of bytes in string | |
# I2 = index (skipping header) | |
# I3 = value of current byte | |
# I4 = sum of bytes | |
# I5 = 8 | |
set_imm I1, 0, 0 | |
get_word I1, S0, I1 | |
set_imm I2, 0, 8 | |
set_imm I3, 0, 0 | |
set_imm I4, 0, 0 | |
set_imm I5, 0, 8 | |
loop_start: | |
# get value of current byte | |
set I3, I2, x | |
get_byte I3, S0, I3 | |
# add to total | |
add_i I4, I4, I3 | |
# increment index | |
add_i I2, I2, I0 | |
# goto loop_start unless index < I1+8 | |
add_i I6, I1, I5 | |
sub_i I6, I6, I2 | |
goto_if loop_start, I6 | |
# print the sum and a newline | |
print_i I0, I4, x | |
print_s I0, S1, x | |
# create a copy of string 0, change "thing" to "thingy", free string | |
# This code isn't really PASM, but the highlighting works well. | |
# vim: expandtab shiftwidth=4 ft=pasm: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment