使用MIPS汇编语言子集,如何输出以32为基数的数字?



给出的具体说明:

以 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

在任何基数中字符串化数字的算法与基数 10 相同,只需将您喜欢的基数替换为 10。

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

但是,这种相对简单的方法以相反的顺序生成数字字符串;因此,可以使用基数中nubmer-as-string的最大大小的缓冲区,并且放置在缓冲区末尾的数字依次向后(寻址向下(。  生成的字符串 go 按正确的正向顺序排列,可以用作常规字符串。

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

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

最新更新