MIPS程序设计实例
第一题:用系统功能调用实现简单输入输出
题目要求
利用系统功能调用从键盘输入,转换后在屏幕上显示,具体要求如下:
1、如果输入的是字母(A~Z,区分大小写)或数字(0~9),则将其转换成对应的英文单词后在屏幕上显示,对应关系见下表
2、若输入的不是字母或数字,则在屏幕上输出字符“*”
3、每输入一个字符,即时转换并在屏幕上显示
4、支持反复输入,直到按“?”键结束程序
MIPS代码
.data
alphabetU: .asciiz " Alpha\n"," Bravo\n"," China\n"," Delta\n"," Echo\n"," Foxtrot\n"," Golf\n"," Hotel\n",
" India\n"," Juliet\n"," Kilo\n"," Lima\n"," Mary\n"," November\n"," Oscar\n"," Paper\n"," Quebec\n",
" Research\n"," Sierra\n"," Tango\n"," Uniform\n"," Victor\n"," Whisky\n"," X-ray\n"," Yankee\n"," Zulu\n"
alphabetL: .asciiz " alpha\n"," bravo\n"," china\n"," delta\n"," echo\n"," foxtrot\n"," golf\n"," hotel\n"," india\n",
" juliet\n"," kilo\n"," lima\n"," mary\n"," november\n"," oscar\n"," paper\n"," quebec\n"," research\n",
" sierra\n"," tango\n"," uniform\n"," victor\n"," whisky\n"," x-ray\n"," yankee\n"," zulu\n"
al_offset: .word ,,,,,,,,,,,,,,,,,,,,,,,,,
number: .asciiz " Zero\n"," First\n"," Second\n"," Third\n"," Fourth\n"," Fifth\n"," Sixth\n"," Seventh\n"," Eighth\n"," Ninth\n"
n_offset: .word ,,,,,,,,,
exit_str: .asciiz "#Stop program#" .text
.globl main
main:
li $v0, #$v0 contian character read
syscall
beq $v0,'?',exit #if(v0 == '?'),exit #is symble?
sub $t1,$v0,''
blt $t1,,symble #is number?
sub $t2,$t1, #$t2 = $t1 -
blt $t2,,getnum #is upper case?
sub $t0,$v0,
slt $s2,$t0,$ #if (v0 < 'Z') then s2 = 1
sub $t0,$v0,
sgt $s3,$t0,$ #if(v0 > 'A') then s3 = 1
and $s1,$s2,$s3 #$s1 = ($s2 && s3)
beq $s1,,getuword #is lower case?
sub $t0,$v0,
slt $s2,$t0,$ #if (v0 < 'Z') then s2 = 1
sub $t0,$v0,
sgt $s3,$t0,$ #if(v0 > 'A') then s3 = 1
and $s1,$s2,$s3 #$s1 = ($s2 && s3)
beq $s1,,getlword symble:
li $v0,
add $a0,$, #load value into argument register $a0
syscall #print space add $a0,$a0, # + = '*'
syscall sub,$a0,$a0, #'*' - = '\n'
syscall
j main getnum:
add $t2,$t2, #add
sll $t2,$t2, #every word use byte (no. at address of <<) la $s0,n_offset #load address to $s0
add $s0,$s0,$t2 #$s0 = $s0 + offset
lw $s1,($s0) la $a0,number
add $a0,$a0,$s1 #$a0 = number + offset
li $v0,
syscall
j main getuword:
sub,$t0,$t0,
sll $t0,$t0,
la $s0,al_offset
add $s0,$s0,$t0
lw $s1,($s0)
la $a0,alphabetU
add $a0,$a0,$s1
li $v0,
syscall
j main getlword:
sub,$t0,$t0,
sll $t0,$t0,
la $s0,al_offset
add $s0,$s0,$t0
lw $s1,($s0)
la $a0,alphabetL
add $a0,$a0,$s1
li $v0,
syscall
j main exit:
add $a0,$,'\n'
li $v0,
syscall la $a0,exit_str
li $v0,
syscall
第二题:字符串查找比较
题目要求
利用系统功能调用从键盘输入一个字符串,然后输入单个字符,查找该字符串中是否有该字符(区分大小写)。具体要求如下:
1、如果找到,则在屏幕上显示:
Success! Location: X
其中,X为该字符在字符串中第一次出现的位置
2、如果没找到,则在屏幕上显示
Fail!
3、输入一个字符串后,可以反复输入希望查询的字符,直到按“?”键结束程序
4、每个输入字符独占一行,输出查找结果独占一行,位置编码从1开始。
提示:为避免歧义,字符串内不包含”?”符号
MIPS代码
#https://ehye.github.io/2017/10/10/MIPS-program-design0/
.data
success_str: .asciiz "\nSuccess! Location: "
fail_str: .asciiz "\nFail!
buffer: .space
exit_str: .asciiz "\n#Stop program#"
str1: .asciiz "Enter string: "
str2: .asciiz "Enter char: .text
.globl main
main:
#init
# a0 = input/result string
# a1 = input string length
# a2 = input char
# a3 = loop donefail
li $a3,
li $t0, #take string
la $a0,buffer
li $a1,
li $v0,
syscall takechar:
li $v0,
syscall
beq $v0,'?',exit
move $a2,$v0 search:
beq $t0,$a1,fail #if (i == length) then donefail
la $a0,buffer #else load byte
add $t2,$a0,$t0 #$t2 = $a0 + offset
lb $s1,($t2) #$s1 = *$t2
beq $s1,$a2,success #if($s1 == $a2),thne sucess
add $t0,$t0, #i++
j search fail:
add $a3,$zero, # flag donefail =
add $t0,$zero,$zero # reset int i = la $a0,fail_str
li $v0,
syscall add $a0,$zero,'\n'
li $v0,
syscall
j takechar success:
la $a0,success_str
li $v0,
syscall li $v0,
add $a0,$t0, #print index begin from
syscall add $a0,$zero,'\n'
li $v0,
syscall
add $t0,$zero, #reset int i =
j takechar exit:
la $a0,exit_str #print exit
li $v0,
syscall li $v0, #exit
syscall
解释一下,由于这个编辑器插入代码不能选择MIPS,所以没有语法高亮,额,哎哎,水平太菜,知道好的解决方法得大佬请留言啊!
MIPS程序设计实例的更多相关文章
- gpgpu-sim卡分配程序设计实例分析
gpgpu-sim卡分配程序设计实例分析 运行代码地址:https://github.com/gpgpu-sim/gpgpu-sim_distribution 一.概述 此文件包含有关安装.生成和运行 ...
- Java 打印程序设计实例
3.1 打印文本 3.1.1 应用场景 假设我们需要打印一个窗体的某个文本编辑域(可能只有几行,也可能包含多页)的内容,并且每页最多打印 54 行,如何实现呢? 3.1.2 解决方法 基本思路如下:首 ...
- 《Java程序设计》终极不改版【下】
package 大学生信息管理系统; import java.awt.event.*; import javax.swing.*; import java.awt.*; import java.uti ...
- 《程序设计方法》【PDF】下载
内容简介 <程序设计方法>主要以方法为主导,结合C语言,把程序设计方法学研究中若干成熟的理论和方法用通俗易懂的语言描述出来.<程序设计方法>还选取趣味性强.技巧性高.能够启发学 ...
- 批处理引擎MapReduce程序设计
批处理引擎MapReduce程序设计 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MapReduce API Hadoop同时提供了新旧两套MapReduce API,新AP ...
- C#图书资源【更新中...】喜欢的就转存吧
百度分享暂时取消了,需要的朋友可以直接关注我,我发给你. 重点篇 1.C#与.NET3.5高级程序设计 2.C#与.NET3.5高级程序设计.rar 3.Effective_C#_中文版_改善C#程序 ...
- 3000本IT书籍下载地址
http://www.shouce.ren/post/d/id/112300 黑客攻防实战入门与提高.pdfhttp://www.shouce.ren/post/d/id/112299 黑 ...
- B/S在北大青鸟-ASP.NET 总结
一个.前言: 这几周跟着于海涛老师进入了.NET编程世界.领略到了ASP.NET的精髓. 要说起ASP.NET的发展史,那要追溯到HTML了,由于它功能简单,无法从用户接收信息并自己主动进行更新.而不 ...
- 如何学习FPGA
如何学习FPGA 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/k331922164/article/details/44626989 PS:笔者强 ...
随机推荐
- 5-1条件运算符 & 5-2
三目运算符 新建类: ConditionDemo 用三目运算符: package com.imooc.operator; public class ConditionDemo { public sta ...
- struts2 中的 addActionError 、addFieldError、addActionMessage的方法
addActionError .addFieldError.addActionMessage都是ActionSupport的方法 一.addActionError("错误内容"): ...
- humans.txt学习理解
可以通过以下链接访问到Google的humans.txt https://www.google.com/humans.txt 百度上有关于humans.txt的介绍 http://baike.baid ...
- Swift3.0 数组(Array)
学习了数组的定义,创建,和元素的,增,删,改 //定义数组类型 var array1: Array<String> var array2:[String] //创建一个空数组,int类型元 ...
- IT兄弟连 JavaWeb教程 EL表达式获取对象的属性以及数组的元素
使用${对象名.属性名} EL表达式语言可以使用点号运算符"."来访问对象的属性,例如表达式${customer.name}表示customer对象的name属性. 使用${对象名 ...
- 简单重载运算符in priority_queue By cellur925
我们都知道priority_queue是大根堆. 一.变成小根堆 法一:把元素的相反数丢进堆中 法二 priority_queue<int,vector<int>,greater&l ...
- [Noip2012普及组]摆花
Description 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共 m 盆.通过调查顾客的喜好,小明列出了顾客最喜欢的 n 种花,从 1 到 n 标号.为了在门口展出更多种花,规定 ...
- random 库
random 是使用随机数的python 标准库 ——为随机数:采用梅森旋转算法生成的(伪)随机序列中的元素 —— import random 基本随机数函数:seed(),random() 扩展随机 ...
- python_argparse
使用python argparser处理命令行参数 #coding:utf-8 # 导入模块 import argparse # 创建ArgumentParser()对象 parser = argpa ...
- 【aspnetcore】在过滤器(Filter)中使用注入服务(ServiceFilter|TypeFilter)
在MVC中,AOP是很常用的功能,我们经常会使用如 ActionFilter,IAuthorizeFilter 等描述对Controller和Action进行约束和扩展,一般做法如下: public ...