Lab #2

The goal of this lab is to work with FUNCTIONS and character arrays in assembly language.
You are expected to follow the function usage guidelines discussed in class.

Things you'll need to understand and be able to use to do this lab:

  • Basic syntax of assembler
  • syscalls for reading and printing integers and strings
  • Some basic instructions and pseudo ops: li,la,move,add,sub,lb,sb,syscall,j,ble,bgt,etc.
  • Understanding of the basic assembler directives: .data, .text, .asciiz, etc.
  • An understanding of the basic concepts involved in creating functions in assembly: argument registers ($a0-$a3), return value registers ($v0-$v1), and the stack/stack pointer.
  • Correct Usage of Registers and the Stack (As described in the handout from the MIPS Programmer's Handbook)
  • Lab Report Format & Requirements (What you need to do and what you need to turn in):

    1. Program written in any high level language you choose (C, C++, Pascal, Fortran, Cobol, Lisp, Java, etc.) Be sure to comment and fully test this version. 
    2. A revised version of the program with comments and notes indicating how each part will be converted into assembly language.
      Relevant info would include: where labels will be created (and label names) and which registers will be used and for what purposes. (Do this prior to writting the MIPS version of the program)
    3. Program written in MIPS Assembly - This should be well commented
    4. "Test Cases" - What test cases did you use to verify that you're program worked?
      This time your should compare your functions (m_atoi, ...) to the real C library version (atoi) to ensure that they work identically.
    5. Sample Runs: Show me the test cases for BOTH your high level language program and the Assembly version.
      Use your test cases and comment the output to demonstrate that it worked as expected.
      Your test cases should show that the program did work correctly.
    6. Match the structure of the generated output as closely as possible
    7. Answer the following questions:
      1. What test cases did you use and why? (What was each case meant to test?)
        (My advice is to write test cases BEFORE programming)
      2. What mistakes did you make in writing the program?
      3. How might you revise/improve the program?
      4. How long did this assignment take you?
      5. How long did you spend debugging?
      6. Were there any special debugging techniques that you used?
      7. Were there any special development techniques that you used?

    The Program/Functions:

    Write the following functions for working with strings.
    They must be proper functions in assembly language that follow the register and stack usage conventions.

    m_isspace:
    Function: Determines if a character is considered "whitespace" (I.e. should be ignored)
    argument 0: a character
    return value: non-zero if the character is white-space (space (' '),  
                             form-feed  ('\f'),  newline  ('\n'),  carriage  return ('\r'),
                             horizontal tab ('\t'), and vertical tab ('\v').

    From the C man page:
    int isspace(int C);
    isspace checks  for white-space characters.  In the "C" and "POSIX"  locales, these  are:
    space (' '), form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v')
    RETURN VALUE
    The values returned are nonzero if the character C falls into the tested class, and a zero value if not.

    m_toupper:
    Function: Makes all letters uppercase and leaves non-letters alone.
    argument 0: a character
    return value: if the character is a lower case letter, the uppercase letter is returned. Otherwise, the original character is returned.
    Hint: Remember the ASCII properties.

    From the C man page:
    int toupper(int c);
    toupper() converts the letter c to upper case, if possible. 
    If c is not an unsigned char value, or EOF, the behaviour of these functions is undefined.
    RETURN VALUE
    The value returned is that of the converted letter, or c if the conversion was not possible.
    m_atoi:
    Function: Converts ASCII characters into an integer.
    NOTES: This function uses a "powers of 10" trick similiar to the powers of 2 trick.
                      This function ignores initial white space and MUST call m_isspace.
                      The number may have a '+' or a '-' as the 1st character (after the whitespace).
    argument 0: a pointer to the character array to convert into an integer.
    return value: an integer representing the value of the entered number.
    Hint: Don't forget the ASCII properties of '0'-'9'.

    From the C man page:
    int atoi(char *nptr);
    The atoi() function converts the initial portion of the string pointed to by nptr to int.
    The behaviour is the same as:
      strtol(nptr, NULL, 10); 
    except that atoi() does not detect errors.
    RETURN VALUE
    The converted value.
    Some Examples of Valid Inputs:
    "\t \n+123" = 123
    "  " = 0
    " -123" = -123
    "   -123" = -123
    "  -      123" = 0

    EXTRA CREDIT:

    For Extra credit you can implement strtol and also implement atoi differently. Click here for details
    
        

    Hints & Suggestions:

    1. Start Early 
    2. Revise your C/C++ program as much as possible.
      Try to think about how it will translate to ASM and revise it to make it easier.
      (A more complex/hard to follow C program may translate to a simpler ASM program)
    3. Work slowly and methodically - Translate one line of C at a time.
      Make notes about register usage/etc. before starting the ASM.
    4. I don't care HOW you perform testing, but you must test your functions.
      You don't need to spend time developing an interactive program to test your functions.
    5. You should compare your functions (m_isspace, m_toupper, m_strtol, m_atoi) to the real C library version (isspace, toupper, strtol, atoi) to ensure that they work identically.
    6. Remeber the properties of ASCII
    7. Work on isspace and toupper first, then atoi. (atoi is the most complicated)
    8. Plan this assignment out ahead of time. Identify which functions must use the stack and why they may need it.
      Draw out diagrams of the stack frames to aid your development.
    9. My version of is space was approximately 10 lines of ASM.
      My version of toupper was approximately 10 lines of ASM.
      My version of  atoi was approximately 45 lines of ASM.
    10. You can use character constanst in pseudo-ops: blt $t0,'a',loop
      You may also use numerical constants in pseudo-ops: blt $t0,0x61,loop
      '\n' and '\t' are valid characters. \v=0x0b, \f=0x0c, \r=0x0d.
    Getting Started:
    SPIM Setup:
    For this program you SHOULD load the trap file
    You need to be sure that you are NOT emulating a bare machine