如何使用汇编语言一次从用户处获取一个字符?
我已经为此工作了几个小时,但我无法弄清楚。我应该让用户输入一个字符串和两个字符,然后程序应该用第二个字符替换第一个字符的任何实例。
示例
输入一个字符串:String
输入一个字符:t
输入另一个字符:p
您的新 字符单词是: Spring
到目前为止,这是我的代码(用汇编语言):(
.data
userStr: .space 50
ch1: .space 1
ch2: .space 1
str: .asciiz "Please enter a string: "
char1: .asciiz "\nEnter a character: "
char2: .asciiz "\nEnter a replacement character: "
result1: .asciiz "\nOriginal String: "
result2: .asciiz "\nResult String: "
result3: .asciiz " Substitute "
result4: .asciiz " --> "
tester: .asciiz "\nCharacter is: "
tester2: .asciiz "\nCharacter 2 is: "
.text
.globl main
main:
la $a0, str # Prompt to enter a string
li $v0, 4
syscall
la $a0, userStr # input string is stored in 'userStr'
li $v0, 8
syscall # Calls the operating system
li $v0, 4
la $a0, char1 # Prints prompt to enter a character
syscall
la $a0, ch1 # Stores character as ch1
li $v0, 8
syscall # Calls the operating system
li $v0, 4
la $a0, char2 # Prints prompt to enter a character
syscall
la $a0, ch1 # Stores second character as ch2
li $v0, 8
syscall
la $a0, tester # print "Character is: "
li $v0, 4
syscall
la $a0,ch1 # print <character>
li $v0, 4
syscall
la $a0, tester2 # print "Character is: "
li $v0, 4
syscall
la $a0,ch2 # print <character>
li $v0, 4
syscall
li $t1,0 # $t1 is the index of the original string
li $t2,0 # $t2 is the counter
#lb $t3,ch1 # $t3 holds char1
#lb $t4,ch2 # $t4 holds char2
la $a0, userStr # print <original string>
li $v0, 4
syscall
la $a0, tester # print "Character is: "
li $v0, 4
syscall
la $a0,ch1 # print <character>
li $v0, 4
syscall
loop: lb $t0, userStr($t1) # $t0 holds the specific char from the string
beqz $t0,results # checks for end of string (null)
bne $t0,$t3,inc # compares char1 to char at index of string; increments index regardless of match
move $t0, $t4 # if both chars match, replace char1 with char2
inc: add $t1,$t1,1 # also, index +1
j loop # loop again
我在 PCSPIM 上运行它。我对汇编语言非常陌生。我通常用 C 或 Java 编程)
结果表明我的第一个字符是 p而且我没有第二个角色。但原始字符串不受影响。我现在将字符编程为字符串,因为我认为这可能有帮助,但事实并非如此。任何对此的帮助将不胜感激!
I've been working on this for hours and I can't figure it out. I am supposed to have the user input a string and two characters, then the program is supposed to replace any instance of the first character with the second..
EXAMPLE
Enter a string: String
Enter a character: t
Enter another character: p
Your new word is: Spring
Here is my code so far (in assembly language):
.data
userStr: .space 50
ch1: .space 1
ch2: .space 1
str: .asciiz "Please enter a string: "
char1: .asciiz "\nEnter a character: "
char2: .asciiz "\nEnter a replacement character: "
result1: .asciiz "\nOriginal String: "
result2: .asciiz "\nResult String: "
result3: .asciiz " Substitute "
result4: .asciiz " --> "
tester: .asciiz "\nCharacter is: "
tester2: .asciiz "\nCharacter 2 is: "
.text
.globl main
main:
la $a0, str # Prompt to enter a string
li $v0, 4
syscall
la $a0, userStr # input string is stored in 'userStr'
li $v0, 8
syscall # Calls the operating system
li $v0, 4
la $a0, char1 # Prints prompt to enter a character
syscall
la $a0, ch1 # Stores character as ch1
li $v0, 8
syscall # Calls the operating system
li $v0, 4
la $a0, char2 # Prints prompt to enter a character
syscall
la $a0, ch1 # Stores second character as ch2
li $v0, 8
syscall
la $a0, tester # print "Character is: "
li $v0, 4
syscall
la $a0,ch1 # print <character>
li $v0, 4
syscall
la $a0, tester2 # print "Character is: "
li $v0, 4
syscall
la $a0,ch2 # print <character>
li $v0, 4
syscall
li $t1,0 # $t1 is the index of the original string
li $t2,0 # $t2 is the counter
#lb $t3,ch1 # $t3 holds char1
#lb $t4,ch2 # $t4 holds char2
la $a0, userStr # print <original string>
li $v0, 4
syscall
la $a0, tester # print "Character is: "
li $v0, 4
syscall
la $a0,ch1 # print <character>
li $v0, 4
syscall
loop: lb $t0, userStr($t1) # $t0 holds the specific char from the string
beqz $t0,results # checks for end of string (null)
bne $t0,$t3,inc # compares char1 to char at index of string; increments index regardless of match
move $t0, $t4 # if both chars match, replace char1 with char2
inc: add $t1,$t1,1 # also, index +1
j loop # loop again
(I am running this on PCSPIM . I'm very new to assembly language. I normally program in C or Java)
The results say that my first character is p and that I do not have a second character. The original string isn't affected though. I'm programming the characters as strings for now because I thought that might help, but it hasn't. Any help on this would be very appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是一个关于如何操作的示例。
请注意,我正在使用延迟分支。
Here's an example on how do it.
Note that I'm using delayed branching.