如何在不使用换行符Mips汇编语言的情况下打印字符串

问题描述 投票:0回答:1

我正在尝试将输出与result(string)放在同一行。我找到了示例,但没有一个示例可以解释该过程,我知道该字符串必须存储在内存中,然后一点一点地访问它,但是我在过程中迷路了

。data提示:.asciiz“输入您的姓名:”名称:.space 101

结果:.asciiz“ ...就是名字”。文本.globl main

主要:la $ a0,提示li $ v0,4syscall

la $a0, name # Get the input
li $v0, 8
li $a1,101
syscall

la $a0, name # print the result
li $v0, 4
syscall

la $a0, result
li $v0, 4
syscall

li $v0,10
syscall
mips
1个回答
0
投票

问题是,当您在输入中读取新行时,也会读取它,因此为了满足您的需要,必须将其删除。

这是一种实现方法:

.data
prompt: .asciiz "Enter your name: "
name: .space 101

result: .asciiz " ...that's the name"
.text
.globl main

main:
    la $a0, prompt
    li $v0, 4
    syscall

    la $a0, name # Get the input
    li $v0, 8
    li $a1,101
    syscall

    addi $t1, $t1, 0 # len = 0
len_to_new_line:
    lb $t2, ($a0) # t2 = *a0
    beq $t2, '\n', end # if t2 == '\n' -> stop
    addi $t1, $t1, 1 # len++
    addi $a0, $a0, 1 # a0++
    b len_to_new_line   
end:
    la $a0, name 
    add $a0, $a0, $t1 
    sb $zero, ($a0) # overwrite '\n' with 0

    la $a0, name # print the result
    li $v0, 4
    syscall

    la $a0, result
    li $v0, 4
    syscall

    li $v0,10
    syscall

输出

Enter your name: David
David ...that's the name
© www.soinside.com 2019 - 2024. All rights reserved.