問題描述
顯示字符的 Ascii 值 (Displaying Ascii values of characters)
I am trying to make a program in which I can display ascii value of characters. The problem is it doesn't display the true ascii values. I am making this code in MIPS assembly which is very much similar to normal assembly language. Here is the code:
.data
User: .asciiz "Bush"
Line: .asciiz "\n"
Address: .word User
.text
main:
li $t0,1
li $t1,2
li $t2,3
li $t3,4
li $t4,5
lb $a0,User($0)
li $v0,1
syscall
la $a0,Line
li $v0,4
syscall
lb $a0,User($t0)
li $v0,1
syscall
la $a0,Line
li $v0,4
syscall
lb $a0,User($t1)
li $v0,1
syscall
la $a0,Line
li $v0,4
syscall
lb $a0,User($t2)
li $v0,1
syscall
la $a0,Line
li $v0,4
syscall
lb $a0,User($t3)
li $v0,1
syscall
la $a0,Line
li $v0,4
syscall
li $t1,-1
jal Length
j Exit
Length:
beq $a0,0,End
addi $t1,$t1,1
lb $a0,User($t1)
j Length
End:
move $a0,$t1
li $v0,1
syscall
jr $ra
Exit:
li $v0,10
syscall
What is the possible reason that It is not showing the true ascii values. Moreover When I try to make a program which finds the ascii values of all english alphabets, I get run time errors.It will be great deal to me if any one can help me with this.
Regards
參考解法
方法 1:
What are you expecting it to do and what does it currently do? Here's the output for me:
66
117
115
104
0
4
If you want the actual letters to show up, use vector 11 instead of 1 for the syscall
, which is the print character vector:
B
u
s
h
4
lb $a0,User($0)
li $v0,1
syscall
This loads one byte from the address of User
and puts it into $a0
. Then, it prints that value as an integer. The value at User
is the value of the ASCII letter 'B', which is 66, hence it prints 66. Is this not correct?
la $a0,Line
li $v0,4
syscall
This puts the address of Line
into $a0
, and then it prints the string stored at that address. In this case it's a newline string, so a newline is printed. This is correct.
lb $a0,User($t0)
li $v0,1
syscall
This is almost identical to the first block, except now we have an address offset of $t0
, which in this case is 1, meaning it loads the byte after the 'B' (the 'u'). The ASCII value of 'u' is 117 and that's what gets printed. Is this not correct?
This goes on for the first five characters of "Bush" (remember, the strings have been null-terminated with a 0-value), and the correct values seem to be printed, so you tell us what's wrong with it.
There's a bug with your Length
routine, in that the first character isn't from User
, but rather from Length
. Change the ordering as such:
addi $t1,$t1,1
lb $a0,User($t1)
beq $a0,0,End
j Length
As for the alphabet stuff, please make a separate question and provide sufficient detail of the problem: what have you tried, what does it do, what is it supposed to do, things like that.
方法 2:
$s1 is ascii value
Show the character from register $s1
li $v0, 11 #print character move $a0, $s1 syscall
example: $s1="0"=0x30=48 Result: 0 Show the ascii value from register $s1 to decimal
li $v0, 1
move $a0, $s1
syscall
example: $s1="0"=0x30=48 Result: 48
(by user1698102、Jeff、andreas karkotis)