从MIPS(汇编代码)中的字符串读取多位数字

时间:2016-04-12 22:28:50

标签: string assembly mips

我试图从用户那里读取字符串以评估表达式。例如,我想计算((2-(8 * 9)) 1-4))。为了实现这一点,我将读取的数字的字符转换为它们所代表的实际数字,同时使用下面的代码将它们保存回字符串。当我在操作命令' - ()/'之间输入(0-9)的数字时,我的算法很有效。但是我需要在输入字符串中的多位数字时使其工作。例如,((20-(8 * 91))* 1-43))。我知道要转换的算法,让我们说3 1 4到314,但我的问题是,我怎样才能将它插回到我的字节串中,同时在下面的字符上保持相同的顺序?我尝试了很多方法,但无法让它发挥作用。提前谢谢

.data

expression: .space 110            # to store string
stack: .space 110                 # to help with operations

.text


#______reading string from user
li $v0, 8
li $a1, 100
la $a0, expression
syscall
#_____________________________



#___count number of characters in string 
la $s0, expression                           #s0 = adress of string  
li $t1, 0                                      #t1 = # characters
loopcounter:
lb $t0, ($s0)
beq $t0, '\n', outcounter
addi $t1, $t1, 1
addi $s0, $s0, 1
j loopcounter

outcounter:             #if you want to print the amount of characters in the  string
#li $v0, 1
#move $a0, $t1
#syscall
#_______________________________________



#__convert number characters into integers
la $s0, expression                            #s0 = adress of string 
li $t0, 1                                       #t0 loop counter
loopStringtoNum:                               
lb $t2, ($s0)                                    #t2 = ASCII of a digit
blt $t2, '0', skipCharConversion
bgt $t2, '9', skipCharConversion
subi $t2, $t2, 0x30                            #t1 = the digit
sb $t2, ($s0)   
skipCharConversion:
addi $s0, $s0, 1
addi $t0, $t0, 1
ble $t0, $t1, loopStringtoNum                  #skip string to num loop
#_________________________________________

0 个答案:

没有答案