转换的ASCII数字字符串在MIPS /汇编成int [英] Convert String of ASCII digits to int in MIPS/Assembler

查看:2787
本文介绍了转换的ASCII数字字符串在MIPS /汇编成int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些MIPS code采取的ASCII数字串和字符串转换为整数。该字符串是通过用户输入,并且可以是在长度为10位。我的code正常工作,使用表演的明显的方法循环除了由数组的索引号10的幂字符串中的最低有效数相乘后,从最后一个数字开始进入(10 ^ 0)至第一个数字输入(10 ^ N,N- =阵列中的位数)。

我想知道如果有是会更快或更短写一个替代方法。我特别想知道,如果使用的是逻辑移位可能会使这个过程更短。优化或改善该code任何想法将大大AP preciated!

此外,作为一个侧面说明,我想打电话给gets和子程序的readInt使用日航,但由于获取和双方的readInt子程序调用使用JAL的主要方法调用获取或引起的readInt问题。任何想法如何解决这个问题?再次感谢
干杯

PS:对不起,这个code意见的格式,复制和从火星模拟器入堆栈溢出文本框中粘贴导致对准处于关机状态:/

  #IO
#Prompts用户输入10 ASCII数字成阵列
#Converts数字串到单个int
#Also处理1和10之间的任何数量的数字
#Returns 0,如果非数字字符输入到串瓦尔的。数据#declaration如下
数组:对于10 ELEM阵列。空间11 #reserves空间
焦炭:2。空间
提示:.asciiz请输入10个数字,然后preSS ENTER键:\\ n
空:.asciiz
空间:.ascii
换行:.asciiz\\ n
的.text #instructions跟随主要:
LA $ A0,提示#load提示信息到$ A0的系统调用
李$ V0,4 #load系统调用来打印字符串
系统调用#PRINT提示信息
Ĵ的readInt #CALL的readInt函数来获取用户输入的字符串得到:从键盘缓冲区#阅读多个字符,直到ENTER键,
                            #将NULL char和存储到缓存指向数组*
                            #passed给子程序
LA $ S1,数组的数组#SET基址S1
循环:读取循环的#START
日航GETC #jump到子程序的getc
磅$ T0,焦炭#load焦炭从字符缓冲区为T0,剥空
SB $ T0,0($ S1)#store炭到数组的第n个ELEM
磅$ T1,换行符#load换行字符到T1
BEQ $ T0,T1 $,做串#END?跳转到DONE
阿迪$ S1,S1 $,数组的1 #increments基址
j循环#jump开始读循环GETC:从键盘缓冲区和返回ASCII值#阅读CHAR
李$ V0,8 #CALL code的读取字符串
LA $ A0,读字符的字符#load地址
李$ A1,2 #length字符串是1字节char和1字节的空
系统调用#store从输入缓冲区炭字节转换成char
JR $ RA#跳注册到调用函数的readInt:ASCII码数字,商店#阅读串入一个局部变量,
                    #convert为整数,回报诠释除非字符串包含
                    #非整数
Ĵ得到#let S1是数组的首地址,让S0是digitcounter
完成:#let S2是总和
阿迪$ S1,S1 $,-1 #reposition数组指针到最后一个字符换行字符前
LA $ S0,数组的数组#SET基址S0用作反
阿迪$ S0,S0 $,-1 #reposition基本阵列读取字符串最左边的字符
加$ S2,$零,零$一笔#initialize为0
李$ T0,10#设置T0为10,用于十进制转换
李$ T3,1
磅$ T1,0($ S1)从数组T1 #load CHAR
BLT $ T1,48#检查错误如果char是不是一个数字(ASCII<'0')
BGT $ T1,57,#检查错误如果char是不是一个数字(ASCII>'9')
阿迪$ T1,T1 $,-48 #converts T1的ASCII值十进制值
加$ S2,S2 $,$ T1#将T1的十进制值SumTotal公司
阿迪$ S1,S1 $,-1 #decrement阵列地址
LP:#LOOP为所有数字preceeding的LSB
MUL $ T3,T3 $,$ 10 T0 #multiply电源
BEQ $ S1,S0 $,FIN #exit如果达到字符串的开头
磅$ T1从阵列分为T1,($ S1)#load CHAR
BLT $ T1,48#检查错误如果char是不是一个数字(ASCII<'0')
BGT $ T1,57,#检查错误如果char是不是一个数字(ASCII>'9')
阿迪$ T1,T1 $,-48 #converts T1的ASCII值十进制值
MUL $ T1,T1 $,$#T3 T1 * 10 ^(计数器)
加$ S2,S2 $,$#T1 = SumTotal公司+ SumTotal公司T1
阿迪$ S1,S1 $,-1 #decrement阵列地址
ĴLP #jump开始循环错误:#如果输入非数字字符,返回的readInt 0
加$ S2,$零,零$
ĴFIN鳍:
李$ V0,1
加$ A0,$ S2,$零
系统调用
李$ V0,10 #ends程序
系统调用


解决方案

由安定面膜前四位的字符串为0x0F 这样的低于

 安迪$ T0,$#t0,0x0F其中$ T0包含ASCII数字。

现在 $ T0 有它的INT。

Im writing some MIPS code to take a string of ASCII digits and convert the string into an integer. The string is entered by the user and can be at most 10 digits in length. My code works fine and uses the obvious method of performing looped addition after multiplying the Least Significant number in the string by a power of ten determined by the index of the array, starting from the last digit entered (10^0) to the first digit entered (10^n, n=number of digits in the array).

I was wondering if there was an alternate method that would be quicker or shorter to write. In particular, I wanted to know if using a logical bit shift might make this process shorter. Any ideas for optimizing or improving this code would be greatly appreciated!

Also, as a side note, I would like to call the gets and readInt subroutines using jal, but because gets and readInt both call subroutines, using jal in the main method to call gets or readInt causes problems. Any ideas how to get around this? Thanks again Cheers

PS: sorry for the formatting of the comments in this code, copy and pasting from MARS simulator into the stack overflow text box caused the alignment to be off :/

#IO
#Prompts user to input 10 ascii digits into an array
#Converts the string of digits into a single int
#Also handles any number of digits between 1 and 10 
#Returns 0 if non-digit chars are entered into the string

.data           #declaration of vars follows
array: .space 11    #reserves space for a 10 elem array
char: .space 2
prompt: .asciiz "Please enter 10 numbers, then press ENTER:  \n"
null: .asciiz ""
space: .ascii " "
newline: .asciiz "\n"
.text           #instructions follow

main:
la $a0, prompt      #load prompt message into $a0 for syscall
li $v0, 4               #load syscall to print string
syscall         #print prompt message
j readInt               #call readInt function to get user input string         

gets:           #read multiple chars from keyboard buffer until ENTER key,
                            #add NULL char and store into buffer pointed to by *array
                            #passed to the subroutine
la $s1, array       #set base address of array to s1
loop:           #start of read loop
jal getc        #jump to getc subroutine
lb $t0, char        #load the char from char buffer into t0, stripping null
sb $t0, 0($s1)      #store the char into the nth elem of array
lb $t1, newline     #load newline char into t1
beq $t0, $t1, done  #end of string?  jump to done
addi $s1, $s1, 1    #increments base address of array
j loop          #jump to start of read loop

getc:           #read char from keyboard buffer and return ascii value
li $v0, 8       #call code for read string
la $a0, char        #load address of char for read
li $a1, 2       #length of string is 1byte char and 1byte for null
syscall         #store the char byte from input buffer into char
jr $ra          #jump-register to calling function

readInt:        #read string of ascii digits, store into a local variable and  
                    #convert into integer, return that int unless string contains 
                    #non-integers 
j gets          #let s1 be top address of array, let s0 be the digitcounter
done:           #let s2 be the sum total
addi $s1, $s1, -1   #reposition array pointer to last char before newline char
la $s0, array       #set base address of array to s0 for use as counter
addi $s0, $s0, -1   #reposition base array to read leftmost char in string
add $s2, $zero, $zero   #initialize sum to 0
li $t0, 10      #set t0 to be 10, used for decimal conversion
li $t3, 1
lb $t1, 0($s1)      #load char from array into t1
blt $t1, 48, error  #check if char is not a digit (ascii<'0')
bgt $t1, 57, error  #check if char is not a digit (ascii>'9')
addi $t1, $t1, -48  #converts t1's ascii value to dec value
add $s2, $s2, $t1   #add dec value of t1 to sumtotal
addi $s1, $s1, -1   #decrement array address
lp:         #loop for all digits preceeding the LSB
mul $t3, $t3, $t0   #multiply power by 10
beq $s1, $s0, FIN   #exit if beginning of string is reached
lb $t1, ($s1)       #load char from array into t1
blt $t1, 48, error  #check if char is not a digit (ascii<'0')
bgt $t1, 57, error  #check if char is not a digit (ascii>'9')
addi $t1, $t1, -48  #converts t1's ascii value to dec value
mul $t1, $t1, $t3   #t1*10^(counter)
add $s2, $s2, $t1   #sumtotal=sumtotal+t1
addi $s1, $s1, -1   #decrement array address
j lp            #jump to start of loop

error:          #if non digit chars are entered, readInt returns 0
add $s2, $zero, $zero
j FIN

FIN:
li $v0, 1
add $a0, $s2, $zero
syscall 
li $v0, 10      #ends program
syscall

解决方案

mask the first four bits by anding the string with 0x0F like this below

andi $t0,$t0,0x0F # where $t0 contains the ascii digit .

now $t0 has the int of it.

这篇关于转换的ASCII数字字符串在MIPS /汇编成int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆