2014-03-26 227 views
0

以交互方式讀取表示32位帶符號二進制補碼二進制數字(S2CB)的字符串值,該數字可以是十進制數字表示爲32位的S2CB號碼或8位帶符號的十六進制數字。您的程序必須在所有三個數字中顯示指定的值。MIPS轉換32位帶符號2s-com數字,十進制數字或8位帶符號十六進制

到目前爲止程序要求用戶輸入數字,但我在創建實際轉換算法時遇到問題。到目前爲止,程序要求輸入並確定它是否是正確的樣式。誰能幫幫我嗎?提前致謝。

.data 

    inputString: .asciiz "         " 
    testString: .asciiz "Enter a number as decimal (12345), binary (%10101), or hex (0xABCDEF): " 

    decDigits: .asciiz "" 
    hexDigits: .asciiz "ABCDEF" 
    binDigits: .asciiz "000000010010001101000110011110001001101010111100110111101111" 

    detectedDec: .asciiz "Decimal input detected" 
    detectedHex: .asciiz "Hexadecimal input detected" 
    detectedBin: .asciiz "Binary input detected" 
    detectedBad: .asciiz "Invalid input detected" 

    .text 
begin: li $v0, 4 
    la $a0, testString 
    syscall 

read: li $v0, 8 
    la $a0, inputString 
    li $a1, 33 
    syscall 

    jal isDec 

    beq $v0, 0, n1 
    li $v0, 4 
    la $a0, detectedDec 
    syscall 
    j endPgm 

n1: jal isHex 

    beq $v0, 0, n2 
    li $v0, 4 
    la $a0, detectedHex 
    syscall 
    j endPgm 

n2: jal isBin 

    beq $v0, 0, bad 
    li $v0, 4 
    la $a0, detectedBin 
    syscall 
    j endPgm 

bad: li $v0, 4 
    la $a0, detectedBad 
    syscall 
    j endPgm 

###### Is Decimal? Returns 1 in $v0 if yes 

isDec: li $t0, 0 
    li $t2, 0 

decLoop:lb $t1, inputString($t0) 
    beq $t1, 0, decDone 
    beq $t1, 10, decDone 
    bgt $t1, 57, notDec 
    blt $t1, 48, notDec 
    li $t2, 1 
    addi $t0, $t0, 1 
    j decLoop 

notDec: li $v0, 0 
    jr $ra 

decDone:beq $t2, 0, notDec 
    li $v0, 1 
    jr $ra 

###### 

###### Is Binary? Returns 1 in $v0 if yes 

isBin: li $t0, 1 
    li $t2, 0 

hasPer: lb $t1, inputString 
    bne $t1, 37, notBin 

binLoop:lb $t1, inputString($t0) 
    beq $t1, 0, binDone 
    beq $t1, 10, binDone 
    bgt $t1, 49, notBin 
    blt $t1, 48, notBin 
    li $t2, 1 
    addi $t0, $t0, 1 
    j binLoop 

notBin: li $v0, 0 
    jr $ra 

binDone:beq $t2, 0, notDec 
    li $v0, 1 
    jr $ra 

###### 

###### Is Hex? Returns 1 in $v0 if yes 

isHex: li $t0, 2 
    li $t2, 0 

has0x: lb $t1, inputString 
    bne $t1, 48, notHex 
    lb $t1, inputString + 1 
    bne $t1, 120, notHex 

hexLoop:lb $t1, inputString($t0) 
    beq $t1, 0, hexDone 
    beq $t1, 10, hexDone 
    bgt $t1, 57, testAF 
    blt $t1, 48, testAF 
    li $t2, 1 
    addi $t0, $t0, 1 
    j hexLoop 

testAF: bgt $t1, 70, notHex 
    blt $t1, 65, notHex 
    li $t2, 1 
    addi $t0, $t0, 1 
    j hexLoop 

notHex: li $v0, 0 
    jr $ra 

hexDone:beq $t2, 0, notDec 
    li $v0, 1 
    jr $ra 

###### 

endPgm: li $v0, 10 
    syscall 
enter code here 
+0

格式化熄滅: – user3465875

回答

0

一個給定基的字符串轉換算法實際上是非常簡單的。

假設鹼是b,在那裏它可能是21016考慮以下僞代碼:

num := 0 

while c <- input_char: 
    num := num * b 
    num := num + char_to_int(c) 

return num 
相關問題