MIPS编程:存储和整数以及打印其ASCII字符

时间:2013-09-17 04:00:29

标签: assembly integer character ascii mips

我是汇编语言的新手,我试图让用户输入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     

3 个答案:

答案 0 :(得分: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'

答案 1 :(得分:0)

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

答案 2 :(得分: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