sections in this module | City
College of San Francisco - CS270 Computer Architecture Module: MIPS-V (Pointers) |
module list |
Problems
messages.c
In the text, we discussed solving the following problem
char
*messages[3] = { "message1", "message2", "message3" };
void outmessages(char **
themessages,int n) {
int i;
for
(i=0;i<n; i++)
puts(*themessages++);
}
main() {
outmessages(messages, 3);
}
We will rewrite this program to use a NULL pointer to indicate the end of the messages array, i.e., where it is initialized like this
char *messages[] = { "message1", "message2", "message3", 0 };
The C code for the modified messages.c is below
char
*messages[] = { "message1", "message2", "message3", 0 };
void outmessages(char **
themessages) {
// the C function puts outputs each message on its own line
puts(*themessages++);
}
And here is the MIPS code:
# char *messages[] = {
"message1", "message2", "message3", 0 };
.data
Lmsg1:
.asciiz "message1"
Lmsg2:
.asciiz "message2"
Lmsg3:
.asciiz "message3"
.align 2
messages:
.word Lmsg1, Lmsg2, Lmsg3, 0
.text
# this is not a leaf, so we must at least save our return address
addiu $sp,$sp, -20
This program is messages.c, messages0.c and messages.s in the
online/mipsV directory.
myxyz.s
In the example for class xyz in the section on Object-oriented programming, suppose the declaration of myxyz was changed from
xyz myxyz(2,4)
to
xyz *myxyz = new xyz(2,4);
what changes to the assembly code are required? (The answer is in myxyzptr.cpp and myxyzptr.s)
Prev | This page was made entirely
with free software on linux: Kompozer and LibreOffice |
Next |