我有一个赋值,我必须在MMIX中获取一个输入并返回完全相同的东西,除了所有空格必须是换行符。我现在已经尝试了大约两天,并且已经找到了如何获取输入以及如何将其输出到控制台。但第二部分使我失望,明天将分配任务。这就是我到目前为止所拥有的:
LOC Data_Segment % Sets data storage location
GREG @ % Reserves a new global register
Wordbuffer BYTE 0 % New Byte for incoming words
LOC Wordbuffer+5 % After the buffer
Arg OCTA Wordbuffer
OCTA 21 % Sets max word length
LOC #100 % Code section
Main LDA $255,Arg % Loads the buffer into the global register
TRAP 0,Fgets,StdIn % Gets input
LDA $255,Wordbuffer % Puts input in global register
TRAP 0,Fputs,StdOut % Prints inputted word
TRAP 0,Halt,0 % Stops program
答案 0 :(得分:1)
我在对 Fgets 和 Fputs 的调用之间插入了一个循环
也回复评论
// space_to_newline.mms
LOC Data_Segment % Sets data storage location
GREG @ % Reserves a new global register
Wordbuffer BYTE 0 % New Byte for incoming words
LOC Wordbuffer+5 % After the buffer
Arg OCTA Wordbuffer
OCTA 21 % Sets max word length
LOC #100 % Code section
Main LDA $255,Arg % Loads the buffer into the global register
TRAP 0,Fgets,StdIn % Gets input
// loop over the string in Wordbuffer
// check if each character is a space
// replace with newline (0xa) if so
nl GREG #0a newline constant
LDA $0,Wordbuffer load base address
SET $1,0 initialize index to zero
0H LDBU $2,$0,$1 load byte in $2
BZ $2,2F done if null byte
CMP $255,$2,' ' check if space
PBNZ $255,1F skip character if not space
STBU nl,$0,$1 fallthru to replace with newline
1H INCL $1,1 increment index
JMP 0B loop back
2H LDA $255,Wordbuffer % Puts input in global register
TRAP 0,Fputs,StdOut % Prints inputted word
TRAP 0,Halt,0 % Stops program
使用 mmix 汇编器和模拟器进行测试
$ mmixal space_to_newline.mms
$ echo "abc defgh ijklm nopq" | mmix space_to_newline
abc
defgh
ijklm
nopq