MIPS十进制到二进制转换代码是工作的,但结果必须反过来如何



我有一个mips项目,它是关于十进制到二进制转换的,我设法编写代码,它工作得很好,但正如你们所知道的,当我们在纸上进行十进制到二进制转换时,我们将结果反过来写,这是关键点,因为我的程序以正常顺序写1和0,因为它只是在屏幕上打印,我不能给出一个命令来反转它。

我该如何逆转它,我应该在数组中存储变量并反转它或使用堆栈,请我的知识是非常低的(正如你可以从代码中看到的),我坚持它。请帮帮我。

.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

你的标题是"二进制到十进制",但你的描述和代码暗示"十进制到二进制",所以我假设是后者。

不需要缓冲区将数字显示为二进制字符串。这只是一个移动和屏蔽的问题,你可以按照任何你想要的顺序来做。

例如,下面的代码将以二进制形式输出一个值,省略前导零:

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汇编应该是一个相当简单的任务。

最新更新