0

给出的具体说明:

在 base-32 中打印来自 Prgm1 的答案。不要使用 div/mul/rem 或类似的。尽可能只使用 t 寄存器。

笔记:

基数 32 我的意思是像十六进制(基数 16),但有 5 位组而不是 4 位,所以我们使用最多 V 的字母。首先考虑如何执行系统调用 1、35 或 34。

我能够完成这项工作的第一部分,但我不知道如何以 32 为基数或 5 个为一组获得我的输出。任何帮助将不胜感激。

.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
4

1 回答 1

1

以任何底数对数字进行字符串化的算法与以 10 为底的数字相同,只需将您的首选底数替换为 10。

该算法是用数字基础修改要打印的数字,并取一个数字。接下来将要打印的数字除以数字基数,然后重复直到数字降至零。(如果您需要前导零,请填写它们。)

不过,这种相对简单的方法会以相反的顺序生成数字字符串;因此,可以使用一个最大大小的缓冲区,用于基数中的 numer-as-string,并将数字放在缓冲区的末尾,依次向后(寻址向下)。生成的字符串以正确的前向顺序排列,可以用作常规字符串。

替代方法是(a)向后生成字符串(但在内存中向前)然后将其反转,并且,(b)将数字除以数字基数中的最大 1xxx 值(例如,1000000000 用于十进制/基数 10),取一个数字,然后减去该数字乘以 1000xxx 的工作值,并以下一个较小的 10 的形式重复。

基数 2(二进制)和基数 16(十六进制)只是同一算法的特例,它们利用了这些基数相对于处理器二进制能力的规律性。在这些形式中,由于每个数字的位数是偶数,因此可以直接按正序生成字符串数字,而无需除法、取模或乘法。

于 2019-10-03T04:50:55.643 回答