在MIPS中将十六进制字符转换为十进制等效 [英] Convert hex character to decimal equivalent in MIPS
本文介绍了在MIPS中将十六进制字符转换为十进制等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取单个ASCII字符并将其转换为MIP中的十进制等值线?
我是否必须具有某些条件才能从中减去一定量ascii代码使它成为十进制表示形式?
解决方案
这是Pax写的一个简单实现(它假定十六进制数字 - A到F总是大写)
文件hextodec.c
#include< stdio.h>
/ *
*将ASCII字符转换为其十进制等值。
*错误时返回-1。
*
* /
extern int hextodec(char * c);
int main(int argc,char ** argv){
int i = 0;
char digits [] = {'0','1','2','3','4','5','6','7','8','9', 'A', 'B', 'C', 'd', 'F'}; (; i <16; i ++){
printf(%c\t%d\\\
,digits [i],hextodec(digits + i))的
;
}
返回0;
文件hextodec.S
#include< mips / regdef.h>
$ b $ * * int hextodec(char * c)
*在寄存器a0中设置第一个(也是唯一的)参数。
*返回值在寄存器v0中设置。
*函数调用约定被忽略。
* /
.text
.globl hextodec
.align 2
.ent hextodec
hextodec:
lbu t0,0(a0)#从参数中加载字节
li t1,0X30
li t2,0x39
andi t1,t1, 0x000000ff#用于比较的单词。
andi t2,t2,0x000000ff
bltu t0,t1,ERROR #error if low than 0x30
bgt t0,t2,dohex #if大于0x39,测试A - F
addiu t0,t0,-0x30 #OK,48到55之间的字符。减去48.
b返回
dohex:li t1,0x41
li t2,0x46
andi t1,t1,0x000000ff#用于比较的单词。
andi t2,t2,0x000000ff
/ *字节在65到70之间?* /
bltu t0,t1,ERROR #error if low than 0x41
bgt t0,t2,如果大于0x46,错误#error
ishex:addiu t0,t0,-0x37 #subtract 55 from hex char('A'-'F')
b返回
错误:addiu t0,零,-1#返回-1。
return:move v0,t0 #move return value to register v0
jr ra
.end hextodec
测试运行
root @:〜/ stackoverflow#./hextodec
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
A 10
B 11
C 12
D 13
E 14
F 15
root @:〜/ stackoverflow#
How do I take a single ASCII character and convert it to its decimal equivelant in MIPs?
Do I simply have to have some conditions to subtract a certain amount from the ascii code to make it its decimal representation?
解决方案
Here's a simplistic implementation of what Pax wrote (it assumes that hexadecimal digits - A to F are always upper case)
File hextodec.c
#include <stdio.h>
/*
*Converts an ASCII char to its decimal equivalent.
*Returns -1 on error.
*
*/
extern int hextodec(char* c);
int main(int argc,char **argv){
int i=0;
char digits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','F'};
for (;i<16;i++){
printf("%c\t%d\n",digits[i],hextodec(digits+i));
}
return 0;
}
File hextodec.S
#include <mips/regdef.h>
/* int hextodec(char* c)
* first (and only) argument is set in register a0.
* return value is set in register v0.
* function calling convention is ignored.
*/
.text
.globl hextodec
.align 2
.ent hextodec
hextodec:
lbu t0,0(a0) #load byte from argument
li t1,0X30
li t2,0x39
andi t1,t1,0x000000ff #Cast to word for comparison.
andi t2,t2,0x000000ff
bltu t0,t1,ERROR #error if lower than 0x30
bgt t0,t2,dohex #if greater than 0x39, test for A -F
addiu t0,t0,-0x30 #OK, char between 48 and 55. Subtract 48.
b return
dohex: li t1,0x41
li t2,0x46
andi t1,t1,0x000000ff #Cast to word for comparison.
andi t2,t2,0x000000ff
/*is byte is between 65 and 70?*/
bltu t0,t1,ERROR #error if lower than 0x41
bgt t0,t2,ERROR #error if greater than 0x46
ishex: addiu t0,t0,-0x37 #subtract 55 from hex char ('A'- 'F')
b return
ERROR: addiu t0,zero,-1 #return -1.
return: move v0,t0 #move return value to register v0
jr ra
.end hextodec
test run
root@:~/stackoverflow# ./hextodec
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
A 10
B 11
C 12
D 13
E 14
F 15
root@:~/stackoverflow#
这篇关于在MIPS中将十六进制字符转换为十进制等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文