Question

In my MIPs Assembly Programming class I've been tasked with writing a program that converts a string into simplified pig latin.

Simplified pig latin assumes all words in the string are at least 2 characters long, and every word has its first letter shifted to the end followed by "ay". Also, assume there are no punctuation marks of any kind.

So, "stackoverflow is great" becomes "tackoverfloway siay reatgay".

I've taken a stab at this program and I'm nearly completed. It works fine, except that for some reason I have a line break in the middle of the last word every time.

For example:

tackoverflowsay siay reat
gay

Instead of:

tackoverflowsay siay reatgay

I've tried decuding what would be causing this line break in the last exit portion of my program but I don't see it. Do I have a pre-mature null terminated string? If I do, I don't see it.

Here is my code:

####################################################
#  Text Segment
####################################################
        .text
        .globl main
main:
        la $t0, char
        la $t1, buffer
        lb $t3, space

        la $a0, prompt
        li $v0, 4
        syscall

        la $a0, buffer
        li $a1, 200
        li $v0, 8
        syscall

        lb $t2, ($t1)           # Load first char
        addi $t1, $t1, 1

loop:
        lb $t4, ($t1)           # Load next character into $t4

        addi $t1, $t1, 1
        beqz $t4, exit          # Reached end of string, exit?  
        beq $t3, $t4, loop2     # If $t4 = " " char, second loop

        move $a0, $t4           # else, lets keep printing chars
        li $v0, 11
        syscall

        j loop

loop2:
        move $a0, $t2
        li $v0, 11
        syscall

        la $a0, aystr
        li $v0, 4
        syscall

        lb $t2, ($t1)   
        addi $t1, $t1, 1    

        j loop
exit:

        move $a0, $t2
        li $v0, 11
        syscall

        la $a0, aystr
        li $v0, 4
        syscall

        li $v0, 10
        syscall         # Cya...

####################################################
#  Data Segment
####################################################

        .data
prompt: .asciiz "Enter Phrase: "
result: .asciiz "Pig Latin: "
space:  .ascii " "
aystr:  .asciiz "ay "
char:   .byte 1
buffer: .byte 200
Was it helpful?

Solution

You almost certainly have a string of the form:

stackoverflow is great\n

where \n is a newline character. This would translate into:

tackoverflowsay siahy reat\ngay

if you simplistically detected the end of the word as either space or null-terminator.

I'm not going to give you the code (since this is homework) but the easiest solution, in my opinion, would be to have another loop processing the entire string, replacing all "\n" characters with spaces.

This would be done before your latinization loops.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top