简体   繁体   中英

Using MIPS assembly language subset, how to output a number in base 32?

Specific instructions given:

Print the answer from Prgm1 in base-32. Do not use div/mul/rem or similar. Stick to using only the t-registers when you can.

Notes:

By base 32 I mean like hex (base 16) but with groups of 5 bits instead of 4 so we use letters up to V. Start by thinking about how you would do syscall 1, 35, or 34.

I was able to get the first part of this done but I have no idea how to get my output in base 32 or in groups of 5. Any help would be appreciated.

.data
prompt: .asciiz "Enter a number: "
prompt2: .asciiz "Enter another number: "
.text

# Prompt the user to enter number.
li $v0, 4
la $a0, prompt # Print prompt
syscall

# Get the user's number
li $v0, 5
syscall

# Store the result in $t2
move $s0, $v0 # Move users number from $v0 to $t2

# Prompt the user to enter number.
li $v0, 4
la $a0, prompt2 # Print prompt
syscall

# Get the user's number
li $v0, 5
syscall

# Store the result in $t3
move $s1, $v0 # Move users number from $v0 to $t3

# Store the result in $s0
li $s2, 0 
li $s3, 1 # Mask for extracting bit
li $t1, 0 # Counter

Loop:
# If $t1 equals 31, branch the number of instructions by the offset
beq $t1, 31, exit
and $t0, $s1, $s3 # ands $s1 and $s3 and stores in $t0
sll $s3, $s3, 1 # Multiplies value in $s3 by 2^1 and stores in $s3

# If $t0 equals 0, branch the number of instructions by the offset
beq $t0, 0, Loop2 
add $s2, $s2, $s0 # Stores the sum of $s0 and $s2 in $s2

Loop2:
# Multiplies value in $s0 by 2^1 and stores in $s0
sll $s0, $s0, 1 
addi $t1, $t1, 1 #adds 1 to $t1 and stores in $t1
j Loop



exit:
# Print or show the number
li $v0, 1
add $a0, $s2, $zero # Move the number to the argument
syscall

#Exit
li $v0, 10
syscall

The algorithm for string'ifying a number in any base is the same as for base 10, just substitute your preferred base for 10.

The algorithm is to mod the number-to-print with the number-base, and take a digit. Next divide the number-to-print by the number-base, and repeat until the number is down to zero. (If you need leading zeros, the fill them in.)

This relatively simple approach generates the digit string in reverse order, though; so, a buffer of maximum size for the nubmer-as-string in the base can be used, and the digits placed at the end of the buffer, going successively backwards (down in addressing). The resulting string goes is in proper forwards order and can be used as a regular string.

Alternatives are (a) to generate the string backwards (yet forwards in memory) and then reverse it, and, (b) to divide the number by the maximum 1xxx value in the number base (eg 1000000000 for decimal/base 10), take a digit, then subtract that digit times the working 1000xxx value, and repeat with the next smaller 10's form.

Base 2 (binary) and base 16 (hex) are just specializations of that same algorithm that take advantage of the regularity of those bases relative to the binary capabilities of the processors. In these forms, due to the even number of bits per digit, the strings digits can be directly generated in forwards order, without division, mod, or multiplication.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM