MIPS程序设计实例
第一题:用系统功能调用实现简单输入输出
题目要求
利用系统功能调用从键盘输入,转换后在屏幕上显示,具体要求如下:
1、如果输入的是字母(A~Z,区分大小写)或数字(0~9),则将其转换成对应的英文单词后在屏幕上显示,对应关系见下表
2、若输入的不是字母或数字,则在屏幕上输出字符“*”
3、每输入一个字符,即时转换并在屏幕上显示
4、支持反复输入,直到按“?”键结束程序
MIPS代码
1 .data 2 alphabetU: .asciiz " Alpha\n"," Bravo\n"," China\n"," Delta\n"," Echo\n"," Foxtrot\n"," Golf\n"," Hotel\n", 3 " India\n"," Juliet\n"," Kilo\n"," Lima\n"," Mary\n"," November\n"," Oscar\n"," Paper\n"," Quebec\n", 4 " Research\n"," Sierra\n"," Tango\n"," Uniform\n"," Victor\n"," Whisky\n"," X-ray\n"," Yankee\n"," Zulu\n" 5 alphabetL: .asciiz " alpha\n"," bravo\n"," china\n"," delta\n"," echo\n"," foxtrot\n"," golf\n"," hotel\n"," india\n", 6 " juliet\n"," kilo\n"," lima\n"," mary\n"," november\n"," oscar\n"," paper\n"," quebec\n"," research\n", 7 " sierra\n"," tango\n"," uniform\n"," victor\n"," whisky\n"," x-ray\n"," yankee\n"," zulu\n" 8 al_offset: .word 0,8,16,24,32,39,49,56,64,72,81,88,95,102,113,121,129,138,149,158,166,176,185,194,202,211 9 number: .asciiz " Zero\n"," First\n"," Second\n"," Third\n"," Fourth\n"," Fifth\n"," Sixth\n"," Seventh\n"," Eighth\n"," Ninth\n" 10 n_offset: .word 0,7,15,24,32,41,49,57,67,76 11 exit_str: .asciiz "#Stop program#" 12 13 .text 14 .globl main 15 main: 16 li $v0,12 #$v0 contian character read 17 syscall 18 beq $v0,'?',exit #if(v0 == '?'),exit 19 20 #is symble? 21 sub $t1,$v0,'0' 22 blt $t1,0,symble 23 24 #is number? 25 sub $t2,$t1,10 #$t2 = $t1 - 10 26 blt $t2,0,getnum 27 28 #is upper case? 29 sub $t0,$v0,92 30 slt $s2,$t0,$0 #if (v0 < 'Z') then s2 = 1 31 sub $t0,$v0,64 32 sgt $s3,$t0,$0 #if(v0 > 'A') then s3 = 1 33 and $s1,$s2,$s3 #$s1 = ($s2 && s3) 34 beq $s1,1,getuword 35 36 #is lower case? 37 sub $t0,$v0,123 38 slt $s2,$t0,$0 #if (v0 < 'Z') then s2 = 1 39 sub $t0,$v0,96 40 sgt $s3,$t0,$0 #if(v0 > 'A') then s3 = 1 41 and $s1,$s2,$s3 #$s1 = ($s2 && s3) 42 beq $s1,1,getlword 43 44 symble: 45 li $v0,11 46 add $a0,$0,32 #load value 32 into argument register $a0 47 syscall #print space 48 49 add $a0,$a0,10 #32 + 10 = '*' 50 syscall 51 52 sub,$a0,$a0,32 #'*' - 32 = '\n' 53 syscall 54 j main 55 56 getnum: 57 add $t2,$t2,10 #add 10 58 sll $t2,$t2,2 #every word use 1 byte (no.9 at address of 9<<2) 59 60 la $s0,n_offset #load address to $s0 61 add $s0,$s0,$t2 #$s0 = $s0 + offset 62 lw $s1,($s0) 63 64 la $a0,number 65 add $a0,$a0,$s1 #$a0 = number + offset 66 li $v0,4 67 syscall 68 j main 69 70 getuword: 71 sub,$t0,$t0,1 72 sll $t0,$t0,2 73 la $s0,al_offset 74 add $s0,$s0,$t0 75 lw $s1,($s0) 76 la $a0,alphabetU 77 add $a0,$a0,$s1 78 li $v0,4 79 syscall 80 j main 81 82 getlword: 83 sub,$t0,$t0,1 84 sll $t0,$t0,2 85 la $s0,al_offset 86 add $s0,$s0,$t0 87 lw $s1,($s0) 88 la $a0,alphabetL 89 add $a0,$a0,$s1 90 li $v0,4 91 syscall 92 j main 93 94 exit: 95 add $a0,$0,'\n' 96 li $v0,11 97 syscall 98 99 la $a0,exit_str 100 li $v0,4 101 syscall 102
第二题:字符串查找比较
题目要求
利用系统功能调用从键盘输入一个字符串,然后输入单个字符,查找该字符串中是否有该字符(区分大小写)。具体要求如下:
1、如果找到,则在屏幕上显示:
Success! Location: X
其中,X为该字符在字符串中第一次出现的位置
2、如果没找到,则在屏幕上显示
Fail!
3、输入一个字符串后,可以反复输入希望查询的字符,直到按“?”键结束程序
4、每个输入字符独占一行,输出查找结果独占一行,位置编码从1开始。
提示:为避免歧义,字符串内不包含”?”符号
MIPS代码
1 #https://ehye.github.io/2017/10/10/MIPS-program-design0/ 2 .data 3 success_str: .asciiz "\nSuccess! Location: " 4 fail_str: .asciiz "\nFail! 5 buffer: .space 20 6 exit_str: .asciiz "\n#Stop program#" 7 str1: .asciiz "Enter string: " 8 str2: .asciiz "Enter char: 9 10 .text 11 .globl main 12 main: 13 #init 14 # a0 = input/result string 15 # a1 = input string length 16 # a2 = input char 17 # a3 = loop donefail 18 li $a3,0 19 li $t0,0 20 21 #take string 22 la $a0,buffer 23 li $a1,20 24 li $v0,8 25 syscall 26 27 takechar: 28 li $v0,12 29 syscall 30 beq $v0,'?',exit 31 move $a2,$v0 32 33 search: 34 beq $t0,$a1,fail #if (i == length) then donefail 35 la $a0,buffer #else load byte 36 add $t2,$a0,$t0 #$t2 = $a0 + offset 37 lb $s1,($t2) #$s1 = *$t2 38 beq $s1,$a2,success #if($s1 == $a2),thne sucess 39 add $t0,$t0,1 #i++ 40 j search 41 42 fail: 43 add $a3,$zero,1 # flag donefail = 1 44 add $t0,$zero,$zero # reset int i = 0 45 46 la $a0,fail_str 47 li $v0,4 48 syscall 49 50 add $a0,$zero,'\n' 51 li $v0,11 52 syscall 53 j takechar 54 55 success: 56 la $a0,success_str 57 li $v0,4 58 syscall 59 60 li $v0,1 61 add $a0,$t0,1 #print index begin from 1 62 syscall 63 64 add $a0,$zero,'\n' 65 li $v0,11 66 syscall 67 add $t0,$zero,0 #reset int i = 0 68 j takechar 69 70 exit: 71 la $a0,exit_str #print exit 72 li $v0,4 73 syscall 74 75 li $v0,10 #exit 76 syscall
解释一下,由于这个编辑器插入代码不能选择MIPS,所以没有语法高亮,额,哎哎,水平太菜,知道好的解决方法得大佬请留言啊!
个性签名:时间会解决一切