2

我是汇编语言的新手,我试图让用户输入一个介于 1 和 26 之间的整数以保持简单,然后打印与该整数关联的 ASCII 字符。但是,当它打印出整数时,它会打印一些看起来很奇怪的符号而不是字母、字符等。这是我的代码:

    .data 
prompt1:    .asciiz "Enter the value of n here: " 
prompt2:    .asciiz "The letter is: "
outside:    .asciiz "?"

.globl  main
.text
main: 
li $t1, 1   #register to check for 1
li $t2, 27  #register for no numbers over 26
li $v0, 4   #prompt user for integer
la $a0, prompt1
syscall
li $v0, 5   #store the integer the user inputed
syscall
add $t0, $0, $v0 #store that number in register
blt $t0, $t1, outOfBounds #if less than 1, print a ?
blt $t0, $t2, print #if okay, go to print the ascii character
j   outOfBounds 
print:
li $v0, 11
move $a0, $t0
syscall
j   Exit
outOfBounds:
li $v0, 4
la $a0, outside
syscall
j   Exit
Exit:
li $v0, 10
syscall     
4

3 回答 3

2

替换这一行:

move $a0, $t0

有了这个:

addiu $a0, $t0, 'A'-1  # Converts the value in the range 1-26 to a character
                       # in the range 'A'-'Z'
于 2013-09-17T09:41:49.813 回答
0
1     .data 
2   prompt1:    .asciiz "Enter the value of n here: " 
3   prompt2:    .asciiz "The letter is: "
4   outside:    .asciiz "?"
5
6   .globl  main
7   .text
8   main: 
9   li $t1, 1               # register to check for 1
10  li $t2, 27              # register for no numbers over 26
11  li $v0, 4               # prompt user for integer
12  la $a0, prompt1
13  syscall
14  li $v0, 5               # store the integer the user inputed
15  syscall
16  add $t0, $0, $v0                  # store that number in register
17  blt $t0, $t1, outOfBounds             # if less than 1, print a ?
18  blt $t0, $t2, print                   # if okay, go to print the ascii character
19  j   outOfBounds 
20  print:
21  li $v0, 11
22  move $a0, $t0
23  syscall
24  j   Exit
25  outOfBounds:
26  li $v0, 4
27  la $a0, outside
28  syscall
29  j   Exit
30  Exit:
31  li $v0, 10
32  syscall     
于 2013-09-17T04:57:44.973 回答
0

可打印的 ASCII 字符从 32 开始,到 126 左右结束,因此将输入限制在这个范围内会产生合理的输出。

于 2013-09-17T04:30:34.337 回答