第一次互評作業:MIPS彙編程序設計

第一題:用系統功能調用實現簡單輸入輸出

利用系統功能調用從鍵盤輸入,轉換後在屏幕上顯示,具體要求如下:

(1) 如果輸入的是字母(A~Z,區分大小寫)或數字(0~9),則將其轉換成對應的英文單詞後在屏幕上顯示,對應關係見下表

(2) 若輸入的不是字母或數字,則在屏幕上輸出字符“*”,

(3) 每輸入一個字符,即時轉換並在屏幕上顯示,

(4) 支持反覆輸入,直到按“?”鍵結束程序。

      .data
u_word:     .asciiz
            "Alpha ","Bravo ","China ","Delta ","Echo ","Foxtrot ",
            "Golf ","Hotel ","India ","Juliet ","Kilo ","Lima ",
            "Mary ","November ","Oscar ","Paper ","Quebec ","Research ",
            "Sierra ","Tango ","Uniform ","Victor ","Whisky ","X-ray ",
            "Yankee ","Zulu "
uw_offset:  .word
            0,7,14,21,28,34,43,49,56,63,71,
            77,83,89,99,106,113,121,131,
            139,146,155,163,171,178,186
l_word:     .asciiz
            "alpha ","bravo ","china ","delta ","echo ","foxtrot ",
            "golf ","hotel ","india ","juliet ","kilo ","lima ",
            "mary ","november ","oscar ","paper ","quebec ","research ",
            "sierra ","tango ","uniform ","victor ","whisky ","x-ray ",
            "yankee ","zulu "
lw_offset:  .word
            0,7,14,21,28,34,43,49,56,63,71,
            77,83,89,99,106,113,121,131,
            139,146,155,163,171,178,186
number:     .asciiz
            "zero ", "First ", "Second ", "Third ", "Fourth ",
            "Fifth ", "Sixth ", "Seventh ","Eighth ","Ninth "
n_offset:   .word
            0,6,13,21,28,36,43,50,59,67

            .text
            .globl main
main:       li $v0, 12 # read character
            syscall
            sub $t1, $v0, 63 # '?'
            beqz $t1, exit
            sub $t1, $v0, 48 # '0'
            slt $s0, $t1, $0 # if t1 < 0 then s0 = 1
            bnez $s0, others

            # is number?
            sub $t2, $t1, 10 # number
            slt $s1, $t2, $0 # if t2 < 0 then s1 = 1
            bnez $s1, getnum

            # is capital?
            sub $t2, $v0, 91
            slt $s3, $t2, $0 # if v0 <= 'Z' then s3 = 1
            sub $t3, $v0, 64 
            sgt $s4, $t3, $0 # if v0 >='A' then s4 = 1
            and $s0, $s3, $s4 # if s3 == 1 && s4 == 1 
            bnez $s0, getuword

            # is lower case?
            sub $t2, $v0, 123
            slt $s3, $t2, $0 # if v0 <= 'z' then s3 = 1
            sub $t3, $v0, 96 
            sgt $s4, $t3, $0 # if v0 >= 'a' then s4 = 1
            and $s0, $s3, $s4
            bnez $s0, getlword
            j others

getnum:     add $t2, $t2, 10
            sll $t2, $t2, 2
            la $s0, n_offset
            add $s0, $s0, $t2
            lw $s1, ($s0)
            la $a0, number
            add $a0, $a0, $s1
            li $v0, 4
            syscall
            j main

            # upper case word
getuword:   sub $t3, $t3, 1
            sll $t3, $t3, 2
            la $s0, uw_offset
            add $s0, $s0, $t3
            lw $s1, ($s0)
            la $a0, u_word
            add $a0, $s1, $a0
            li $v0, 4
            syscall
            j main

            # lower case word
getlword:   sub $t3, $t3, 1
            sll $t3, $t3, 2
            la $s0, lw_offset
            add $s0, $s0, $t3
            lw $s1, ($s0)
            la $a0, l_word
            add $a0, $s1, $a0
            li $v0, 4
            syscall
            j main

others:     and $a0, $0, $0
            add $a0, $a0, 42 # '*'
            li $v0, 11 # print character
            syscall
            j main

exit:       li $v0, 10 # exit
            syscall


第二題:字符串查找比較

利用系統功能調用從鍵盤輸入一個字符串,然後輸入單個字符,查找該字符串中是否有該字符(區分大小寫)。具體要求如下:

(1) 如果找到,則在屏幕上顯示:

Success! Location: X

其中,X爲該字符在字符串中第一次出現的位置

(2) 如果沒找到,則在屏幕上顯示:

Fail!

(3) 輸入一個字符串後,可以反覆輸入希望查詢的字符,直到按“?”鍵結束程序

(4) 每個輸入字符獨佔一行,輸出查找結果獨佔一行,位置編碼從1開始。

提示:爲避免歧義,字符串內不包含"?"符號

格式示例如下:

abcdefgh

a

Success! Location: 1

x

Fail!

           .data
msg_s:      .asciiz "\r\nSuccess! Location: "
msg_f:      .asciiz "\r\nFail!\r\n"
s_end:      .asciiz "\r\n"
buf:        .space 100

            .text
            .globl main
main:       la $a0, buf # address of input buffer
            la $a1, 100 # maximum number of characters to read
            li $v0, 8 # read string
            syscall

inputchar:  li $v0, 12 # read character
            syscall
            addi $t7, $0, 63 # '?'
            sub $t6, $t7, $v0
            beq $t6, $0, exit
            add $t0, $0, $0
            la $s1, buf

find_loop:  lb $s0, 0($s1)
            sub $t1, $v0, $s0
            beq $t1, $0, success
            addi $t0, $t0, 1
            slt $t3, $t0, $a1
            beq $t3, $0, fail
            addi $s1 $s1, 1
            j find_loop

success:    la $a0, msg_s
            li $v0, 4 # print string
            syscall
            addi $a0, $t0, 1
            li $v0, 1 # print integer
            syscall
            la $a0, s_end
            li $v0, 4
            syscall
            j inputchar

fail:       la $a0, msg_f
            li $v0, 4
            syscall
            j inputchar

exit:       li $v0, 10
            syscall




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章