簡體   English   中英

使用 MIPS 匯編語言子集,如何 output 以 32 為底數?

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

給出的具體說明:

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

筆記:

基數 32 我的意思是像十六進制(基數 16),但有 5 位組而不是 4 位,所以我們使用最多 V 的字母。首先考慮如何執行系統調用 1、35 或 34。

我能夠完成這項工作的第一部分,但我不知道如何讓我的 output 以 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。

該算法是用數字基礎修改要打印的數字,並取一個數字。 接下來將要打印的數字除以數字基數,然后重復直到數字降至零。 (如果您需要前導零,請填寫它們。)

不過,這種相對簡單的方法會以相反的順序生成數字字符串; 因此,可以使用一個最大大小的緩沖區,用於基數中的 numer-as-string,並將數字放在緩沖區的末尾,依次向后(尋址向下)。 生成的字符串以正確的前向順序排列,可以用作常規字符串。

替代方法是(a)向后生成字符串(但在內存中向前)然后將其反轉,並且,(b)將數字除以數字基數中的最大 1xxx 值(例如,1000000000 用於十進制/基數 10),取一個數字,然后減去該數字乘以 1000xxx 的工作值,並以下一個較小的 10 的形式重復。

基數 2(二進制)和基數 16(十六進制)只是同一算法的特例,它們利用了這些基數相對於處理器二進制能力的規律性。 在這些 forms 中,由於每個數字的位數為偶數,因此可以直接按正序生成字符串數字,無需除法、取模或乘法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM