如何使用汇编语言一次从用户获取一个字符?

时间:2011-11-04 00:40:43

标签: assembly character mips

我一直在努力工作几个小时,我无法理解。我应该让用户输入一个字符串和两个字符,然后该程序应该用第二个字符替换第一个字符的任何实例..


实施例

输入字符串: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,而我没有第二个角色。但原始字符串不受影响。我现在正在将字符编程为字符串,因为我认为这可能有所帮助,但事实并非如此。任何有关这方面的帮助将非常感谢!

1 个答案:

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

请注意,我正在使用延迟分支。