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) {

     while (*themessages != NULL)

       // the C function puts outputs each message on its own line
       // We will mimick this in assembler with a function of our own

       puts(*themessages++);

}


main() {
    outmessages(messages);
}

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

# void outmessages(char ** themessages) {
    .globl outmessages
outmessages:
    addiu $sp,$sp,-20
    sw    $ra,16($sp)
    sw    $a0,20($sp)

Lomout:
    lw    $t0,20($sp)
    # if (*themessages == NULL) goto Lomdone;
    lw    $a0,0($t0)          # get the current string pointer
    beq   $a0,$zero,Lomdone   # if it's NULL, we're done
    # output it
    # puts(*themessages);
    jal   puts
    # themessages++;
    lw    $t0,20($sp)
    addi  $t0,$t0,4          # skip to the next string pointer
    sw    $t0,20($sp)
    # goto Lomout;
    b     Lomout
Lomdone:
    lw    $ra,16($sp)
    addiu $sp,$sp,20
    jr    $ra

#
#   void puts(char *msg) {
#       char *nlmsg = "\n";
#       PrintString(msg);
#       Printstring(nlmsg);
#   }
    .data
nlmsg: .asciiz "\n"
    .align 2
    .text
    .globl puts
puts:
    addiu $sp,$sp,-20
    sw    $ra,16($sp)
    jal   PrintString
    la    $a0,nlmsg
    jal   PrintString
    lw    $ra,16($sp)
    addiu $sp,$sp,20
    jr    $ra
    # main() {
    #   outmessages(messages);
    # }
    .globl main
main:

    # this is not a leaf, so we must at least save our return address
    # of course, we need the standard room for the arguments

    addiu   $sp,$sp, -20
    sw      $ra,16($sp)
    la      $a0,messages
    jal     outmessages
    lw      $ra,16($sp)
    addiu   $sp,$sp,20
    jr      $ra
.include "/pub/cs/gboyd/cs270/util.s"

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

Copyright 2016 Greg Boyd - All Rights Reserved.