2011-11-04 54 views
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,而且我沒有第二個字符。原始字符串不受影響。我現在將字符編碼爲字符串,因爲我認爲這可能有幫助,但它沒有。任何對此的幫助將非常感激!

+0

不組裝。什麼是*結果*?你沒有聲明這個符號。我也可以建議MARS作爲SPIM的更好替代品。 – m0skit0

回答

0

下面是一個如何做的例子。

.text 

# Read string 
li $v0, 8 
la $a0, str_input 
li $a1, 50 
syscall 

# Read char1 
jal readchar 
nop 

# Store char1 in $s0 
or $s0, $v0, $zero 

# Read char2 
jal readchar 
nop 

# Store char2 in $s1 
or $s1, $v0, $zero 

# Call str_replace 
la $a0, str_input 
or $a1, $s0, $zero 
jal str_replace 
or $a2, $s1, $zero 

# Print string result 
li $v0, 4 
la $a0, str_input 
syscall 

########################################### 
readchar: 
# Read char from input, returns char on $v0 

li $v0, 12 
syscall 
jr $ra 
nop 
########################################### 

########################################### 
str_replace: 
# Replaces one char with another in a string 
# $a0 -> string buffer to be manipulated 
# $a1 -> char to be replaced 
# $a2 -> char to replace with 

or $t0, $a0, $zero # $t0 -> pointer to char 

str_replace_loop: 
lb $t1, 0($t0) # $t1 -> current char 
beq $t1, $zero, str_replace_exit # Is end of string? 
nop 

bne $t1, $a1, str_replace_next # Is char to replace? 
nop 
sb $a2, 0($t0) 

str_replace_next: 
addiu $t0, $t0, 1 
b str_replace_loop 
nop 

str_replace_exit: 
jr $ra 
nop 

########################################### 

exit: 
li $v0, 10 
syscall 

.data 
str_input: .byte 50 

請注意,我使用延遲分支。

相關問題