--%>

reads a line of text and tests whether it is a palindrome


palindrome.asm -- reads a line of text and tests whether it is a palindrome. ## Register usage: ## $t1 - A. ## $t2 - B. ## $t3 - the character *A. ## $t4 - the character *B. ## $v0 - syscall parameter / return values. ## $a0 - syscall parameters. ## $a1 - syscall parameters. .globl test_loop .globl length_loop .globl string_space .text main: # SPIM starts by jumping to main. # read the string S: la $a0, string_space li $a1, 1024 li $v0, 8 # load "read_string" code into $v0. syscall la $t1, string_space # A <- S. la $t2, string_space ## we need to move B to the end length_loop: # of the string: lb $t3, ($t2) # load the byte *B into $t3. beqz $t3, end_length_loop # if $t3 == 0, branch out of loop. addu $t2, $t2, 1 # otherwise, increment B, b length_loop # and repeat the loop. end_length_loop: subu $t2, $t2, 2 ## subtract 2 to move B back past # the '\0' and '\n'. test_loop: bge $t1, $t2, is_palin # if A >= B, it is a palindrome. lb $t3, ($t1) # load the byte *A into $t3, lb $t4, ($t2) # load the byte *B into $t4. bne $t3, $t4, not_palin # if $t3 != $t4, not a palindrome. # Otherwise, addu $t1, $t1, 1 # increment A, subu $t2, $t2, 1 # decrement B, b test_loop # and repeat the loop. is_palin: ## print the is_palin_msg, and exit. la $a0, is_palin_msg li $v0, 4 syscall b exit not_palin: la $a0, not_palin_msg ## print the is_palin_msg, and exit. li $v0, 4 syscall exit: ## exit the program: li $v0, 10 # load "exit" into $v0. syscall # make the system call. ## Here is where the data for this program is stored: .data string_space: .space 1024 # set aside 1024 bytes for the string. is_palin_msg: .asciiz "The string is a palindrome.\n" not_palin_msg: .asciiz "The string is not a palindrome.\n" ## end of palindrome.asm

 

 

   Related Questions in Programming Languages

  • Q : Describe Locale Locale : The details

    Locale: The details which are dependent on conventions and customs approved by a specific country or culture. Within programs, this influences issues like number and date formatting, for example. Designers of classes must be sensitive to the locale-sp

  • Q : Write a program to display its negative

    Write a program in object code that reads a single digit decimal number and displays its negative in binary.  To do this, you must first read the number as a character and then convert it to its numeric value, as discussed in class.  Then, you're going to change this to a negative numbe

  • Q : How virtual machine simplify writing

    How does a virtual machine simplify the task of writing a distributed application?

  • Q : Explain Java PathFinder Java

    Java PathFinder: Java PathFinder (JPF) is a model checker that has been developed as a verification and testing environment for Java programs (Figure shown below). It is available as open source at SourceForge.net (JPF website). It consists of a custo

  • Q : Write a program and estimate pi using

    Consider a dartboard of radius 1. Since the area of the board will be  π r2 = π * 1 *1, it's clear the area of the dartboard is exactly π. The area of a square surrounding the board (circumscribing it) would be 2*2 = 4, sin

  • Q : Explain Timesharing system Timesharing

    Timesharing system: It is an operating system which shares processor time among multiple processes by assigning each a time slice. Once a process's time slice has finished or expired, the other procedure is given a possibility to run.

  • Q : Basic features of OOPs Illustrate the

    Illustrate the basic features of OOPs?

  • Q : Define the term Mutual recursion Define

    Define the term Mutual recursion: Recursion which outcomes from two methods calling one other recursively.

  • Q : Define the term Monitor Define the term

    Define the term Monitor: It is an object with one or more synchronized techniques.

  • Q : Explain a quality and metrics reporting

    What opinion would you provide to someone who asked you where to begin to introduce to their company a quality and metrics reporting program?