MIPS十进制到二进制的转换代码有效,但必须将结果反转怎么办? [英] MIPS decimal to binary conversion code is working but result must be reversed how?

查看:258
本文介绍了MIPS十进制到二进制的转换代码有效,但必须将结果反转怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mips中有一个项目,它大约是十进制到二进制的转换,我设法编写了代码,并且工作正常,但是众所周知,当我们在纸上进行十进制到二进制的转换时,我们将结果写成反向,这就是关键点因为我的程序以正常顺序写入1和0,并且因为它只是在屏幕上打印,所以我无法给出命令将其反转.

I have a project in mips and its about decimal to binary conversion I managed to write the code and its working fine but as you all know when we make decimal to binary conversion on paper we write result in reverse and thats the key point because my program writes 1 and 0 in normal order and because that it just prints on screen I cant give a command to reverse it.

如果我将变量存储在数组中并反转或使用堆栈,我该如何反转呢?我的知识非常低(如从代码中可以看到的那样),我坚持使用它.请帮帮我.

How can I reverse it should I store varibles in array and reverse it or use stacks please my knowlage is very low(as you can see from the code) and I am stuck with it.Help me please.

.data
ms1_msg:.asciiz "Enter The Number:"
.text
.globl main
main:

la $a0,ms1_msg    
li $v0,4
syscall

li $v0,5          #user enters number and it is stored in t0
syscall
move $t0,$v0


addi $t1,$zero,1  #t1=1
addi $t2,$zero,2  #t2=2
add $t5,$zero,1   #t5=1
add $t8,$zero,$zero 

add $t6,$zero,$t0  #t6=1

loop1:            #trying to find the counter for loop 2

addi $t5,$t5,1    
div $t0,$t2       
mflo $t4          
beq $t4,$t1,loop2 
sub $t0,$t0,$t0   
add $t0,$t4,$t0
j loop1           

s

loop2:            #twith using the counter (t5) I define how many times loop should circle. 

    addi $t9,$t9,1    
    div $t6,$t2       
    mfhi $t7          
    mflo $t8          
    move $a0, $t7     
    li $v0, 1
    syscall
    beq $t9,$t5,exit
    sub $t6,$t6,$t6   
    add $t6,$t8,$t6 
    j loop2           

    exit:                                      
    li $v0,10       
    syscall

推荐答案

您的标题为从二进制到十进制",但是您的描述和代码暗含从十进制到二进制",因此我假设后者为准.

Your title says "binary to decimal", but your description and code implies "decimal to binary", so I'll assume the latter.

不需要缓冲区将数字显示为二进制字符串.只需移动和遮罩即可,您可以按照想要的任何顺序进行操作.

There's no need for a buffer to display a number as a binary string. It's just a matter of shifting and masking, which you can do in any order you want.

例如,以下代码将以其二进制表示形式打印一个值,并省略前导零:

For example, the following code would print a value in its binary representation, with the leading zeroes omitted:

uint32_t value = 1234;
bool foundAOne = false;
int bit = 31;

printf("%d in binary is ", value);

do {
    // Check the most significant bit (i.e. the "leftmost" bit)
    if (value & 0x80000000) {  
        foundAOne = true;        
        printf("1");
    } else {
        if (foundAOne || bit == 0) {
            printf("0");
        }
    }
    bit--;
    // Shift out the current msb and move all other bits one step
    // to the left
    value <<= 1;
} while (bit >= 0);

printf("\n");

将其转换为MIPS程序集应该是一个相当简单的任务.

Converting this into MIPS assembly should be a fairly simple task.

这篇关于MIPS十进制到二进制的转换代码有效,但必须将结果反转怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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