2017-10-19 193 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 

這是所有的代碼只是輸入的東西是太多的努力得到它的正確格式