2017-10-19 433 views
1

对于我的作业,我需要使用MIPS在前面输入任意字母,然后输入数字(例如x123),然后在数字中加5,然后打印出最终数字(从该示例输出将是128)字符串到int转换和字符串操作MIPS

+0

嗨,欢迎堆栈溢出。为了帮助您,我们需要查看一些代码,预期结果以及迄今的尝试。看看这个快速指南如何提出相关问题:https://stackoverflow.com/help/how-to-ask – Moseleyi

回答

0

。数据

entmsg:.asciiz “输入字符串:\ n” 个 UI1:20。空间计数器 :20。空间 outmsg:.asciiz“的值+5是:\ n“

.text main: #Printing消息 李$ V0,4 LA $ A0,entmsg 系统调用

#Saving the text 
li $v0, 8 
la $a0, ui1 
li $a1, 20 
syscall 
#Count the length of the string 
la $t0, ui1 # la means load address (so we load the address of str into $t0) 
    li $t3, 0 # $t1 is the counter. set it to 0 

countChr:
磅$ T2,0($ T0)#负载从地址$在T0
的第一个字节 beqz $ t2,pot#if $ t2 == 0 then then to label end
add $ t0,$ t0,1#else increment the address
add $ t3,$ t3,1#and add the counter of course
j countChr#finally loop
锅:

li $t1, 1 # $t1 is the counter. set it to 1 
li $t2, 0 #$t2 is the sum 
sub $t3, $t3, $t1 

getint:
LBU $ t0时,UI1($ T1)#负载从地址$在T0
BEQ $ T1,$ t3中,端#NULL终止的第一个字节发现 阿迪$ t0,$ t0,-48#将t1的ascii值转换为dec值 mul $ t2,$ t2,10 #sum * = 10 add $ t2,$ t2,$ t0 #sum + = array [s1] - '0 ' 阿迪$ T1,$ T1,1个#increment阵列地址 Ĵgetint#最后循环

#in the loop take the ascii value of the char and then convert it to an int and then add it to the answer 

末: #将5号 阿迪$ T2,T2 $ 5

#print out second message and text 
li $v0, 4 
la $a0, outmsg 
syscall 

#Print out number 
#Gets the right answer now I just need to be able to output it 
li $v0, 1 
add $a0, $zero, $t2 
syscall 

#Close program 
li $v0, 10 
syscall 

这是所有的代码只是输入的东西是太多的努力得到它的正确格式