block DString { // char *duplicate( char *s ) { // int len = String.length( s ); // char *result = Memory.allocate( len + 1 ); // String.copy( result, s ); // return result; // } public block duplicate uses proc { abs { s = s0; len = s1; result = s2; } abs code { public enter: lda $sp, -sav3($sp); stq $ra, savRet($sp); stq $s0, sav0($sp); stq $s1, sav1($sp); stq $s2, sav2($sp); init: mov $a0, $s; body: mov $s, $a0; bsr String.length.enter; mov $v0, $len; addq $v0, 1; mov $v0, $a0; bsr Memory.allocate.enter; mov $v0, $result; mov $v0, $a0; mov $s, $a1; bsr String.copy.enter; mov $result, $v0; return: ldq $s2, sav2($sp); ldq $s1, sav1($sp); ldq $s0, sav0($sp); ldq $ra, savRet($sp); lda $sp, +sav3($sp); ret; } code } block duplicate // char *substring( char *s, int start, int finish ) { // char *result = Memory.allocate( finish - start + 1 ); // int i; // for ( i = start; i < finish; i++ ) // result[ i - start ] = s[ i ]; // return result; // } public block substring uses proc { abs { s = s0; start = s1; finish = s2; result = s3; i = s4; } abs code { public enter: lda $sp, -sav5($sp); stq $ra, savRet($sp); stq $s0, sav0($sp); stq $s1, sav1($sp); stq $s2, sav2($sp); stq $s3, sav3($sp); stq $s4, sav4($sp); init: mov $a0, $s; mov $a1, $start; mov $a2, $finish; body: subq $finish, $start, $a0; addq $a0, 1; bsr Memory.allocate.enter; mov $v0, $result; { for: mov $start, $i; while: cmplt $i, $finish, $t0; blbc $t0, end; do: addq $s, $i, $t0; ldbu $t0, ($t0); addq $result, $i, $t1; subq $t1, $start; stb $t0, ($t1); continue: addq $i, 1; end: } mov $result, $v0; return: ldq $s4, sav4($sp); ldq $s3, sav3($sp); ldq $s2, sav2($sp); ldq $s1, sav1($sp); ldq $s0, sav0($sp); ldq $ra, savRet($sp); lda $sp, +sav5($sp); ret; } code } block substring // char *concat( char *s, char *t ) { // int sLen = String.length( s ); // int tLen = String.length( t ); // char *result = Memory.allocate( sLen + tLen + 1 ); // String.copy( result, s ); // String.copy( result + sLen, t ); // return result; // } public block concat uses proc { abs { s = s0; t = s1; sLen = s2; tLen = s3; result = s4; } abs code { public enter: lda $sp, -sav5($sp); stq $ra, savRet($sp); stq $s0, sav0($sp); stq $s1, sav1($sp); stq $s2, sav2($sp); stq $s3, sav3($sp); stq $s4, sav4($sp); init: mov $a0, $s; mov $a1, $t; body: mov $s, $a0; bsr String.length.enter; mov $v0, $sLen; mov $t, $a0; bsr String.length.enter; mov $v0, $tLen; addq $sLen, $tLen, $a0; addq $a0, 1; bsr Memory.allocate.enter; mov $v0, $result; mov $result, $a0; mov $s, $a1; bsr String.copy.enter; addq $result, $sLen, $a0; mov $t, $a1; bsr String.copy.enter; mov $result, $v0; return: ldq $s4, sav4($sp); ldq $s3, sav3($sp); ldq $s2, sav2($sp); ldq $s1, sav1($sp); ldq $s0, sav0($sp); ldq $ra, savRet($sp); lda $sp, +sav5($sp); ret; } code } block concat } block DString