首页 > 解决方案 > 剩下大多数 mips

问题描述

# 1 - Request a number
# 2 - Read the number
# 3 - Display Binary msg
# 4 - Display the number as binary
# 5 - Find the left-most bit (loop)
# 6 - Display the result msg
# 7 - Display the position


.data 
    prompt:    .asciiz "Enter a 16 bit number: "
    binaryMsg: .asciiz "\nBinary: "
    results:   .asciiz "\nThe left most bit is in position(zero based): "
        .text
    .globl main
    
    main:
    
        li $v0, 4
        la $a0, prompt
        syscall
        
        li $v0, 5
        syscall 
        move $a1, $v0
        
        li $v0, 4
        la $a0, binaryMsg
        syscall
        
        
        
        
       jal prtbin
    
        # New Line
        li $v0, 11
        li $a0, 10
        syscall
    
       jal main
        
        prtbin:
        add $t0, $zero, $a1 # put our input ($a1) into $t0
        add $t1, $zero, $zero # Zero out $t1
        addi $t3, $zero, 1 # load 1 as a mask
        sll $t3, $t3, 15 # move the mask to appropriate position
        addi $t4, $zero, 16 # loop counter
        
        loop:
        and $t1, $t0, $t3 # and the input with the mask
        beq $t1, $zero, print # Branch to print if its 0
    
        add $t1, $zero, $zero # Zero out $t1
        addi $t1, $zero, 1 # Put a 1 in $t1
        j print
        
        
        print:
           
        li $v0, 1
        move $a0, $t1
        syscall
        
        srl $t3, $t3, 1
        addi $t4, $t4, -1
        bne $t4, $zero, loop
    
 
        
        
        li $v0, 4
        la $a0, results
        syscall
        
        
        li $v0, 10
        syscall 

我正在构建一个程序,它需要一个 16 位数字将其转换为二进制并获取最左边的位并输出它的那个位置。我有两个问题,一个是我似乎无法在不循环回主程序的情况下使其工作,并且似乎无法获得最左边的位并将其输出到结果中。任何帮助都感激不尽。

更新我想出了如何从循环中获取它,但现在我似乎仍然无法找到如何获取最左边的位并显示它

标签: bit-manipulationmipsbit-shift

解决方案


推荐阅读