简体   繁体   English

MIPS 组装中的 Pig Latin

[英]Pig Latin in MIPS Assembly

In my MIPs Assembly Programming class I've been tasked with writing a program that converts a string into simplified pig latin.在我的 MIP 汇编编程 class 中,我的任务是编写一个将字符串转换为简化的拉丁语的程序。

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".简化的猪拉丁语假设字符串中的所有单词至少有 2 个字符长,并且每个单词的第一个字母都移到末尾,然后是“ay”。 Also, assume there are no punctuation marks of any kind.另外,假设没有任何类型的标点符号。

So, "stackoverflow is great" becomes "tackoverfloway siay reatgay".因此,“stackoverflow 很棒”变成了“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 tackoverflowsay siay reat
gay同性恋

Instead of:代替:

tackoverflowsay siay reatgay 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?我有一个未成熟的 null 终止字符串吗? 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

You almost certainly have a string of the form:您几乎可以肯定有以下形式的字符串:

stackoverflow is great\n

where \n is a newline character.其中\n是换行符。 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.我不会给你代码(因为这是家庭作业),但在我看来,最简单的解决方案是让另一个循环处理整个字符串,用空格替换所有"\n"字符。

This would be done before your latinization loops.这将在您的拉丁化循环之前完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM