2

我有一个程序,它将所有小写字母大写,并将用户在字符串中输入的所有大写字母小写。它通过在字符值中加上或减去 32 来获得所需的字符。我的问题是它不会改变字符串中的任何内容。关于改变什么的任何建议?

.data
prompt: .asciiz "\n\nEnter an string of characters: "
result: .asciiz "\n\nHere is the string you entered: "
after_sort: .asciiz "\n\nHere is the string after the case sorting: "
buffer: .space 80
.text

main:

#Prints the prompt string
li $v0, 4
la $a0, prompt 
syscall 

#reads string from user and saves in $a0
li $v0, 8
la $a0, buffer
li $a1, 80
syscall

#Prints the result string
li $v0, 4 
la $a0, result 
syscall

#Prints the string entered by the user
la $a0, buffer 
li $v0, 4
syscall


li $t0, 0 # t0 = i = 0
for_loop:
slti $t1, $t0, 80 # t1 = 1 if and only if t0 < 80
beq $t1, $0, for_loop_done

slti $t2, $a0, 91
li $t3, 1
beq $t2, $t3, upper #if the character value is less than 91 branch to upper addition
bne $t2, $t3, lower

upper:
addi $a0, $a0, 32 #adds 32 to the character value to lowercase it

lower:
subi $a0, $a0, 32 #subtracts 32 from the character value to capitalize it

addi $t0, $t0, 1

j for_loop
for_loop_done:

#Prints the result string
li $v0, 4 
la $a0, after_sort 
syscall

#Prints the string entered by the user
la $a0, buffer 
li $v0, 4
syscall

exitProgram:    li $v0, 10  # system call to
    syscall         # terminate program
4

3 回答 3

0

您正在$a0用作角色,例如这里:

slti $t2, $a0, 91

但它永远不会充满角色。目前,它包含一个内存地址,而不是一个字符。

您应该使用 加载字符lb并在使用sb.

如果您需要代码示例,请随时添加评论。

编辑:代码相关部分的更改:

...
li $t0, 0 # t0 = i = 0
for_loop:
slti $t1, $t0, 80 # t1 = 1 if and only if t0 < 80
beq $t1, $0, for_loop_done

lb $t4, 0($a0)
beqz $t4, for_loop_done
beq $t4, 10, for_loop_done
slti $t2, $t4, 91
li $t3, 1
beq $t2, $t3, upper #if the character value is less than 91 branch to upper addition
bne $t2, $t3, lower

upper:
addi $t4, $t4, 32 #adds 32 to the character value to lowercase it
j done

lower:
addi $t4, $t4, -32 #subtracts 32 from the character value to capitalize it
done:

addi $t0, $t0, 1

sb $t4, 0($a0)
addi $a0, $a0, 1

j for_loop
for_loop_done:

#Prints the result string
...
于 2013-10-13T23:19:05.923 回答
0

很容易忘记,在汇编中,你不能这样做:

if something
    do this
else
    do that

没有“其他”,只有后藤不寒而栗。

所以在这段代码中:

slti $t2, $a0, 91
li $t3, 1
beq $t2, $t3, upper #if the character value is less than 91 branch to upper addition
bne $t2, $t3, lower

upper:
addi $a0, $a0, 32 #adds 32 to the character value to lowercase it

lower:
subi $a0, $a0, 32 #subtracts 32 from the character value to capitalize it

addi $t0, $t0, 1

当您分支到 时upper,它会添加 32。然后它会减去 32,因为执行已进行到下一行。所以你的代码大写小写,但对大写没有任何作用。

您需要在if/then/else 等效项之后添加到第一条指令的跳转:

upper:
addi $a0, $a0, 32 #adds 32 to the character value to lowercase it
j done # No, I don't want to subtract it again!

lower:
subi $a0, $a0, 32 #subtracts 32 from the character value to capitalize it

done:
addi $t0, $t0, 1

事实上,您可能应该bne完全摆脱它 - 它是多余的。如果beq不分支,则不相等。所以这将是成品:

slti $t2, $a0, 91
li $t3, 1
beq $t2, $t3, upper #if the character value is less than 91 branch to upper addition

# Otherwise, it's lower
subi $a0, $a0, 32 #subtracts 32 from the character value to capitalize it
j done

upper:
addi $a0, $a0, 32 #adds 32 to the character value to lowercase it

done:
addi $t0, $t0, 1

希望有帮助!

(编辑:@Patrik 也是对的,您需要“取消引用”$a0。我的示例没有考虑到这一点。)

于 2013-10-16T21:36:52.840 回答
0

这是我的代码:它运行良好

.data 
theString:
.space 20
prompt: .asciiz "Enter a string of characters: "
.text
main:
li $v0, 4
la $a0, prompt 
syscall

li $v0, 8
la $a0, theString
li $a1, 20
syscall

li  $v0, 4
syscall
la $t1,theString
for:    lb $a0, 0($t1)
beqz $a0,out  #to find out end of string
beq $a0,10,out  #to find out end of string
slti $t2, $a0,91 #if $a0<91 $t2=1
beq $t2,1,small
beq $t2,0,capital
capital:
subu $a0, $a0, 32
li $v0,11
syscall
addi $t1,$t1,1
j for
small:
addi $a0, $a0, 32
li $v0,11
syscall
addi $t1,$t1,1
j for
out: 
li $v0, 10
syscall 
于 2013-10-28T18:34:09.540 回答