0
我一直在這個工作幾個小時,我無法弄清楚。我應該讓用戶輸入一個字符串和兩個字符,那麼程序應該替換第二個是第一個字符的任何實例..如何使用匯編語言從用戶一次獲取一個字符?
例
輸入的字符串:字符串
輸入一個字符:T
進入另一個角色:對
您的新詞是:春天
這裏是我到目前爲止的代碼(彙編語言):
.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,而且我沒有第二個字符。原始字符串不受影響。我現在將字符編碼爲字符串,因爲我認爲這可能有幫助,但它沒有。任何對此的幫助將非常感激!
不組裝。什麼是*結果*?你沒有聲明這個符號。我也可以建議MARS作爲SPIM的更好替代品。 – m0skit0