1

I'm writing a program that reads 4 hexadecimals digits, that represent an unsigned integer number, then it condensates those digits in $t1 and finally it calculates and displays the decimal number.

I have completely grasped the solution in theory,but I have trouble using mips as this is my first program. Currently, I have trouble storing the bytes of the string. Here's my code thus far:

.data 
msg1:   .asciiz "Enter the hexadecimal :    "
newline:   .asciiz  "\n"
.text

main:
#Print string msg1
li $v0 ,4   # print_string syscall code = 4
la $a0,msg1 #load the adress of msg
syscall

# Get input A from user and save
li  $v0,8       # read_string syscall code = 8
syscall 
move    $t0,$v0     # syscall results returned in $v0

li $v0,10
syscall

I know that I'll use lb Rdest, address at some point. But,won't I have to read each digit of the string one by one if that's the case?

4

1 回答 1

1
.data
   msg1:   .asciiz "Enter the hexadecimal :    "
   buffer: .space 10
.text

main:
    #Print string msg1
    li $v0 ,4   # print_string syscall code = 4
    la $a0,msg1 #load the adress of msg
    syscall

    # Get input A from user and save
    la $a0, buffer  # address
    li $a1, 10      # length
    li  $v0,8       # read_string syscall code = 8
    syscall

    li $t0, 0                # result
loop:
    lb $t1, ($a0)            # fetch char
    beq $t1, $0, done        # zero terminator?
    addiu $t1, $t1, -10      # line feed?
    beq $t1, $0, done
    addiu $t1, $t1, -38      # convert digit
    sltiu $t2, $t1, 10       # was it a digit?
    bne $t2, $0, append      # yes
    # add validation and upper case here as needed
    addiu $t1, $t1, -39      # convert lower case letter
append:
    sll $t0, $t0, 4          # make room
    or $t0, $t0, $t1         # append new digit
    addiu $a0, $a0, 1        # next char
    j loop

done:
    move $a0, $t0            # print result
    li $v0, 1
    syscall

    li $v0,10
    syscall
于 2015-11-17T13:25:27.607 回答