|CS 354||Computer Organization and Systems||Fall 2017|
Lab 8 - Using Characters and Strings in MIPS to verify palindromes
In this lab you will gain familiarity with basic MIPS assembly programming related to the representation of characters, null-terminated strings, and memory access. You will do this by writing a MIPS program to determine if an input string is a palindrome.
Character Representation in MIPS
Characters in MIPS are represented using 8 bits using the ASCII standard (developed in part by 1940 Albion alumnus Bob Bemer). Bemer received the IEEE Computer Pioneer Award in 2002 for "meeting the world's needs for variant character sets and other symbols, via ASCII, ASCII-alternate sets, and escape sequences." Unicode is a 16 bit character standard is used for character representation in Java. The green reference card in your book contains an ASCII chart and that each character can be associated with a number. For example, the ASCII bit pattern for the letter 'A' can be interpreted as the base 10 number 65 and the binary pattern 01000001. Look carefully at the bit pattern for 'a' and you will see it is 01100001. Compare the bit patterns for the upper and lower case representations of each letter and observe a very interesting pattern!
Because characters in MIPS occupy 8 bits and the word size is 32 bits, there is an issue with which memory location holds what byte. This is important when dealing with data that is smaller than the word size and is called the endian-ness of a machine. The Intel architecture uses a little-endian format. Some other architectures, such as Sun SPARC and Macintosh, use a big-endian format. See our book for more information.
String Representation in MIPS
Strings in MIPS are represented by a sequential group of characters starting at one location and going until a byte with a value of zero (null) is found. This is called a null terminated string and is a common string represention (used in the C language).Image you would like a program that reads a string from the user, counts the length of the string, then prints the string and the length of the string. The program string.s shown below illustrates one way to do this. Download and run this program, stepping through the program and watch the memory and the register values change.
# String length and echo .text main: # Get the string from user li $v0, 4 # Load 4=print_string into $v0 la $a0, prompt # Load address of prompt into $a0 syscall # Output the prompt la $a0, prompt # Load address of prompt into $a0 li $v0, 8 # Load 8=read_string into $v0 la $a0, str # $a0=address of str li $a1, 1024 # $a1= max str length syscall # read string # Compute string length la $s0, str # $s0 contains base address of str add $s2, $0, $0 # $s2 = 0 addi $s3, $0, '\n' # $s2 = '\n' loop: lb $s1, 0($s0) # load character into $s0 beq $s1, $s3, end # Break if byte is newline addi $s2, $s2, 1 # increment counter addi $s0, $s0, 1 # increment str address j loop end: sb $0, 0($s0) #replace newline with 0 # Output li $v0, 4 # Load 4=print_string into $v0 la $a0, r1 # Load address of newline into $a0 syscall # Output the newline li $v0, 4 # Load 4=print_string into $v0 la $a0, str # Load address of newline into $a0 syscall # Output the newline li $v0, 4 # Load 4=print_string into $v0 la $a0, r2 # Load address of newline into $a0 syscall # Output the newline li $v0, 1 # Load 1=print_int into $v0 add $a0, $s2, $0 # Load first number into $a0 syscall # Output the prompt via syscall li $v0, 4 # Load 4=print_string into $v0 la $a0, r3 # Load address of newline into $a0 syscall # Output the newline # Exit Gracefully li $v0, 10 syscall .data prompt: .asciiz "Enter a string of characters: " str: .space 1024 # Allocate 1024 bytes for the input string r1: .asciiz "The string \"" r2: .asciiz "\" has " r3: .asciiz " characters." newline: .asciiz "\n"
Here are some important things to note about the above program. You can read a string using a syscall, as you have done to read and print integers, using a code of 8 loaded into $v0. You also need to supply the address in memory to load the string in $a0 and the maximum number of characters to load in $a1, including the null termination character (this should be the same as the amount of space allocated). Since a newline character is included when the string is read, the above program replaces it with a null character (0).
Recall that a palindrome is a word or phrase in which the characters can be reversed without changing the word or phrase (ignoring case and any punctuation). Examples include:
Write a MIPS program that prompts the user for an input string and reports the if the string is a palindrome or the string is not a palindrome. Ignore any differences in case and any punctuation. For example, entering the string "Albion College", your program should report 'The string "Albion College" is not a palindrome.' Likewise, entering the string "Rats live on no evil star", your program should report 'The string "Rats live on no evil star" is a palindrome!'
Send me an email with the MIPS file containing your program. Comment each line and code segment explaining its function.
Copyright © 2017, David A. Reimann. All rights reserved.