它如何知道字符串何时结束而没有终止符? (下面的代码)
How does it know when string ends without a terminator? (Code below)
我正在 MARS 上学习 MIPS,但我对以下代码感到困惑。因为当我将输入的字符串加载到保留的 space 中时,即使没有终止符,代码也能正常输出。为什么是这样?
我认为每个字符串都需要一个终止符,以便输入缓冲区知道何时停止。
是自动填写还是...?提前致谢。
代码是:
# 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
MARS 系统调用文档:
... Service 8 — Follows semantics of UNIX fgets
. For specified length n
, the input string from the user can be no longer than n-1
. If the input string is less than that, this syscall adds newline to end. In either case, this syscall pads with null byte. ...
因此,在您的情况下,任何 78 字节或更少字节的输入字符串都会得到一个换行符和一个空终止符。
换行是一种痛苦,因为我们通常不想要它。
另一方面,.space 81
将在程序加载时为零,因此在第一个系统调用之后,您将看到零填充到末尾,但同一区域的第二个不一定(即如果输入更短),因此 syscall#8 的空终止行为是有用的,也是必要的——特别是因为服务没有 return 输入的长度!
此外,请注意 MARS 在菜单项中提供了文档:
帮助菜单 ↪ MIPS 选项卡 → 系统调用子选项卡
“帮助”菜单中还有一些其他有趣的 material/information。
我正在 MARS 上学习 MIPS,但我对以下代码感到困惑。因为当我将输入的字符串加载到保留的 space 中时,即使没有终止符,代码也能正常输出。为什么是这样? 我认为每个字符串都需要一个终止符,以便输入缓冲区知道何时停止。 是自动填写还是...?提前致谢。 代码是:
# 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
MARS 系统调用文档:
... Service 8 — Follows semantics of UNIX
fgets
. For specified lengthn
, the input string from the user can be no longer thann-1
. If the input string is less than that, this syscall adds newline to end. In either case, this syscall pads with null byte. ...
因此,在您的情况下,任何 78 字节或更少字节的输入字符串都会得到一个换行符和一个空终止符。
换行是一种痛苦,因为我们通常不想要它。
另一方面,.space 81
将在程序加载时为零,因此在第一个系统调用之后,您将看到零填充到末尾,但同一区域的第二个不一定(即如果输入更短),因此 syscall#8 的空终止行为是有用的,也是必要的——特别是因为服务没有 return 输入的长度!
此外,请注意 MARS 在菜单项中提供了文档:
帮助菜单 ↪ MIPS 选项卡 → 系统调用子选项卡
“帮助”菜单中还有一些其他有趣的 material/information。