如何使用 MARS/SPIM 系统调用在同一行打印数字和字符串?
How print a number and string on the same line with MARS/SPIM system calls?
最近,我正在做作业,并且正在努力在同一行上打印变量和字符串。我想问一下如何在 MIPS 中将它们打印在同一行上?
我目前的成绩:
Please enter your nickname: Harry
Please enter your surname: Chan
Harry
Chan
How old are you? 20
You are 20
years old.
我想要的:
Please enter your nickname: Harry
Please enter your surname: Chan
Harry
Chan
How old are you? 20
You are 20 years old.
MIPS 代码:
.data
question1: .asciiz "Please enter your nickname: "
question2: .asciiz "Please enter your surname: "
question3: .asciiz "How old are you? "
buffer1: .space 60
buffer2: .space 60
buffer3: .space 60
str1: .asciiz "You are "
str2: .asciiz " years old."
#-----Text Segment-----------------
.text
.globl __start
__start:
# ----------------------
# Write your code here
li $v0, 4
la $a0, question1
syscall
li $v0,8 #take in input
la $a0, buffer1 #load byte space into address
li $a1, 60 # allot the byte space for string
move $t0,$a0 #save string to t0
syscall
li $v0, 4
la $a0, question2
syscall
li $v0,8 #take in input
la $a0, buffer2 #load byte space into address
li $a1, 60 # allot the byte space for string
move $t1,$a0 #save string to t1
syscall
la $a0, buffer1 #reload byte space to primary address
move $a0,$t0 # primary address = t0 address (load pointer)
li $v0,4 # print string
syscall
la $a0, buffer2 #reload byte space to primary address
move $a0,$t1 # primary address = t1 address (load pointer)
li $v0,4 # print string
syscall
li $v0, 4
la $a0, question3
syscall
li $v0,8 #take in input
la $a0, buffer2 #load byte space into address
li $a1, 60 # allot the byte space for string
move $t2,$a0 #save string to t2
syscall
li $v0, 4
la $a0, str1
syscall
la $a0, buffer1 #reload byte space to primary address
move $a0,$t2 # primary address = t2 address (load pointer)
li $v0,4 # print string
syscall
li $v0, 4
la $a0, str2
syscall
# ----------------------
# Terminate the program
li $v0, 10
syscall
我同学的回答:
#print out question 3 and store input
li $v0, 4
la $a0, question3
syscall
li $v0, 5
syscall
move $t0, $v0
#print out str 1, str2 and answer
li $v0, 4
la $a0, str1
syscall
li $v0, 1
move $a0, $t0
syscall
li $v0, 4
la $a0, str2
syscall
最近,我正在做作业,并且正在努力在同一行上打印变量和字符串。我想问一下如何在 MIPS 中将它们打印在同一行上?
我目前的成绩:
Please enter your nickname: Harry
Please enter your surname: Chan
Harry
Chan
How old are you? 20
You are 20
years old.
我想要的:
Please enter your nickname: Harry
Please enter your surname: Chan
Harry
Chan
How old are you? 20
You are 20 years old.
MIPS 代码:
.data
question1: .asciiz "Please enter your nickname: "
question2: .asciiz "Please enter your surname: "
question3: .asciiz "How old are you? "
buffer1: .space 60
buffer2: .space 60
buffer3: .space 60
str1: .asciiz "You are "
str2: .asciiz " years old."
#-----Text Segment-----------------
.text
.globl __start
__start:
# ----------------------
# Write your code here
li $v0, 4
la $a0, question1
syscall
li $v0,8 #take in input
la $a0, buffer1 #load byte space into address
li $a1, 60 # allot the byte space for string
move $t0,$a0 #save string to t0
syscall
li $v0, 4
la $a0, question2
syscall
li $v0,8 #take in input
la $a0, buffer2 #load byte space into address
li $a1, 60 # allot the byte space for string
move $t1,$a0 #save string to t1
syscall
la $a0, buffer1 #reload byte space to primary address
move $a0,$t0 # primary address = t0 address (load pointer)
li $v0,4 # print string
syscall
la $a0, buffer2 #reload byte space to primary address
move $a0,$t1 # primary address = t1 address (load pointer)
li $v0,4 # print string
syscall
li $v0, 4
la $a0, question3
syscall
li $v0,8 #take in input
la $a0, buffer2 #load byte space into address
li $a1, 60 # allot the byte space for string
move $t2,$a0 #save string to t2
syscall
li $v0, 4
la $a0, str1
syscall
la $a0, buffer1 #reload byte space to primary address
move $a0,$t2 # primary address = t2 address (load pointer)
li $v0,4 # print string
syscall
li $v0, 4
la $a0, str2
syscall
# ----------------------
# Terminate the program
li $v0, 10
syscall
我同学的回答:
#print out question 3 and store input
li $v0, 4
la $a0, question3
syscall
li $v0, 5
syscall
move $t0, $v0
#print out str 1, str2 and answer
li $v0, 4
la $a0, str1
syscall
li $v0, 1
move $a0, $t0
syscall
li $v0, 4
la $a0, str2
syscall