如何从4位十六进制转换为7位ASCII? [英] How to convert from 4-bit hexadecimal to 7-bit ASCII?
问题描述
的赋值是通过写一个子程序,可4位十六进制和7位ASCII之间的转换学习汇编编程。起初,我不知道,但经过一番研究,我能和努力,绘制流程图,使一个程序,但它是不完全正确的,所以我要求你的指导,帮助我解决这个问题。
实际分配的文字是这样的:
HA 3.1。绘制的流程图用于子程序转换的4位
十六进制值到相应的7位ASCII- code。查看完整
规范下面hexasc。例如:二进制0010(十六进制
数字2)转化成011 0010(ASCII- code代表'2')。另一个
例如:二进制1011(十六进制数字B)转换为100 0010
(ASCII- code为B),请确保您的子程序被记录
根据我们的要求。
HA 3.2。在Nios II IDE使用labwork项目,创建一个新的
文件调用hexasc.s
HA 3.3。在文件hexasc.s,编写一个名为hexasc子程序,即
一个4位十六进制值转换为相应的7位
ASCII- code。
块引用>我画了程序的流程图:
和我已经试过程序是这样的,但我怀疑这是不是根据规范:
。全球主要 。文本
.align伪2主:MOVI R8,为0x09
MOVI R9,为0x0F 安迪R4,R4,为0x0F BGT R8,R4,L1 MOVI R2,0X1E
ADD R2,R2,R4
ANDI R2,R2,0xff的 MOVIA R2,的putchar
BR L2L1:MOVI R2,0x29
ADD R2,R2,R4
ANDI R2,R2,0xff的 MOVIA R2,的putcharL2:.END你能帮助我发展和解决这个任务?有充足的时间,这是不是由于直到一个月。
更新
在这里看到的评论说的流程是不正确的,我做了必要的调整:
我也想讨论这个算法是如何正确的十六进制和ASCII之间进行转换。
更新/修改
下面是完整的程序。
。全球hexasc 。文本
.align伪2hexasc:MOVI R8,为0x09
MOVI R9,为0x0F
ANDI R4,R4,为0x0F #keep只有4位
BGT R4,R8,L1 #IS X - GT; 9?
MOVI R2,的0x30
ADD R2,R2,R4
ANDI R2,R2,0xff的
RETL1:MOVI R2,0x37符号
ADD R2,R2,R4
ANDI R2,R2,0xff的
RET
解决方案当你屏蔽掉的低四位,你有与值0x0到0xF的结束了的潜力。理想的结果的表是:
为0x0 - > 0=的0x30
为0x1 - > '1'= 0X31
0X2 - > '2'= 0x32
0x3中 - > '3'= 0x33
为0x4 - > '4'= 0x34
0x5的 - > '5'= 0x35
为0x6 - > '6'= 0x36数据
0x7的 - > '7'= 0x37符号
0x8中 - > '8'= 0x38
0x9 - > '9'= 0x39
是0xA - > 'A'= 0×41
0XB - > 'B'=的0x42
位于0xC - > 'C'= 0x43中
为0xD - > 'D'= 0x44进行
0xE - > 'E'=×45
0xF的 - > 'F'= 0×46从预期的效果该表我们可以看到,有两个线性部分,从0x0到0x9和为0xA到0xF的。对于0x0到0x9情况下的0x30 - 为0x0 = 0X30所以我们添加的0x30。对于以是0xA段0xF的的0x41 - 为0xA = 0x37符号
请问这项工作?
为0x0 +的0x30 =的0x30
为0x1 +的0x30 = 0X31
0X2 +的0x30 = 0x32
0x3中+的0x30 = 0x33
为0x4 +的0x30 = 0x34
为0x5 +的0x30 = 0x35
为0x6 +的0x30 = 0x36数据
为0x7 +的0x30 = 0x37符号
地址0x8 +的0x30 = 0x38
0x9 +的0x30 = 0x39是0xA + 0x37符号= 0×41
0XB + 0x37符号=的0x42
位于0xC + 0x37符号= 0x43中
为0xD + 0x37符号= 0x44进行
0xE + 0x37符号=×45
0xF的+ 0x37符号= 0×46看起来不错。
一个稍微不同的方式始终是添加的0x30然后调整后。
为0x0 +的0x30 =的0x30
为0x1 +的0x30 = 0X31
0X2 +的0x30 = 0x32
0x3中+的0x30 = 0x33
为0x4 +的0x30 = 0x34
为0x5 +的0x30 = 0x35
为0x6 +的0x30 = 0x36数据
为0x7 +的0x30 = 0x37符号
地址0x8 +的0x30 = 0x38
0x9 +的0x30 = 0x39
是0xA +的0x30 + 7 = 0×41
0XB +的0x30 + 7 =的0x42
位于0xC +的0x30 + 7 = 0x43中
为0xD +的0x30 + 7 = 0x44进行
0xE +的0x30 + 7 =×45
0xF的+的0x30 + 7 = 0×46在创建所需的结果表,左边你应该知道的东西与0xF的相与为您提供了0x0到0xF的,似乎你没有。所需表的右侧来自一个ASCII图表。我认为,如果你做了该图表,并得到了一个计算器(与老人们使用的按钮是啊,那个小东西,虽然一个没有十六进制,你的手机应该有它的一个应用程序)。从该表有视觉上拿出的算法。
您也应该问问自己,如果我想A到F是小写而不是大写字母(A,B,C,D,E,F)是什么?如何更改算法?
The assigment is to learn assembly programming by writing a subroutine that can convert between 4-bit hexadecimal and 7-bit ASCII. At first I had no idea but after some research I could make and effort and draw a flowchart and make a program but it is not entirely correct so I'm asking for your guidance to help me solve this.
The actual assignment text is this:
HA 3.1. Draw a flow-chart for a subroutine to convert a 4-bit hexadecimal value to the corresponding 7-bit ASCII-code. See the full specification for hexasc below. Example: binary 0010 (hexadecimal digit 2) is converted to 011 0010 (ASCII-code for '2'). Another example: binary 1011 (hexadecimal digit B) is converted to 100 0010 (ASCII-code for 'B') Make sure that your subroutine is documented according to our requirements.
HA 3.2. Using the labwork project in the Nios II IDE, create a new file called hexasc.s
HA 3.3. In the file hexasc.s, write a subroutine called hexasc, that converts a 4-bit hexadecimal value to the corresponding 7-bit ASCII-code.
I've drawn a flowchart for the program:
And the program I've tried is this but I suspect that it is not according to spec:
.global main .text .align 2 main: movi r8, 0x09 movi r9, 0x0f andi r4, r4, 0x0f bgt r8, r4, L1 movi r2, 0x1e add r2, r2, r4 andi r2, r2, 0xff movia r2,putchar br L2 L1: movi r2, 0x29 add r2, r2, r4 andi r2, r2, 0xff movia r2,putchar L2: .end
Can you help me develop and solve this assignment? There's plenty of time, it's not due until in a month.
Update
After seeing here in a comment that the flowchart was incorrect, I've made the necessary adjustments:
I also want to discuss how this algorithm is correct that converts between hexadecimal and ASCII.
Update / edit
Here is the complete program.
.global hexasc .text .align 2 hexasc: movi r8, 0x09 movi r9, 0x0f andi r4, r4, 0x0f #keep only 4 bits bgt r4, r8, L1 #is x>9? movi r2, 0x30 add r2, r2, r4 andi r2, r2, 0xff ret L1: movi r2, 0x37 add r2, r2, r4 andi r2, r2, 0xff ret
解决方案When you mask off the lower four bits, you have the potential for ending up with the values 0x0 to 0xF. The table of desired results is:
0x0 -> '0' = 0x30 0x1 -> '1' = 0x31 0x2 -> '2' = 0x32 0x3 -> '3' = 0x33 0x4 -> '4' = 0x34 0x5 -> '5' = 0x35 0x6 -> '6' = 0x36 0x7 -> '7' = 0x37 0x8 -> '8' = 0x38 0x9 -> '9' = 0x39 0xA -> 'A' = 0x41 0xB -> 'B' = 0x42 0xC -> 'C' = 0x43 0xD -> 'D' = 0x44 0xE -> 'E' = 0x45 0xF -> 'F' = 0x46
From that table of desired results we can see that there are two linear sections, from 0x0 to 0x9 and from 0xA to 0xF. For the 0x0 to 0x9 case 0x30 - 0x0 = 0x30 so we add 0x30. For the 0xA to 0xF section 0x41 - 0xA = 0x37.
Will that work?
0x0 + 0x30 = 0x30 0x1 + 0x30 = 0x31 0x2 + 0x30 = 0x32 0x3 + 0x30 = 0x33 0x4 + 0x30 = 0x34 0x5 + 0x30 = 0x35 0x6 + 0x30 = 0x36 0x7 + 0x30 = 0x37 0x8 + 0x30 = 0x38 0x9 + 0x30 = 0x39 0xA + 0x37 = 0x41 0xB + 0x37 = 0x42 0xC + 0x37 = 0x43 0xD + 0x37 = 0x44 0xE + 0x37 = 0x45 0xF + 0x37 = 0x46
Looks good.
A slightly different way is always add 0x30 then adjust after.
0x0 + 0x30 = 0x30 0x1 + 0x30 = 0x31 0x2 + 0x30 = 0x32 0x3 + 0x30 = 0x33 0x4 + 0x30 = 0x34 0x5 + 0x30 = 0x35 0x6 + 0x30 = 0x36 0x7 + 0x30 = 0x37 0x8 + 0x30 = 0x38 0x9 + 0x30 = 0x39 0xA + 0x30 + 7 = 0x41 0xB + 0x30 + 7 = 0x42 0xC + 0x30 + 7 = 0x43 0xD + 0x30 + 7 = 0x44 0xE + 0x30 + 7 = 0x45 0xF + 0x30 + 7 = 0x46
When creating the desired result table, the left side you should have known something anded with 0xF gives you 0x0 to 0xF, and it appears you did. The right side of the desired table comes from an ASCII chart. I think if you had made that chart and gotten out a calculator (yea that little thing with buttons that old people use, although one that does hex, your phone should have an app for it). From there visually from that table come up with the algorithm.
You should also ask yourself, what if I want A to F to be lower case instead of upper case (a,b,c,d,e,f)? How do I change the algorithm?
这篇关于如何从4位十六进制转换为7位ASCII?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!