Tom Kelliher, CS 240
Feb. 18, 2002
If you didn't see my e-mail:
Read 3.10. I will have you compile a C function to MIPS assembler in class. You will:
Finished up function call mechanism.
Function writing lab.
.asciiz is a NULL-terminated string.
.ascii is not terminated.
Example functions we'll look at:
int strlen(char *s)
char * strcat(char *dest, char *src)
strcat is dangerous --- buffer overflows.
strlen:
int strlen(char *s)
{
char *t = s;
while (*t != '\0')
++t;
return t - s;
}
# s in $a0
# t in $t0
# Current character of s in $t1
# strlen returned via $v0
move $t0, $a0
while: lbu $t1, 0($t0)
beqz $t1, endWhile
addi $t0, $t0, 1
b while
endWhile: sub $v0, $t0, $a0
strcat:
char * strcat(char *dest, char *src)
{
char *t = dest;
while (*t != '\0') /* Find end of dest. */
++t;
while (*src != '\0') /* Start appending src to dest. */
{
*t = *src;
++t;
++src;
}
*t = '\0'; /* Append NULL to dest. */
return dest;
}
# dest in $a0
# src in $a1
# t in $t0
# current character in $t1
# dest returned via $v0
move $t0, $a0
while1: lbu $t1, 0($t0)
beqz $t1, endWhile1
addi $t0, $t0, 1
b while
endWhile1:
while2: lbu $t1, 0($a1)
beqz $t1, endWhile2
sb $t1, 0($t0)
addi $t0, $t0, 1
addi $a1, $a1, 1
b while2
endWhile2: sb $0, 0($t0)
move $v0, $a0
Register, immediate, base and offset, PC-relative, ``direct.''
Base and offset is a form of indirect addressing.

Example: add $t0, $t0, $t1

Examples:
addi $t0, $t0, 1 lw $t1, 4($t0)
words?

Example: j reallyFarLabel
<code>
while: beq $t0, $t1, reallyFarLabel
<lots of code>
b while
becomes:
<code>
while: bne $t0, $t1, near
j reallyFarLabel
near: <lots of code>
j while
Solved with jr!
These are just comments. Make sure you read 3.9 on your own!
Dynamic data is maintained in the heap segment.
jr style
instructions.
Program assembled assuming it will be load starting at address 0. This is never the case.
Example: printf("The answer is: %d", ans);
Dynamic linking.