MIPS 排序和数组

发布于 2024-10-02 04:13:40 字数 3506 浏览 0 评论 0原文

我有这个家庭作业问题。我想编写一个程序来执行以下操作:

  1. 将句子中的字母按 ASCII 整理顺序(即按字母顺序)排序。打印结果和句子中的字符总数。
  2. 接下来,打印出每个字母在句子中出现的次数列表。
  3. 最后,打印出句子中不同字符的总数。

当我尝试测试第一个要求时,我没有收到输出字符串。 我还需要帮助完成后两项。我对 MIPS 相当陌生,对它有一些了解,但有一些困难。我修改了一些代码以对我找到的示例代码进行排序。

        .data
length: .word   0
buffer: .space 255
prompt1:.asciiz  "Type in a sentence: "

after:  .asciiz  "\nString  after: "


    .text


# Start of code section

main:
      li       $v0, 4         # Prompt user for a text string
      la       $a0, prompt1      # address of string to print
      syscall
      li       $v0, 8         # Read in text string
      la       $a0, buffer
      li       $a1, 255
      syscall

      la       $t0, buffer    #  Read character from text string
      lw       $t1, length
      addu     $t0, $t0, $t1

# Call on QuickSort

        la      $a0, buffer       # Constant pointer to string

        add     $a1, $a0, 0      # Pointer to left end

        add     $a2, $a0, 25     # Pointer to right end

        jal     QS               # The one call from main

# Print out results

        la      $a0, after

        li      $v0, 4

        syscall

        la      $a0, buffer

        syscall

# End main

        li      $v0, 10

        syscall


QS:     sub     $sp, $sp, 16     # \
        sw      $a1, 0($sp)      #  \

        sw      $a2, 4($sp)      #   > Save registers

        sw      $t2, 8($sp)      #  /  

        sw      $ra, 12($sp)     # / and return address

        bge     $a1, $a2, QSret  # Nothing to sort   

        sub     $t3, $a1, $a0     # index i

        sub     $t4, $a2, $a0     # index j

        add     $t0, $t3, $t4     # t0 = i+j choose middle

        sra     $t0, $t0, 1       # t0 = (i+j) div 2

        add     $t0, $t0, $a0     # t0 -> A[(i+j) div 2]

        lb      $t5, 0($t0)       # t5 = pivot value 

        lb      $t6, 0($a1)       # t6 = A[i] = left element

        sb      $t5, 0($a1)       # Swap them so pivot

        sb      $t6, 0($t0)       # is first element
# 

        move    $t1, $a1         # Initdially i -> first (pivot) item a1

        add     $t2, $a2, 1      # Initially j -> just past last item a2

        lb      $t0, 0($a1)      # Pivot value in t0 (in $t5)

# 
loop:
# 

i_loop: add     $t1, $t1, 1      # i=i+1;

        lb      $t5, 0($t1)      # t5 = A[i]

        bgt     $t0, $t5, i_loop # until pivot <= A[i]

j_loop: sub     $t2, $t2, 1      # j=j-1;

        lb      $t6, 0($t2)      # t6 = A[j]

        blt     $t0, $t6, j_loop # until pivot >= A[j]
#

        bge     $t1, $t2, test   # if i<j swap
#

        sb      $t6, 0($t1)      # A[i] and

        sb      $t5, 0($t2)      # A[j]
#

test:   blt     $t1, $t2, loop   # until i >= j
#

        lb      $t5, 0($a1)      # swap a[a1] = pivot

        lb      $t6, 0($t2)      # and a[j]

        sb      $t5, 0($t2)      # now a[j] is

        sb      $t6, 0($a1)      # in its final position

# Done with partition

        lw      $a1, 0($sp)

        sub     $a2, $t2, 1

        jal     QS               # Recursive call on left

        add     $a1, $t2, 1

        lw      $a2, 4($sp)

        jal     QS               # Recursive call on right
#

QSret:  lw      $ra, 12($sp)     # \Replace return address

        lw      $t2, 8($sp)      #  \

        lw      $a2, 4($sp)      #   > and registers

        lw      $a1, 0($sp)      #  /
        add     $sp, $sp, 16     # /

        jr      $ra

I have this homework problem. I'm suppose to write a program that does the following:

  1. Sort the letters in the sentence into the ASCII collating sequence (i.e. alphabetically). Print the results and the total number of characters in the sentence.
  2. Next, print out a list of the number of times each letter occurred in the sentence.
  3. Finally, print out the total number of different characters in the sentence.

When I try to test out the first requirement, I don't receive the output string.
I also need help on doing the second two. I'm fairly new to MIPS, and I have some understanding of it, but having some difficulty. I modified some code for the sorting from example code I found.

        .data
length: .word   0
buffer: .space 255
prompt1:.asciiz  "Type in a sentence: "

after:  .asciiz  "\nString  after: "


    .text


# Start of code section

main:
      li       $v0, 4         # Prompt user for a text string
      la       $a0, prompt1      # address of string to print
      syscall
      li       $v0, 8         # Read in text string
      la       $a0, buffer
      li       $a1, 255
      syscall

      la       $t0, buffer    #  Read character from text string
      lw       $t1, length
      addu     $t0, $t0, $t1

# Call on QuickSort

        la      $a0, buffer       # Constant pointer to string

        add     $a1, $a0, 0      # Pointer to left end

        add     $a2, $a0, 25     # Pointer to right end

        jal     QS               # The one call from main

# Print out results

        la      $a0, after

        li      $v0, 4

        syscall

        la      $a0, buffer

        syscall

# End main

        li      $v0, 10

        syscall


QS:     sub     $sp, $sp, 16     # \
        sw      $a1, 0($sp)      #  \

        sw      $a2, 4($sp)      #   > Save registers

        sw      $t2, 8($sp)      #  /  

        sw      $ra, 12($sp)     # / and return address

        bge     $a1, $a2, QSret  # Nothing to sort   

        sub     $t3, $a1, $a0     # index i

        sub     $t4, $a2, $a0     # index j

        add     $t0, $t3, $t4     # t0 = i+j choose middle

        sra     $t0, $t0, 1       # t0 = (i+j) div 2

        add     $t0, $t0, $a0     # t0 -> A[(i+j) div 2]

        lb      $t5, 0($t0)       # t5 = pivot value 

        lb      $t6, 0($a1)       # t6 = A[i] = left element

        sb      $t5, 0($a1)       # Swap them so pivot

        sb      $t6, 0($t0)       # is first element
# 

        move    $t1, $a1         # Initdially i -> first (pivot) item a1

        add     $t2, $a2, 1      # Initially j -> just past last item a2

        lb      $t0, 0($a1)      # Pivot value in t0 (in $t5)

# 
loop:
# 

i_loop: add     $t1, $t1, 1      # i=i+1;

        lb      $t5, 0($t1)      # t5 = A[i]

        bgt     $t0, $t5, i_loop # until pivot <= A[i]

j_loop: sub     $t2, $t2, 1      # j=j-1;

        lb      $t6, 0($t2)      # t6 = A[j]

        blt     $t0, $t6, j_loop # until pivot >= A[j]
#

        bge     $t1, $t2, test   # if i<j swap
#

        sb      $t6, 0($t1)      # A[i] and

        sb      $t5, 0($t2)      # A[j]
#

test:   blt     $t1, $t2, loop   # until i >= j
#

        lb      $t5, 0($a1)      # swap a[a1] = pivot

        lb      $t6, 0($t2)      # and a[j]

        sb      $t5, 0($t2)      # now a[j] is

        sb      $t6, 0($a1)      # in its final position

# Done with partition

        lw      $a1, 0($sp)

        sub     $a2, $t2, 1

        jal     QS               # Recursive call on left

        add     $a1, $t2, 1

        lw      $a2, 4($sp)

        jal     QS               # Recursive call on right
#

QSret:  lw      $ra, 12($sp)     # \Replace return address

        lw      $t2, 8($sp)      #  \

        lw      $a2, 4($sp)      #   > and registers

        lw      $a1, 0($sp)      #  /
        add     $sp, $sp, 16     # /

        jr      $ra

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷港 2024-10-09 04:13:40
buffer: .space 255

这将用零填充缓冲区

li       $v0, 8         # Read in text string
la       $a0, buffer
li       $a1, 255
syscall

我不知道你正在使用什么环境,但这通常就像 C 中的 fgets() 一样工作,所以如果你输入 hello,你的缓冲区最终将是:

+-----+-----+-----+-----+-----+-----+-----+-----+   more    +-----+
| 'h' | 'e' | 'l' | 'l' | 'o' |'\n' |  0  |  0  |..zeroes...|  0  |
+-----+-----+-----+-----+-----+-----+-----+-----+           +-----+

之后的代码似乎没有做任何有用的事情:

la       $t0, buffer    #  Read character from text string
lw       $t1, length
addu     $t0, $t0, $t1

但是 length 从未被写入,并且 $t0 没有再次使用(直到它在 内部被覆盖>QS)。

QS 的调用会在 $a2 中传递一个固定值(25 - 为什么?)。假设 QS 例程实际上如宣传的那样工作:如果原始字符串很短,缓冲区中的一些零字节将包含在排序的范围中,因此将在开头结束缓冲区的范围 - 排序的范围将如下所示:

+-----+   more    +-----+-----+-----+-----+-----+-----+-----+-----+
|  0  |..zeroes...|  0  |  0  |'\n' | 'e' | 'h' | 'l' | 'l' | 'o' |
+-----+           +-----+-----+-----+-----+-----+-----+-----+-----+

即,如果排序的范围包含任何零字节,则结果的第一个字节将为零,因此您将打印一个空字符串。

第 2 部分:一旦有了正确排序的字符串,这应该很简单,因为每个字符的所有出现都是相邻的。只需沿着字符串走,并考虑每个字符是否与前一个字符相同或不同。

第 3 部分:在完成第 2 部分的工作时只需要一个额外的计数器。

buffer: .space 255

This fills buffer with zeroes.

li       $v0, 8         # Read in text string
la       $a0, buffer
li       $a1, 255
syscall

I don't know what environment you're using, but this typically works just like fgets() in C, so if you enter hello, your buffer will end up as:

+-----+-----+-----+-----+-----+-----+-----+-----+   more    +-----+
| 'h' | 'e' | 'l' | 'l' | 'o' |'\n' |  0  |  0  |..zeroes...|  0  |
+-----+-----+-----+-----+-----+-----+-----+-----+           +-----+

The code just after that doesn't seem to do anything useful:

la       $t0, buffer    #  Read character from text string
lw       $t1, length
addu     $t0, $t0, $t1

but length has never been written and $t0 isn't used again (until it's overwritten inside QS).

The call to QS passes a fixed value in $a2 (25 - why?). Assuming that the QS routine actually works as advertised: if your original string is short, some of the zero bytes in the buffer will be included in the range that gets sorted, and so will end up at the beginning of the buffer - the range that gets sorted will look something like this:

+-----+   more    +-----+-----+-----+-----+-----+-----+-----+-----+
|  0  |..zeroes...|  0  |  0  |'\n' | 'e' | 'h' | 'l' | 'l' | 'o' |
+-----+           +-----+-----+-----+-----+-----+-----+-----+-----+

i.e. if the range that gets sorted includes any zero bytes, the first byte of the result will be zero, and so you will print an empty string.

Part 2: once you have a correctly sorted string, this should be straightforward, as all of the occurences of each character are adjacent. Just walk along the string, and consider whether each character is the same as the previous one, or different.

Part 3: just needs an additional counter while doing the work for part 2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文