How to print an string without the newline Mips Assembly language

Daniel Mendoza Pupo

I am trying to get the output in the same line with the result(string). I have found examples, but none of them explained the process. I know that the string has to be store in memory and then access it through bit by bit, but I got lost in the process.

.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

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

    la $a0, result
    li $v0, 4
    syscall

    li $v0,10
    syscall
Eraklon

The problem comes from that, when you read in the input the new line will be read also, so it has to be removed in order to achieve your needs.

This is one way to do it:

.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

Output

Enter your name: David
David ...that's the name

EDIT: The len is not even needed and code can be reduced to this (only the changed part)

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related