【发布时间】:2021-09-08 11:09:15
【问题描述】:
我正在 MARS 上学习 MIPS,但我对以下代码感到困惑。因为当我将输入的字符串加载到保留空间时,即使没有终止符,代码也会正常输出。为什么是这样? 我认为每个字符串都需要一个终止符,以便输入缓冲区知道何时停止。 是自动填写的还是...?提前致谢。 代码是:
# Filename: mips3.asm
# Author: me
# Program to read a string from a user, and
# print that string back to the console.
.data
prompt: .asciiz "Please enter a string: "
output: .asciiz "\nYou typed the string: "
input: .space 81 # Reserve 81 bytes in Data segment
inputSize: .word 80 # Store value as 32 bit word on word boundary
# A word boundary is a 4 byte space on the
# input buffer's I/O bus.
# Word:
# A Word is the number of bits that can be transferred
# at one time on the data bus, and stored in a register
# in mips a word is 32 bits, that is, 4 bytes.
# Words are aways stored in consecutive bytes,
# starting with an address that is divisible by 4
.text
# Input a string.
li $v0, 4
la $a0, prompt
syscall
# Read the string.
li $v0, 8 # Takes two arguments
la $a0, input # arg1: Address of input buffer
lw $a1, inputSize # arg2: Maximum number of characters to read
syscall
# Output the text
li $v0, 4
la $a0, output
syscall
# Print string
li $v0, 4
la $a0, input
syscall
# Exit program
li $v0, 10
syscall
【问题讨论】:
-
显式长度字符串很好,只要您不尝试将它们传递给不需要长度的函数或系统调用(而是寻找终止符)。即采用隐式长度的 C 字符串。不幸的是,MARS 继承的传统读取字符串 SPIM 系统调用是愚蠢的,并且不返回读取的字节数。但读取文件 (
fread) 调用确实做到了。
标签: assembly mips mars-simulator