MIPS大会:转换整数为十六进制 [英] MIPS Assembly: Convert from Integer to Hexadecimal

查看:1352
本文介绍了MIPS大会:转换整数为十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个code片段,我相信转换为十六进制整数。不过,我不遵守它。我补充说,说什么,我相信正在发生的事情的意见,但我不知道为什么它被完成。因此,假设我正确地指出了每行的在做,为什么它正在做我的,也有人请解释一下吗?如如何以任何方式帮助转换为十六进制?

$ A0为整数值

$ a1为的,其中结果应该是地址

 阿迪$ T0,$ 0,48#设置$ T0等于48
        SB $ T0,0($ A1)在$ A1在位置0 #store $至(48)
        阿迪$ T0,$ 0 120 #SET $ T0等于120
        SB $ T0,1($ A1)#store在$ $ A1在位置1 T0(120)
        阿迪$ T1,$ A1,9#设置$ T1 =地址+ 9循环:        ANDI $ T0,$ A0,0xF的#$ T0 = 1,如果$ a0和0xF的相同(0xF的=开始十六进制的)?        的SLTⅠ$ t2时,$ t0时,10的#if $ t0为小于10,$ T2 = 1,否则为0
        BNE $ T2,$ 0,数字的#if $ T2不等于0,则分支到DIGIT
        阿迪$ T0,$ t0时,48#设置$ T0等于48
        阿迪$ T0,$ T0,39#设置$ T0等于39(为什么我们只写了48?)
数字:        SB $ T0,0($ T1)#SET $ T0等于不管在$ T1的位置0        SRL $ A0,$ A0,4 #shift右4位        BNE $ A0,$ 0,#如果LOOP $ A0不等于0,跳转到LOOP
        阿迪$ T1,T1 $,-1 #SET $ T1 = $ T1 - 1DONE:        JR $ RA #SET的寄存器跳转回$ RA
        NOP


解决方案

 的SLTⅠ$ T2,$ T0,10#如果$ T0小于10,$ T2 = 1,否则为0
    BNE $ T2,$ 0,数字的#if $ T2不等于0,则分支到DIGIT
    阿迪$ T0,$ t0时,48#设置$ T0等于48
    阿迪$ T0,$ T0,39#设置$ T0等于39(为什么我们只写了48?)

MIPS使用分支延迟槽,这意味着分支指令之后的指令分支是采取之前(或不采取)总是被执行。

那么,这说的是如果$ T0小于10(即范围0..9),转到数字,但首先添加48(ASCII'0'),而不管$ T0的价值。如果分支被带到你现在已经从0..9转换为'0'..'9'。如果该分支不采取,$ T0原是范围10..15,现在将在范围58..63,所以我们增加39个范围得到一个值97..102(ASCII码codeS为'A'..'F')。

I found this code snippet that I believe converts an integer to hex. However, I'm not following it at all. I added the comments that say what I believe is happening, but I have no idea WHY it's being done. So, assuming I correctly noted what each line was doing, can someone please explain to me why it's being done? As in how it in any way helps convert to hex?

$a0 is the integer value

$a1 is the address of where the result should be

        addi $t0, $0, 48       #set $t0 equal to 48 
        sb $t0, 0($a1)         #store $to (48) at location 0 in $a1
        addi $t0, $0, 120      #set $t0 equal to 120
        sb $t0, 1($a1)         #store $t0 (120) at location 1 in $a1
        addi $t1, $a1, 9       #set $t1 = the address + 9

LOOP:

        andi $t0, $a0, 0xf    #$t0 = 1 if $a0 and 0xf are the same (0xf = beginning of hex)?

        slti $t2, $t0, 10     #if $t0 is less than 10, $t2 = 1, else 0
        bne $t2, $0,  DIGIT   #if $t2 does not equal 0, branch to DIGIT
        addi $t0, $t0, 48     #set $t0 equal to 48
        addi $t0, $t0, 39     #set $t0 equal to 39 (why did we just write over the 48?)
DIGIT:

        sb $t0, 0($t1)        #set $t0 equal to whatever's in location 0 of $t1

        srl $a0, $a0, 4       #shift right 4 bits

        bne $a0, $0, LOOP     #if $a0 does not equal 0, branch to LOOP
        addi $t1, $t1, -1     #set $t1 = $t1 - 1

DONE:

        jr $ra                #set the jump register back to $ra
        nop

解决方案

    slti $t2, $t0, 10     #if $t0 is less than 10, $t2 = 1, else 0
    bne $t2, $0,  DIGIT   #if $t2 does not equal 0, branch to DIGIT
    addi $t0, $t0, 48     #set $t0 equal to 48
    addi $t0, $t0, 39     #set $t0 equal to 39 (why did we just write over the 48?)

MIPS uses branch delay slots, meaning that the instruction following the branch instruction always is executed before the branch is taken (or not taken).

So what this says is "If $t0 is less than 10 (i.e. in the range 0..9), goto DIGIT, but first add 48 (ASCII '0') regardless of the value of $t0. In case the branch was taken you'll now have converted from 0..9 to '0'..'9'. In case the branch wasn't taken, $t0 was originally in the range 10..15 and will now be in the range 58..63, so we add 39 more to get a value in the range 97..102 (the ASCII codes for 'a'..'f')".

这篇关于MIPS大会:转换整数为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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